TrueZIP Kernel 7.0-rc1

de.schlichtherle.truezip.util
Class BitField<E extends Enum<E>>

java.lang.Object
  extended by de.schlichtherle.truezip.util.BitField<E>
Type Parameters:
E - The type of Enum objects contained in this set.
All Implemented Interfaces:
Serializable, Iterable<E>

@DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class)
@Immutable
public final class BitField<E extends Enum<E>>
extends Object
implements Iterable<E>, Serializable

A type-safe, immutable set of enums which emulates the concept of a bit field, i.e. a set of predefined bits. As an immutable class, it's inherently thread-safe. All modifying methods return a modified clone of this instance.

In general, passing null as a method parameter results in a NullPointerException and methods return non-null.

TODO: Add more modifying methods.

Author:
Christian Schlichtherle
See Also:
Serialized Form

Field Summary
private  EnumSet<E> bits
           
private static long serialVersionUID
           
 
Constructor Summary
private BitField(Class<E> elementType, boolean allOf)
          Constructs a new bit field which contains all or none of the enums of the given element type.
private BitField(E bit)
          Constructs a new bit field which contains the given bit.
private BitField(E bit, E... bits)
          Constructs a new bit field which contains the given bits.
private BitField(EnumSet<E> bits)
          Constructs a new bit field by sharing the given set of enums.
 
Method Summary
static
<E extends Enum<E>>
BitField<E>
allOf(Class<E> elementType)
          Returns a bit field which contains all enums of the given element type.
 BitField<E> and(BitField<E> that)
           
 int cardinality()
          Returns the number of bits set in this field.
 BitField<E> clear(E bit)
          Clears the given bit.
 boolean equals(Object that)
          Returns true if and only if the given object is another BitField and contains the same bits.
 boolean get(E bit)
          Returns true if and only if the given bit is set.
 int hashCode()
          Returns a hash code which is consistent with equals(java.lang.Object).
 boolean is(E bit)
          Equivalent to get(bit).
 boolean isEmpty()
          Returns true if and only if all bits are cleared in this bit field.
 Iterator<E> iterator()
          Returns a read-only iterator for the bits in this field.
static
<E extends Enum<E>>
BitField<E>
noneOf(Class<E> elementType)
          Returns a bit field which can contain the given element type and is initially empty.
 BitField<E> not()
           
static
<E extends Enum<E>>
BitField<E>
of(Class<E> elementType, String list)
           
static
<E extends Enum<E>>
BitField<E>
of(E bit)
          Returns a bit field which contains the given bit.
static
<E extends Enum<E>>
BitField<E>
of(E bit, E... bits)
          Returns a bit field which contains the given bits.
static
<E extends Enum<E>>
BitField<E>
of(EnumSet<E> bits)
          Returns a bit field which contains the same bits as the given set of enums.
 BitField<E> or(BitField<E> that)
           
 BitField<E> set(E bit)
          Sets the given bit.
 BitField<E> set(E bit, boolean set)
          Sets or clears the given bit.
 EnumSet<E> toEnumSet()
          Returns a new set of enums containing the same bits as this instance.
 String toString()
          Returns a concatenation of the names of the bits in this field, separated by "|".
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

bits

private final EnumSet<E extends Enum<E>> bits
Constructor Detail

BitField

private BitField(Class<E> elementType,
                 boolean allOf)
Constructs a new bit field which contains all or none of the enums of the given element type.


BitField

private BitField(E bit)
Constructs a new bit field which contains the given bit.


BitField

private BitField(E bit,
                 E... bits)
Constructs a new bit field which contains the given bits.


BitField

private BitField(EnumSet<E> bits)
Constructs a new bit field by sharing the given set of enums. Note that this constructor does NOT make a protective copy - use with care!

Parameters:
bits - the set of enums to share with this instance.
Method Detail

noneOf

public static <E extends Enum<E>> BitField<E> noneOf(Class<E> elementType)
Returns a bit field which can contain the given element type and is initially empty.

This could be used like this:

BitField<Option> field = BitField.noneOf(Option.class);
 
where Option is an arbitrary enum type.


allOf

public static <E extends Enum<E>> BitField<E> allOf(Class<E> elementType)
Returns a bit field which contains all enums of the given element type.

This could be used like this:

BitField<Option> field = BitField.allOf(Option.class);
 
where Option is an arbitrary enum type.


of

public static <E extends Enum<E>> BitField<E> of(E bit)
Returns a bit field which contains the given bit.

This could be used like this:

BitField<Option> field = BitField.of(Option.ONE);
 
where Option.ONE is an arbitrary enum.


of

public static <E extends Enum<E>> BitField<E> of(E bit,
                                                 E... bits)
Returns a bit field which contains the given bits.

This could be used like this:

BitField<Option> field = BitField.of(Option.ONE, Option.TWO);
 
where Option.ONE and Option.TWO are arbitrary enums.


of

public static <E extends Enum<E>> BitField<E> of(EnumSet<E> bits)
Returns a bit field which contains the same bits as the given set of enums.

This could be used like this:

BitField<Option> field = BitField.of(bits);
 
where bits is an EnumSet<Option>.


of

public static <E extends Enum<E>> BitField<E> of(Class<E> elementType,
                                                 String list)

isEmpty

public boolean isEmpty()
Returns true if and only if all bits are cleared in this bit field.


cardinality

public int cardinality()
Returns the number of bits set in this field.


get

public boolean get(E bit)
Returns true if and only if the given bit is set.

Parameters:
bit - The bit to test.

is

public boolean is(E bit)
Equivalent to get(bit).


set

public BitField<E> set(E bit,
                       boolean set)
Sets or clears the given bit.

Subclasses could override this method in order to cache frequently used results, such as a null bit field, a single bit field etc.

Parameters:
bit - The bit to set or clear.
set - Whether the bit shall get set or cleared.

set

public BitField<E> set(E bit)
Sets the given bit.


clear

public BitField<E> clear(E bit)
Clears the given bit.


not

public BitField<E> not()

and

public BitField<E> and(BitField<E> that)

or

public BitField<E> or(BitField<E> that)

iterator

public Iterator<E> iterator()
Returns a read-only iterator for the bits in this field.

Specified by:
iterator in interface Iterable<E extends Enum<E>>

toEnumSet

public EnumSet<E> toEnumSet()
Returns a new set of enums containing the same bits as this instance. The following boolean expression is always true for any non-null bit field bits: bits.equals(BitField.of(bits.toEnumSet())).

Returns:
a new set of enums containing the same bits as this instance.

toString

public String toString()
Returns a concatenation of the names of the bits in this field, separated by "|".

Overrides:
toString in class Object

equals

public boolean equals(@CheckForNull
                      Object that)
Returns true if and only if the given object is another BitField and contains the same bits.

Overrides:
equals in class Object

hashCode

public int hashCode()
Returns a hash code which is consistent with equals(java.lang.Object).

Overrides:
hashCode in class Object

TrueZIP Kernel 7.0-rc1

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.