Re: How to call a function with an enum like argument
Wojtek wrote:
It sounded like the OP wanted to pass the entire enum:
You mean the entire set of values, I assume.
doSomethingWith( SomeEnum );
which needs to be
doSomethingWith( EnumSet.<SomeEnum> allOf(SomeEnum.class) );
That is not the only way to pass all the values. One could also use
doSomethingWith( SomeEnum.values() );
(Strictly speaking, the EnumSet version doesn't need to specify the
'<SomeEnum>' as type inference will take care of that.)
<sscce>
package eegee;
import java.util.EnumSet;
enum Foo { FOO, BAR, BAZ; }
/** Fooeynum. */
public class Fooeynum
{
Foo foo = Foo.BAR;
void doSomethingWith( Foo [] vals )
{
System.out.println( "using values" );
for ( Foo val : vals )
{
System.out.println( val.toString() );
}
}
void doSomethingWith( EnumSet<?> vals )
{
System.out.println( "using EnumSet" );
for ( Object val : vals )
{
System.out.println( val.toString() );
}
}
/** Main method.
* @param args <code>String []</code> command line arguments.
*/
public static void main( String [] args )
{
Fooeynum fooey = new Fooeynum();
System.out.println();
fooey.doSomethingWith( Foo.values() );
System.out.println();
fooey.doSomethingWith( EnumSet.allOf( Foo.class ));
}
}
</sscce>
Output:
using values
FOO
BAR
BAZ
using EnumSet
FOO
BAR
BAZ
--
Lew