net.floodlightcontroller.util
Class EnumBitmaps

java.lang.Object
  extended by net.floodlightcontroller.util.EnumBitmaps

public class EnumBitmaps
extends java.lang.Object

A utility class to convert between integer based bitmaps for (OpenFlow) flags and Enum and EnumSet based representations. The enum used to represent individual flags needs to implement the BitmapableEnum interface. Example: int bitmap = 0x11; // OFPPC_PORT_DOWN | OFPPC_NO_STP EnumSet<OFPortConfig> s = toEnumSet(OFPortConfig.class, bitmap); // s will contain OFPPC_PORT_DOWN and OFPPC_NO_STP EnumSet<OFPortConfig> s = EnumSet.of(OFPPC_NO_STP, OFPPC_PORT_DOWN); int bitmap = toBitmap(s); // returns 0x11

Author:
gregor

Nested Class Summary
static interface EnumBitmaps.BitmapableEnum
          Enums used to represent individual flags needs to implement this interface
 
Constructor Summary
EnumBitmaps()
           
 
Method Summary
static
<E extends java.lang.Enum<E> & EnumBitmaps.BitmapableEnum>
int
getMask(java.lang.Class<E> type)
          Return the bitmap mask with all possible bits set.
static
<E extends java.lang.Enum<E> & EnumBitmaps.BitmapableEnum>
int
toBitmap(java.util.Set<E> set)
          Convert the given EnumSet to the integer bitmap representation
static
<E extends java.lang.Enum<E> & EnumBitmaps.BitmapableEnum>
java.util.EnumSet<E>
toEnumSet(java.lang.Class<E> type, int bitmap)
          Convert an integer bitmap to an EnumSet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EnumBitmaps

public EnumBitmaps()
Method Detail

toEnumSet

public static <E extends java.lang.Enum<E> & EnumBitmaps.BitmapableEnum> java.util.EnumSet<E> toEnumSet(java.lang.Class<E> type,
                                                                                                       int bitmap)
Convert an integer bitmap to an EnumSet. See class description for example

Parameters:
type - The Enum class to use. Must implement BitmapableEnum
bitmap - The integer bitmap
Returns:
A newly allocated EnumSet representing the bits set in the bitmap
Throws:
java.lang.NullPointerException - if type is null
java.lang.IllegalArgumentException - if any enum constant from type has more than one bit set.
java.lang.IllegalArgumentException - if the bitmap has any bits set not represented by an enum constant.

getMask

public static <E extends java.lang.Enum<E> & EnumBitmaps.BitmapableEnum> int getMask(java.lang.Class<E> type)
Return the bitmap mask with all possible bits set. E.g., If a bitmap has the individual flags 0x1, 0x2, and 0x8 (note the missing 0x4) then the mask will be 0xb (1011 binary)

Parameters:
type - The Enum class to use. Must implement BitmapableEnum
Returns:
an integer with all possible bits for the given bitmap enum type set.
Throws:
java.lang.NullPointerException - if type is null
java.lang.IllegalArgumentException - if any enum constant from type has more than one bit set

toBitmap

public static <E extends java.lang.Enum<E> & EnumBitmaps.BitmapableEnum> int toBitmap(java.util.Set<E> set)
Convert the given EnumSet to the integer bitmap representation

Parameters:
set - The EnumSet to convert. The enum must implement BitmapableEnum
Returns:
the integer bitmap
Throws:
java.lang.IllegalArgumentException - if an enum constant from the set (!) has more than one bit set
java.lang.NullPointerException - if the set is null