Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is another demonstration of what you're looking for. The difference between this and previous solutions is that this one is more generic and reusable pattern. This in fact goes beyond the original problem, to show some other benefits of this approach. So you might just comment the bits you don't need. I also attach a unit test to demonstrate the behaviour.</p> <p>So basically to look for name <code>Apple</code> or <code>APPLE</code> in one of these enums just write:</p> <p><code>FruitVeg&lt;?&gt; fvg = getEnum("APPLE", Fruits.class, Vegetables.class);</code></p> <p><code>FruitVeg&lt;&gt;</code> is an interface, which allows to also tap inside of Enum, this interface allows to do some very interesting things with enums below. Here are just some of the things you could do with that:</p> <ul> <li><p><code>Enum.valueOf(fvg.getDeclaringClass(), fvg.name())</code>: <em>returns enum Value</em> e.g. <strong>APPLE</strong> </p></li> <li><p><code>fvg.getRaw()</code>: <em>returns enum Value</em> e.g. <strong>APPLE</strong> </p></li> <li><p><code>fvg.name()</code> : <em>returns enum's String Name</em> e.g. <strong>APPLE</strong></p></li> <li><p><code>fvg.getFriendlyName()</code> : e.g. <strong>Apple</strong> </p></li> <li><p><code>fvg.getDeclaringClass()</code> : <em>returns</em> <code>Class&lt;Enum&lt;?&gt;&gt;</code> e.g. <strong>class ox.dummy.dummyTest$Fruits</strong></p></li> <li><p><code>fvg.getClass()</code> : <strong>class ox.dummy.dummyTest$Fruits</strong> <em>returns</em> <code>Class&lt;?&gt;</code></p></li> <li><p><code>EnumSet.allOf(fvg.getDeclaringClass()))</code> : e.g. <strong>[APPLE, PEAR]</strong></p></li> </ul> <p>Here is code</p> <pre><code> @Test public void doSimpleTest() throws Exception { FruitVeg&lt;?&gt; fvg = getEnum("APPLE", Fruits.class, Vegetables.class); log.info("{} : {} : {} : {} : {}", fvg.name(), fvg.getFriendlyName(), fvg.getClass(), fvg.getDeclaringClass(), EnumSet.allOf(fvg.getDeclaringClass())); log.info("get enum: {} ", Enum.valueOf(fvg.getDeclaringClass(), fvg.name())); } public interface FruitVeg&lt;T extends Enum&lt;T&gt;&gt; { String name(); String getFriendlyName(); Class&lt;T&gt; getDeclaringClass(); T getRaw(); } enum Fruits implements FruitVeg&lt;Fruits&gt; { APPLE("Apple"), PEAR("Pear"); Fruits(String friendlyName) { this.friendlyName = friendlyName; } private final String friendlyName; @Override public String getFriendlyName() { return friendlyName; } @Override public Fruits getRaw() { return this; } } enum Vegetables implements FruitVeg&lt;Vegetables&gt; { CARROT("Carrot"), LETTUCE("Lettuce"); Vegetables(String friendlyName) { this.friendlyName = friendlyName; } private final String friendlyName; @Override public String getFriendlyName() { return friendlyName; } @Override public Vegetables getRaw() { return this; } } public static FruitVeg&lt;?&gt; getEnum(String name, Class&lt;? extends FruitVeg&lt;?&gt;&gt;... fvgClasses) { for (Class&lt;? extends FruitVeg&lt;?&gt;&gt; fruitVegCLass : Arrays.asList(fvgClasses)) { for (FruitVeg&lt;?&gt; fvg : fruitVegCLass.getEnumConstants()) { if (name.equals(fvg.name()) || name.equals(fvg.getFriendlyName())) { return fvg; } } } return null; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload