org.bridj
Class FlagSet<E extends Enum<E>>

java.lang.Object
  extended by org.bridj.FlagSet<E>
All Implemented Interfaces:
Iterable<E>, ValuedEnum<E>
Direct Known Subclasses:
FlagSet.IntFlagSet

public class FlagSet<E extends Enum<E>>
extends Object
implements ValuedEnum<E>

Set of int-valued enum values that is itself int-valued (bitwise OR of all the values).
This helps use Java enums (that implement ValuedEnum) as combinable C flags (see fromValues(E...) ).

Author:
ochafik

Nested Class Summary
static class FlagSet.IntFlagSet<E extends Enum<E>>
           
 
Constructor Summary
protected FlagSet(long value, Class<E> enumClass, E[] enumClassValues)
           
 
Method Summary
 FlagSet<E> and(E... valuesToBeCombinedWithOR)
           
 boolean equals(Object o)
           
static
<EE extends Enum<EE>>
FlagSet.IntFlagSet<EE>
fromValue(int value, Class<EE> enumClass)
           
static
<EE extends Enum<EE>>
FlagSet.IntFlagSet<EE>
fromValue(int value, EE... enumValue)
           
static
<EE extends Enum<EE>>
FlagSet<EE>
fromValue(long value, Class<EE> enumClass)
           
static
<EE extends Enum<EE>>
FlagSet<EE>
fromValue(long value, EE... enumValue)
           
static
<EE extends Enum<EE>>
FlagSet<EE>
fromValue(ValuedEnum<EE> value)
           
static
<E extends Enum<E>>
FlagSet<E>
fromValues(E... enumValues)
           
static List<Long> getBits(long value)
          Isolate bits that are set in the value.
 Class<E> getEnumClass()
           
protected  E[] getEnumClassValues()
           
protected  List<E> getMatchingEnums()
           
 boolean has(E... valuesToBeCombinedWithOR)
          Tests if the flagset value is contains the OR combination of all the given values combined with bitwise OR operations.
 int hashCode()
           
 boolean is(E... valuesToBeCombinedWithOR)
          Tests if the flagset value is equal to the OR combination of all the given values combined with bitwise OR operations.
 Iterator<E> iterator()
           
 FlagSet<E> or(E... valuesToBeCombinedWithOR)
           
 E toEnum()
           
 String toString()
           
 long value()
          Get the integral value of this FlagSet.
 FlagSet<E> without(E... valuesToBeCombinedWithOR)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FlagSet

protected FlagSet(long value,
                  Class<E> enumClass,
                  E[] enumClassValues)
Method Detail

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E extends Enum<E>>

toEnum

public E toEnum()

toString

public String toString()
Overrides:
toString in class Object

fromValue

public static <EE extends Enum<EE>> FlagSet<EE> fromValue(long value,
                                                          Class<EE> enumClass)

fromValue

public static <EE extends Enum<EE>> FlagSet.IntFlagSet<EE> fromValue(int value,
                                                                     Class<EE> enumClass)

fromValue

public static <EE extends Enum<EE>> FlagSet<EE> fromValue(ValuedEnum<EE> value)

fromValue

public static <EE extends Enum<EE>> FlagSet<EE> fromValue(long value,
                                                          EE... enumValue)

fromValue

public static <EE extends Enum<EE>> FlagSet.IntFlagSet<EE> fromValue(int value,
                                                                     EE... enumValue)

getBits

public static List<Long> getBits(long value)
Isolate bits that are set in the value.
For instance, getBits(0xf) yields 0x1, 0x2, 0x4, 0x8

Parameters:
value -
Returns:
split bits, which give the value back if OR-ed all together.

value

public long value()
Get the integral value of this FlagSet.

Specified by:
value in interface ValuedEnum<E extends Enum<E>>
Returns:
value of the flag set

getEnumClass

public Class<E> getEnumClass()

getEnumClassValues

protected E[] getEnumClassValues()

is

public boolean is(E... valuesToBeCombinedWithOR)
Tests if the flagset value is equal to the OR combination of all the given values combined with bitwise OR operations.
The following C code :
E v = ...; // E is an enum type
 if (v == (E_V1 | E_V2)) { ... }
 
Can be translated to the following Java + BridJ code :
FlagSet<E> v = ...;
 if (v.is(E_V1, E_V2)) { ... }
 


has

public boolean has(E... valuesToBeCombinedWithOR)
Tests if the flagset value is contains the OR combination of all the given values combined with bitwise OR operations.
The following C code :
E v = ...; // E is an enum type
 if (v & (E_V1 | E_V2)) { ... }
 
Can be translated to the following Java + BridJ code :
FlagSet<E> v = ...;
 if (v.has(E_V1, E_V2)) { ... }
 


or

public FlagSet<E> or(E... valuesToBeCombinedWithOR)

without

public FlagSet<E> without(E... valuesToBeCombinedWithOR)

and

public FlagSet<E> and(E... valuesToBeCombinedWithOR)

getMatchingEnums

protected List<E> getMatchingEnums()

fromValues

public static <E extends Enum<E>> FlagSet<E> fromValues(E... enumValues)


Copyright © 2009-2012. All Rights Reserved.