如何将Enum的值与Enum方法中Enum的所有可能值进行比较,并避免丢失返回语句

  • 本文关键字:Enum 语句 比较 返回 有可能 方法 java enums
  • 更新时间 :
  • 英文 :


我只是在学习Java中的枚举。当我运行下面的代码时,我得到了一个错误,我也在下面重现。基本上,我的问题是:当我在枚举中定义一个方法,并且在该方法中我想检查枚举的值,以便我可以根据该值做一些事情时,我如何执行此检查?下面我有一个Enum,它有三个可能的值,在方法getNext中,我有三个if语句将该Enum的值与三个可能值中的每一个进行比较。但我仍然会犯一个错误,说有一条路没有回头路。

package enumerations;
enum TrafficLightColor2 {
RED(12), GREEN(10), YELLOW(2);
private int waitTime;
TrafficLightColor2(int waitTime) {
this.waitTime = waitTime;
}
int getWaitTime() {
return waitTime;
}
TrafficLightColor2 getNext() {
if (this.equals(TrafficLightColor2.GREEN)) {
return TrafficLightColor2.YELLOW;
}
if (this.equals(TrafficLightColor2.YELLOW)) {
return TrafficLightColor2.RED;
}
if (this.equals(TrafficLightColor2.RED)) {
return TrafficLightColor2.GREEN;
}
}
}
// A computerized traffic light.
class TrafficLightSimulator2 implements Runnable {
private Thread thrd; // holds the thread that runs the simulation
private TrafficLightColor2 tlc; // holds the traffic light color
boolean stop = false; // set to true to stop the simulation
boolean changed = false; // true when the light has changed
TrafficLightSimulator2(TrafficLightColor2 init) {
tlc = init;
thrd = new Thread(this);
thrd.start();
}
TrafficLightSimulator2() {
tlc = TrafficLightColor2.RED;
thrd = new Thread(this);
thrd.start();
}
// Start up the light.
public void run() {
while (!stop) {
try {
Thread.sleep(tlc.getWaitTime());
} catch (InterruptedException exc) {
System.out.println(exc);
}
changeColor();
}
}
// Change color.
synchronized void changeColor() {
tlc = tlc.getNext();
changed = true;
notify(); // signal that the light has changed
}
// Wait until a light change occurs.
synchronized void waitForChange() {
try {
while (!changed)
wait(); // wait for light to change
changed = false;
} catch (InterruptedException exc) {
System.out.println(exc);
}
}
// Return current color.
synchronized TrafficLightColor2 getColor() {
return tlc;
}
// Stop the traffic light.
synchronized void cancel() {
stop = true;
}
}

class TrafficLightDemo2 {
public static void main(String args[]) {
TrafficLightSimulator tl =
new TrafficLightSimulator(TrafficLightColor.GREEN);
for (int i = 0; i < 9; i++) {
System.out.println(tl.getColor());
tl.waitForChange();
}
tl.cancel();
}
}

我得到错误

$ javac enumerations/TrafficLightDemo2.java
enumerations/TrafficLightDemo2.java:26: error: missing return statement
}
^
1 error
TrafficLightColor2 getNext() {
if (this.equals(TrafficLightColor2.GREEN)) {
return TrafficLightColor2.YELLOW;
}
if (this.equals(TrafficLightColor2.YELLOW)) {
return TrafficLightColor2.RED;
}
if (this.equals(TrafficLightColor2.RED)) {
return TrafficLightColor2.GREEN;
}
}

如果3个if都为false,则此方法不会返回值。

在和处添加返回或更好地抛出错误,例如

throw new IllegalArgumentException("Unsupported enum")

在enum类中使用实例字段的优点是,您可以轻松地将实现细节与独立于API的常量相关联。换句话说,您可以很容易地将数据与枚举常量相关联,这将允许您在需要添加新枚举常量的情况下获得一个优雅的解决方案,而您不会永远与之结婚。

因此,您可以在履行以下相同合同的同时大大简化您的实现:

enum TrafficLightColor2 {
RED(2, 12), 
GREEN(0, 10), 
YELLOW(1, 2);

private int order;   // implementation detail; non-exported
private int waitTime;
TrafficLightColor2(int ord, int waitTime) {
this.order = ord;
this.waitTime = waitTime;
}
int getWaitTime() {
return waitTime;
}
TrafficLightColor2 getNext() {
final int nextColor = (this.order + 1) % 3;  // magic numbers introduce fragility
return Arrays.stream(TrafficLight2.values())
.filter(e -> e.order == nextColor)
.findAny()
.get();
}
}

此版本与原始实现相比有一些优势:它更易于维护,因为如果添加枚举常量,编译器将强制您添加顺序值。在原来的程序中,如果在添加常量后忘记修改if-else块,则程序将继续工作,但不会提供正确的行为。由于order的实现是隐藏的,因此您可以随时将其移除或更改为其他实现,而不会影响API的正确性。

您是否考虑将下一个状态与声明的值一起包含?

public enum TrafficLightColor2 {
RED(12, "GREEN"), GREEN(10, "YELLOW"), YELLOW(2, "RED");
int waitTime;
String nextState;
Configurations(int waitTime, String nextState) {
this.waitTime = waitTime;
this.nextState = nextState;
}
public int getWaitTime() {
return waitTime;
}
public String getNextState() {
return nextState;
}
}

有了这个,你可以获得的下一个状态

TrafficLightColor2 trafficLightColor = TrafficLightColor2.GREEN;
System.out.println(TrafficLightColor2.valueOf(trafficLightColor.getNextState()));

最新更新