-4
Write a Java method to check if a given string matches any value in a given enum.
Your method will take two parameters, one a string and one an enum, and return true
if the string representation of any of the enum's elements matches the given string, or false
otherwise. Note that your method should still function normally if the String parameter is null
, and that you aren't allowed to use any external libraries.
Test cases:
public enum E { A, B, C; }
public enum F { X, Y, Z; }
check("a", E.A); // is false (because "A" is uppercase)
check(null, E.A); // is false
check("C", E.A); // is true
check("X", F.X); // is true
check(null, F.X); // is false
check("y", F.X); // is false
The method has access (as a solver correctly understood) to a generic type E that has the only bound that "E extends Enum(E)".
Reference implementation (Ungolfed):
boolean <E extends Enum<E>> check(String s, E e){
for(E ex : e.getDeclaringClass().getEnumConstants())
if(ex.toString().equals(s)) return true;
return false;
}
Another possible implementation (due to a solution) is (Ungolfed):
public static <E extends Enum<E>> boolean check(String s, E e){
return java.util.stream.Stream.of(e.getDeclaringClass().getEnumConstants()).anyMatch(x->x.name().equals(s));
}
What I'm looking for is the shortest thing I could put after return (or, as a solutor did, after "(String s,E e)->)".
This is code-golf, so fewest bytes wins!
What should the method do? With the current spec, always returning
true
is allowed. – Zgarb – 2016-12-01T09:43:27.343I'll edit, probably the request on parameters is too restrictive, I'll also add an example. – SamCle88 – 2016-12-01T09:45:35.383
3Single-language challenges are also discouraged here. Unless the challenge only makes sense for Java, you should allow all languages. – Zgarb – 2016-12-01T09:47:47.717
In this case, I'm starting from a Java enum so, unless another language can deal with a Java class, I don't see why I should allow other languages. (Sorry for my obstinacy, but I'm new here and I wanted this to solve a more practical than theoretical problem). – SamCle88 – 2016-12-01T09:54:49.593
Okay, now I get what the challenge is about. Because it's so focused, I think it would be received better as a [tag:tips] question that asks for golfing advice, like "I want to check whether the name of a Java enum value matches a given string, is there a shorter way than this?" – Zgarb – 2016-12-01T10:09:10.967
Ok, tried to edit in that sense. May I know if there are some more not clear points left? – SamCle88 – 2016-12-01T10:54:14.600
So is the code to check whether a given string is found in an enum? – george – 2016-12-01T11:07:45.590
1The task could be described in the main text more explicitly, e.g. "The method should return
true
if the string is equal to the string representation of any value in the enum class, andfalse
otherwise.null
strings should also result infalse
." Remove the "shortest code wins" part too; [tag:tips] questions are not competitions. – Zgarb – 2016-12-01T11:37:01.740Edited as suggested. Thank you. – SamCle88 – 2016-12-01T13:57:28.140
Can we assume that the enum class will be
E
? – Mego – 2016-12-02T10:55:49.690I prefer if you don't make assumptions on the class
E
because I'm going to use it within a context with generics. – SamCle88 – 2016-12-02T11:11:20.187ockquote>
I prefer if you don't make assumptions on the class E
That is a bit self-contradictory, as looking at your reference method you do use a literal E type at least twice there. You should then declare it as: <E extends Enum> void check(String s,E e) instead. – zeppelin – 2016-12-02T19:53:04.790
1Otherwise i can not see why E.valueOf() would be incorrect to do, or why do we need a second argument at all (as that is no different of how your check method operates). – zeppelin – 2016-12-02T19:54:53.217
1>
boolean c(String, E)
then "given" means two different things in the first sentence of the question. Other interpretations would lead to signaturesboolean c(String, Enum)
orboolean c(String, Class<? extends Enum>)
. The comment above that you "prefer if you don't make assumptions on the classE
" suggests that you want the second or third signature, but the reference implementation uses the first. 2. It's confusing to ask for return values ofTrue
andFalse
I'll try to edit it. – SamCle88 – 2017-01-11T21:07:33.090
There are now two reference implementations with different signatures. In my opinion that's adding to the confusion, not reducing it. – Peter Taylor – 2017-01-12T09:05:50.467
I don't get where the problem is. This method takes a string and a variable that must be an instance of some enum and returns true iff the string represents an instance of the same enum as the example given by the second argument. – SamCle88 – 2017-01-12T09:57:45.963