On this page

FluentBitSet

public final class org.apache.commons.lang3.util.FluentBitSet implements Cloneable, Serializable[source]

A fluent BitSet with additional operations.

Originally from Apache Commons VFS with more added to act as a fluent replacement for java.util.BitSet.

Since:

3.13.0

Constructors

FluentBitSet()

Creates a new bit set.

FluentBitSet(int)

Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1.

FluentBitSet(BitSet)

Creates a new instance for the given bit set.

public FluentBitSet()[source]

Creates a new bit set. All bits are initially false.

public FluentBitSet(int nbits)[source]

Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1. All bits are initially false.

Parameters:

nbits – The initial size of the bit set.

Throws:

java.lang.NegativeArraySizeException – Thrown if the specified initial size is negative.

public FluentBitSet(BitSet set)[source]

Creates a new instance for the given bit set.

Parameters:

set – The bit set to wrap.

Methods

and(BitSet)

Performs a logical AND of this target bit set with the argument bit set.

and(FluentBitSet)

Performs a logical AND of this target bit set with the argument bit set.

andNot(BitSet)

Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.

andNot(FluentBitSet)

Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.

bitSet()

Gets the wrapped bit set.

cardinality()

Returns the number of bits set to true in this BitSet.

clear()

Sets all of the bits in this BitSet to false.

clear(int)

Sets the bit specified by the index to false.

clear(int, int)

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.

clear(int[])

Sets the bits specified by the indexes to false.

clone()

Cloning this BitSet produces a new BitSet that is equal to it.

equals(Object)

flip(int)

Sets the bit at the specified index to the complement of its current value.

flip(int, int)

Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.

get(int)

Gets the value of the bit with the specified index.

get(int, int)

Gets a new BitSet composed of bits from this BitSet from fromIndex (inclusive) to toIndex (exclusive).

hashCode()

intersects(BitSet)

Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.

intersects(FluentBitSet)

Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.

isEmpty()

Tests whether if this BitSet contains no bits that are set to true.

length()

Returns the “logical size” of this BitSet: the index of the highest set bit in the BitSet plus one.

nextClearBit(int)

Returns the index of the first bit that is set to false that occurs on or after the specified starting index.

nextSetBit(int)

Returns the index of the first bit that is set to true that occurs on or after the specified starting index.

or(BitSet)

Performs a logical OR of this bit set with the bit set argument.

or(FluentBitSet)

Performs a logical OR of this bit set with the bit set argument.

or(FluentBitSet[])

Performs a logical OR of this bit set with the bit set arguments.

previousClearBit(int)

Returns the index of the nearest bit that is set to false that occurs on or before the specified starting index.

previousSetBit(int)

Returns the index of the nearest bit that is set to true that occurs on or before the specified starting index.

set(int)

Sets the bit at the specified index to true.

set(int, boolean)

Sets the bit at the specified index to the specified value.

set(int, int)

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.

set(int, int, boolean)

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value.

set(int[])

Sets the bit at the specified indexes to true.

setInclusive(int, int)

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (inclusive) to true.

size()

Returns the number of bits of space actually in use by this BitSet to represent bit values.

stream()

Returns a stream of indices for which this BitSet contains a bit in the set state.

toByteArray()

Returns a new byte array containing all the bits in this bit set.

toLongArray()

Returns a new byte array containing all the bits in this bit set.

toString()

xor(BitSet)

Performs a logical XOR of this bit set with the bit set argument.

xor(FluentBitSet)

Performs a logical XOR of this bit set with the bit set argument.

public FluentBitSet and(BitSet set)[source]

Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.

Parameters:

set – A bit set.

Returns:

this instance.

public FluentBitSet and(FluentBitSet set)[source]

Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.

Parameters:

set – A bit set.

Returns:

this instance.

public FluentBitSet andNot(BitSet set)[source]

Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.

Parameters:

set – The BitSet with which to mask this BitSet.

Returns:

this instance.

public FluentBitSet andNot(FluentBitSet set)[source]

Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.

Parameters:

set – The BitSet with which to mask this BitSet.

Returns:

this instance.

