title: Checking a value against multiple options
body: <p>This is not so much a question as well as an idea I'm willing to share.</p>
  
  <p>Sometimes I come across a situation where I want to check a value against a small set of possible matches.</p>
  
  <p>Let's say we have a String variable called <code>value</code> and option literals <code>"match1"</code>, <code>"match2"</code> and <code>"match3"</code></p>
  
  <p>Currently there are a few ways we can do this, the first is with a simple if-statement:</p>
  
  <pre><code>// Putting the literal first to prevent NullPointerExceptions
  if ("match1".equals(value) || "match2".equals(value) || "match3".equals(value)) { // do something }
  </code></pre>
  
  <p>This looks horrible to me, especially if the number of options increases. I find it unreadable and unworkable.</p>
  
  <p>Another way to implement this is with a  <code>switch</code>:</p>
  
  <pre><code>switch (value) {
      case "match1":
      case "match2":
      case "match3":
          // do something
          break;
      default:
          break;
  }
  </code></pre>
  
  <p>Again this is not ideal, I don't like switch-statements very much as they look like a weird mix of legacy code (using labels and gotos) and something modern. The biggest drawback however might be that it <strong>only works for Strings, ints and enums</strong>.</p>
  
  <p>I'm using this custom generic method at the moment where I can pass in a variable number of matchable parameters:</p>
  
  <pre><code>@SafeVarargs
  public static &lt;T&gt; boolean hasMatch(final T value, final T... options) {
      if (options == null || value == null) {
          return false;
      }
  
      return Arrays.asList(options).contains(value); // uses equals to match items
  }
  </code></pre>
  
  <p>This works for me and I can call it like this: <code>hasMatch(value, "match1", "match2", "match3");</code></p>
  
  <p>I still find it ugly though, but Java 8 gives us the power of the stream:</p>
  
  <pre><code>@SafeVarargs
  public static &lt;T&gt; boolean hasMatch(final T value, final T... options) {
      if (options == null || value == null) {
          return false;
      }
  
      return Arrays.stream(options).anyMatch(Predicate.isEqual(value));
  }
  </code></pre>
  
  <p>This looks pretty much the same as the previous solution, just syntactically different.</p>
  
  <p>Ideally there would be something in the language to solve this issue, like an <code>in</code> keyword, so I can do:</p>
  
  <pre><code>if (value in {"match1", "match2", "match3"})
  </code></pre>
  
  <p>Is there a JSR proposal for this? If not, should there be? Are there any other ways to solve this issue?
  I'm really looking for the wisdom of the crowd here to see if something like this is a good or bad idea or if it has been thought of before.</p>