How to avoid repetition when working with primitive types?
<p>I have the need to perform algorithms on various primitive types; the algorithm is essentially the same with the exception of which type the variables are.  So for instance,</p>

<pre><code>/**
* Determine if &lt;code&gt;value&lt;/code&gt; is the bitwise OR of elements of &lt;code&gt;validValues&lt;/code&gt; array. 
* For instance, our valid choices are 0001, 0010, and 1000.
* We are given a value of 1001.  This is valid because it can be made from
* ORing together 0001 and 1000.
* On the other hand, if we are given a value of 1111, this is invalid because
* you cannot turn on the second bit from left by ORing together those 3
* valid values.
*/
public static boolean isValid(long value, long[] validValues) {
    for (long validOption : validValues) {
        value &amp;= ~validOption;
    }
    return value == 0;
}

public static boolean isValid(int value, int[] validValues) {
    for (int validOption : validValues) {
        value &amp;= ~validOption;
    }
    return value == 0;
}
</code></pre>

<p>How can I avoid this repetition?  I know there's no way to genericize primitive arrays, so my hands seem tied.  I have instances of primitive arrays and not boxed arrays of say Number objects, so I do not want to go that route either.  </p>

<p>I know there are a lot of questions about primitives with respect to arrays, autoboxing, etc., but I haven't seen it formulated in quite this way, and I haven't seen a decisive answer on how to interact with these arrays.</p>

<p>I suppose I could do something like:</p>

<pre><code>public static&lt;E extends Number&gt; boolean isValid(E value, List&lt;E&gt; numbers) {
    long theValue = value.longValue();
    for (Number validOption : numbers) {
        theValue &amp;= ~validOption.longValue();
    }
    return theValue == 0;
}
</code></pre>

<p>and then</p>

<pre><code>public static boolean isValid(long value, long[] validValues) {
    return isValid(value, Arrays.asList(ArrayUtils.toObject(validValues)));
}

public static boolean isValid(int value, int[] validValues) {
    return isValid(value, Arrays.asList(ArrayUtils.toObject(validValues)));
}
</code></pre>

<p>Is that really much better though?  That way will create a lot more objects than the original implementation, though it DRYs up the source code.  Any thoughts in this matter would be appreciated.</p>