public BitSet bitSet()[source]

Gets the wrapped bit set.

Returns:

The wrapped bit set.

public int cardinality()[source]

Returns the number of bits set to true in this BitSet.

Returns:

The number of bits set to true in this BitSet.

public FluentBitSet clear()[source]

Sets all of the bits in this BitSet to false.

Returns:

this instance.

public FluentBitSet clear(int bitIndex)[source]

Sets the bit specified by the index to false.

Parameters:

bitIndex – The index of the bit to be cleared.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

Returns:

this instance.

public FluentBitSet clear(int fromIndex, int toIndex)[source]

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to false.

Parameters:
  • fromIndex – index of the first bit to be cleared.

  • toIndex – index after the last bit to be cleared.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Returns:

this instance.

public FluentBitSet clear(int... bitIndexArray)[source]

Sets the bits specified by the indexes to false.

Parameters:

bitIndexArray – The index of the bit to be cleared.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

Returns:

this instance.

@Override public Object clone()[source]

Cloning this BitSet produces a new BitSet that is equal to it. The clone of the bit set is another bit set that has exactly the same bits set to true as this bit set.

Returns:

A clone of this bit set

See also:

#size()

@Override public boolean equals(Object obj)[source]
public FluentBitSet flip(int bitIndex)[source]

Sets the bit at the specified index to the complement of its current value.

Parameters:

bitIndex – The index of the bit to flip.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

Returns:

this instance.

public FluentBitSet flip(int fromIndex, int toIndex)[source]

Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value.

Parameters:
  • fromIndex – index of the first bit to flip.

  • toIndex – index after the last bit to flip.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Returns:

this instance.

public boolean get(int bitIndex)[source]

Gets the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this BitSet; otherwise, the result is false.

Parameters:

bitIndex – The bit index.

Returns:

The value of the bit with the specified index.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

public FluentBitSet get(int fromIndex, int toIndex)[source]

Gets a new BitSet composed of bits from this BitSet from fromIndex (inclusive) to toIndex (exclusive).

Parameters:
  • fromIndex – index of the first bit to include.

  • toIndex – index after the last bit to include.

Returns:

A new BitSet from a range of this BitSet.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

@Override public int hashCode()[source]
public boolean intersects(BitSet set)[source]

Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.

Parameters:

set – BitSet to intersect with.

Returns:

boolean indicating whether this BitSet intersects the specified BitSet.

public boolean intersects(FluentBitSet set)[source]

Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.

Parameters:

set – BitSet to intersect with.

Returns:

boolean indicating whether this BitSet intersects the specified BitSet.

public boolean isEmpty()[source]

Tests whether if this BitSet contains no bits that are set to true.

Returns:

boolean indicating whether this BitSet is empty.

public int length()[source]

Returns the “logical size” of this BitSet: the index of the highest set bit in the BitSet plus one. Returns zero if the BitSet contains no set bits.

Returns:

The logical size of this BitSet.

public int nextClearBit(int fromIndex)[source]

Returns the index of the first bit that is set to false that occurs on or after the specified starting index.

Parameters:

fromIndex – The index to start checking from (inclusive).

Returns:

The index of the next clear bit.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

public int nextSetBit(int fromIndex)[source]

Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit exists then -1 is returned.

To iterate over the true bits in a BitSet, use the following loop:

for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
    // operate on index i here
    if (i == Integer.MAX_VALUE) {
        break; // or (i+1) would overflow
    }
}
Parameters:

fromIndex – The index to start checking from (inclusive).

Returns:

The index of the next set bit, or -1 if there is no such bit.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

public FluentBitSet or(BitSet set)[source]

Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true.

Parameters:

set – A bit set.

Returns:

this instance.

public FluentBitSet or(FluentBitSet set)[source]

Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true.

Parameters:

set – A bit set.

Returns:

this instance.

public FluentBitSet or(FluentBitSet... set)[source]

Performs a logical OR of this bit set with the bit set arguments. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true.

Parameters:

set – A bit set.

Returns:

this instance.

public int previousClearBit(int fromIndex)[source]

