2 of 2
replaced http://stackoverflow.com/ with https://stackoverflow.com/
You can create an isValid
or checkValid
method inside the enum and override it in h3
if it's not changing runtime.
public enum TestEnum {
h1,
h2,
h3 {
@Override
public boolean isValid() {
return false;
}
},
h4;
public boolean isValid() {
return true;
}
}
It returns true
for h1
, h2
and h4
, false
for h3
.
You could eliminate the results
variable from checkExcept:
public static boolean checkExcept(String el, TestEnum... except) {
try {
return !Arrays.asList(except).contains(TestEnum.valueOf(el));
} catch (Exception e) {
return false;
}
}
Please note that catching Exception is almost always a bad idea. I'd catch a more specific exception here (IllegalArgumentException
).
palacsint
- 30.3k
- 9
- 82
- 157
default