Returns the index of the nearest bit that is set to false that occurs on or before the specified starting index. If no such bit exists, or if -1 is given as the starting index, then -1 is returned.

Parameters:

fromIndex – The index to start checking from (inclusive).

Returns:

The index of the previous clear bit, or -1 if there is no such bit.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is less than -1.

public int previousSetBit(int fromIndex)[source]

Returns the index of the nearest bit that is set to true that occurs on or before the specified starting index. If no such bit exists, or if -1 is given as the starting index, then -1 is returned.

To iterate over the true bits in a BitSet, use the following loop:

 
for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
    // operate on index i here
}
Parameters:

fromIndex – The index to start checking from (inclusive)

Returns:

The index of the previous set bit, or -1 if there is no such bit

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is less than -1.

public FluentBitSet set(int bitIndex)[source]

Sets the bit at the specified index to true.

Parameters:

bitIndex – A bit index

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

Returns:

this instance.

public FluentBitSet set(int bitIndex, boolean value)[source]

Sets the bit at the specified index to the specified value.

Parameters:
  • bitIndex – A bit index.

  • value – A boolean value to set.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

Returns:

this instance.

public FluentBitSet set(int fromIndex, int toIndex)[source]

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.

Parameters:
  • fromIndex – index of the first bit to be set.

  • toIndex – index after the last bit to be set.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Returns:

this instance.

public FluentBitSet set(int fromIndex, int toIndex, boolean value)[source]

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value.

Parameters:
  • fromIndex – index of the first bit to be set.

  • toIndex – index after the last bit to be set.

  • value – value to set the selected bits to.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Returns:

this instance.

public FluentBitSet set(int... bitIndexArray)[source]

Sets the bit at the specified indexes to true.

Parameters:

bitIndexArray – A bit index array.

Throws:

java.lang.IndexOutOfBoundsException – Thrown if the specified index is negative.

Returns:

this instance.

public FluentBitSet setInclusive(int fromIndex, int toIndex)[source]

Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (inclusive) to true.

Parameters:
  • fromIndex – index of the first bit to be set

  • toIndex – index of the last bit to be set

Throws:

java.lang.IndexOutOfBoundsException – Thrown if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Returns:

this instance.

public int size()[source]

Returns the number of bits of space actually in use by this BitSet to represent bit values. The maximum element in the set is the size - 1st element.

Returns:

The number of bits currently in this bit set.

public IntStream stream()[source]

Returns a stream of indices for which this BitSet contains a bit in the set state. The indices are returned in order, from lowest to highest. The size of the stream is the number of bits in the set state, equal to the value returned by the #cardinality() method.

The bit set must remain constant during the execution of the terminal stream operation. Otherwise, the result of the terminal stream operation is undefined.

Returns:

A stream of integers representing set indices.

Since:

1.8

public byte[] toByteArray()[source]

Returns a new byte array containing all the bits in this bit set.

More precisely, if:

  1. byte[] bytes = s.toByteArray();

  2. then bytes.length == (s.length()+7)/8 and

  3. s.get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)

  4. for all n < 8 * bytes.length.

Returns:

A byte array containing a little-endian representation of all the bits in this bit set

public long[] toLongArray()[source]

Returns a new byte array containing all the bits in this bit set.

More precisely, if:

  1. long[] longs = s.toLongArray();

  2. then longs.length == (s.length()+63)/64 and

  3. s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)

  4. for all n < 64 * longs.length.

Returns:

A byte array containing a little-endian representation of all the bits in this bit set

@Override public String toString()[source]
public FluentBitSet xor(BitSet set)[source]

Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:

  • The bit initially has the value true, and the corresponding bit in the argument has the value false.

  • The bit initially has the value false, and the corresponding bit in the argument has the value true.

Parameters:

set – A bit set

Returns:

this instance.

public FluentBitSet xor(FluentBitSet set)[source]

Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:

  • The bit initially has the value true, and the corresponding bit in the argument has the value false.

  • The bit initially has the value false, and the corresponding bit in the argument has the value true.

Parameters:

set – A bit set

Returns:

this instance.