Top Description Fields Constructors Methods
org.apache.commons.lang3.util

public final Class FluentBitSet

extends Object
implements Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable
Imports
java.io.Serializable, java.util.BitSet, .Objects, java.util.stream.IntStream

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

Field Summary

Modifier and TypeField and Description
private final BitSet
bitSet

Working BitSet.

private static final long

Constructor Summary

AccessConstructor and Description
public
FluentBitSet()

Creates a new bit set.

public
FluentBitSet(final BitSet
The bit set to wrap.
set
)

Creates a new instance for the given bit set.

public
FluentBitSet(final int
the initial size of the bit set.
nbits
)

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

Method Summary

Modifier and TypeMethod and Description
public FluentBitSet

Returns:

this instance.
and
(final BitSet
a bit set.
set
)

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

public FluentBitSet

Returns:

this instance.
and
(final FluentBitSet
a bit set.
set
)

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

public FluentBitSet

Returns:

this instance.
andNot
(final BitSet
the BitSet with which to mask this BitSet.
set
)

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

public FluentBitSet

Returns:

this instance.
andNot
(final FluentBitSet
the BitSet with which to mask this BitSet.
set
)

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

public BitSet

Returns:

the wrapped bit set.
bitSet
()

Gets the wrapped bit set.

public int

Returns:

the number of bits set to true in this BitSet.
cardinality
()

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

public FluentBitSet

Returns:

this instance.
clear
()

Sets all of the bits in this BitSet to false.

public FluentBitSet

Returns:

this instance.
clear
(final int...
the index of the bit to be cleared.
bitIndexArray
)

Sets the bits specified by the indexes to false.

public FluentBitSet

Returns:

this instance.
clear
(final int
the index of the bit to be cleared.
bitIndex
)

Sets the bit specified by the index to false.

public FluentBitSet

Returns:

this instance.
clear
(final int
index of the first bit to be cleared.
fromIndex
,
final int
index after the last bit to be cleared.
toIndex
)

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

public Object

Returns:

a clone of this bit set
clone
()

Overrides java.lang.Object.clone.

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

public boolean
equals(final Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public FluentBitSet

Returns:

this instance.
flip
(final int
the index of the bit to flip.
bitIndex
)

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

public FluentBitSet

Returns:

this instance.
flip
(final int
index of the first bit to flip.
fromIndex
,
final int
index after the last bit to flip.
toIndex
)

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

public boolean

Returns:

the value of the bit with the specified index.
get
(final int
the bit index.
bitIndex
)

Gets the value of the bit with the specified index.

public FluentBitSet

Returns:

a new BitSet from a range of this BitSet.
get
(final int
index of the first bit to include.
fromIndex
,
final int
index after the last bit to include.
toIndex
)

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

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

boolean indicating whether this BitSet intersects the specified BitSet.
intersects
(final BitSet
BitSet to intersect with.
set
)

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

public boolean

Returns:

boolean indicating whether this BitSet intersects the specified BitSet.
intersects
(final FluentBitSet
BitSet to intersect with.
set
)

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

public boolean

Returns:

boolean indicating whether this BitSet is empty.
isEmpty
()

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

public int

Returns:

the logical size of this BitSet.
length
()

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

public int

Returns:

the index of the next clear bit.
nextClearBit
(final int
the index to start checking from (inclusive).
fromIndex
)

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

public int

Returns:

the index of the next set bit, or -1 if there is no such bit.
nextSetBit
(final int
the index to start checking from (inclusive).
fromIndex
)

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

public FluentBitSet

Returns:

this instance.
or
(final BitSet
a bit set.
set
)

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

public FluentBitSet

Returns:

this instance.
or
(final FluentBitSet...
a bit set.
set
)

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

public FluentBitSet

Returns:

this instance.
or
(final FluentBitSet
a bit set.
set
)

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

public int

Returns:

the index of the previous clear bit, or -1 if there is no such bit.
previousClearBit
(final int
the index to start checking from (inclusive).
fromIndex
)

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

public int

Returns:

the index of the previous set bit, or -1 if there is no such bit
previousSetBit
(final int
the index to start checking from (inclusive)
fromIndex
)

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

public FluentBitSet

Returns:

this instance.
set
(final int...
a bit index array.
bitIndexArray
)

Sets the bit at the specified indexes to true.

public FluentBitSet

Returns:

this instance.
set
(final int
a bit index
bitIndex
)

Sets the bit at the specified index to true.

public FluentBitSet

Returns:

this instance.
set
(final int
a bit index.
bitIndex
,
final boolean
a boolean value to set.
value
)

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

public FluentBitSet

Returns:

this instance.
set
(final int
index of the first bit to be set.
fromIndex
,
final int
index after the last bit to be set.
toIndex
)

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

public FluentBitSet

Returns:

this instance.
set
(final int
index of the first bit to be set.
fromIndex
,
final int
index after the last bit to be set.
toIndex
,
final boolean
value to set the selected bits to.
value
)

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

public FluentBitSet

Returns:

this instance.
setInclusive
(final int
index of the first bit to be set
fromIndex
,
final int
index of the last bit to be set
toIndex
)

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

public int

Returns:

the number of bits currently in this bit set.
size
()

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

public IntStream

Returns:

a stream of integers representing set indices.
stream
()

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

public byte[]

Returns:

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

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

public long[]

Returns:

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

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

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public FluentBitSet

Returns:

this instance.
xor
(final BitSet
a bit set
set
)

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

public FluentBitSet

Returns:

this instance.
xor
(final FluentBitSet
a bit set
set
)

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

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

bitSetback to summary
private final BitSet bitSet

Working BitSet.

serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

FluentBitSetback to summary
public FluentBitSet()

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

FluentBitSetback to summary
public FluentBitSet(final BitSet set)

Creates a new instance for the given bit set.

Parameters
set:BitSet

The bit set to wrap.

FluentBitSetback to summary
public FluentBitSet(final int nbits)

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:int

the initial size of the bit set.

Exceptions
NegativeArraySizeException:
if the specified initial size is negative.

Method Detail

andback to summary
public FluentBitSet and(final BitSet set)

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:BitSet

a bit set.

Returns:FluentBitSet

this instance.

andback to summary
public FluentBitSet and(final FluentBitSet set)

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:FluentBitSet

a bit set.

Returns:FluentBitSet

this instance.

andNotback to summary
public FluentBitSet andNot(final BitSet set)

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

Parameters
set:BitSet

the BitSet with which to mask this BitSet.

Returns:FluentBitSet

this instance.

andNotback to summary
public FluentBitSet andNot(final FluentBitSet set)

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

Parameters
set:FluentBitSet

the BitSet with which to mask this BitSet.

Returns:FluentBitSet

this instance.

bitSetback to summary
public BitSet bitSet()

Gets the wrapped bit set.

Returns:BitSet

the wrapped bit set.

cardinalityback to summary
public int cardinality()

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

Returns:int

the number of bits set to true in this BitSet.

clearback to summary
public FluentBitSet clear()

Sets all of the bits in this BitSet to false.

Returns:FluentBitSet

this instance.

clearback to summary
public FluentBitSet clear(final int... bitIndexArray)

Sets the bits specified by the indexes to false.

Parameters
bitIndexArray:int[]

the index of the bit to be cleared.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
clearback to summary
public FluentBitSet clear(final int bitIndex)

Sets the bit specified by the index to false.

Parameters
bitIndex:int

the index of the bit to be cleared.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
clearback to summary
public FluentBitSet clear(final int fromIndex, final int toIndex)

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

Parameters
fromIndex:int

index of the first bit to be cleared.

toIndex:int

index after the last bit to be cleared.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

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:Object

a clone of this bit set

Annotations
@Override
See Also
size()
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
flipback to summary
public FluentBitSet flip(final int bitIndex)

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

Parameters
bitIndex:int

the index of the bit to flip.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
flipback to summary
public FluentBitSet flip(final int fromIndex, final int toIndex)

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

Parameters
fromIndex:int

index of the first bit to flip.

toIndex:int

index after the last bit to flip.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
getback to summary
public boolean get(final int bitIndex)

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:int

the bit index.

Returns:boolean

the value of the bit with the specified index.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
getback to summary
public FluentBitSet get(final int fromIndex, final int toIndex)

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

Parameters
fromIndex:int

index of the first bit to include.

toIndex:int

index after the last bit to include.

Returns:FluentBitSet

a new BitSet from a range of this BitSet.

Exceptions
IndexOutOfBoundsException:
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
intersectsback to summary
public boolean intersects(final BitSet set)

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

Parameters
set:BitSet

BitSet to intersect with.

Returns:boolean

boolean indicating whether this BitSet intersects the specified BitSet.

intersectsback to summary
public boolean intersects(final FluentBitSet set)

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

Parameters
set:FluentBitSet

BitSet to intersect with.

Returns:boolean

boolean indicating whether this BitSet intersects the specified BitSet.

isEmptyback to summary
public boolean isEmpty()

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

Returns:boolean

boolean indicating whether this BitSet is empty.

lengthback to summary
public int length()

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:int

the logical size of this BitSet.

nextClearBitback to summary
public int nextClearBit(final int fromIndex)

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

Parameters
fromIndex:int

the index to start checking from (inclusive).

Returns:int

the index of the next clear bit.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
nextSetBitback to summary
public int nextSetBit(final int fromIndex)

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:int

the index to start checking from (inclusive).

Returns:int

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

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
orback to summary
public FluentBitSet or(final BitSet set)

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:BitSet

a bit set.

Returns:FluentBitSet

this instance.

orback to summary
public FluentBitSet or(final FluentBitSet... set)

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:FluentBitSet[]

a bit set.

Returns:FluentBitSet

this instance.

orback to summary
public FluentBitSet or(final FluentBitSet set)

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:FluentBitSet

a bit set.

Returns:FluentBitSet

this instance.

previousClearBitback to summary
public int previousClearBit(final int fromIndex)

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:int

the index to start checking from (inclusive).

Returns:int

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

Exceptions
IndexOutOfBoundsException:
if the specified index is less than -1.
previousSetBitback to summary
public int previousSetBit(final int fromIndex)

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:int

the index to start checking from (inclusive)

Returns:int

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

Exceptions
IndexOutOfBoundsException:
if the specified index is less than -1
setback to summary
public FluentBitSet set(final int... bitIndexArray)

Sets the bit at the specified indexes to true.

Parameters
bitIndexArray:int[]

a bit index array.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
setback to summary
public FluentBitSet set(final int bitIndex)

Sets the bit at the specified index to true.

Parameters
bitIndex:int

a bit index

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative
setback to summary
public FluentBitSet set(final int bitIndex, final boolean value)

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

Parameters
bitIndex:int

a bit index.

value:boolean

a boolean value to set.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if the specified index is negative.
setback to summary
public FluentBitSet set(final int fromIndex, final int toIndex)

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

Parameters
fromIndex:int

index of the first bit to be set.

toIndex:int

index after the last bit to be set.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
setback to summary
public FluentBitSet set(final int fromIndex, final int toIndex, final boolean value)

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

Parameters
fromIndex:int

index of the first bit to be set.

toIndex:int

index after the last bit to be set.

value:boolean

value to set the selected bits to.

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.
setInclusiveback to summary
public FluentBitSet setInclusive(final int fromIndex, final int toIndex)

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

Parameters
fromIndex:int

index of the first bit to be set

toIndex:int

index of the last bit to be set

Returns:FluentBitSet

this instance.

Exceptions
IndexOutOfBoundsException:
if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex
sizeback to summary
public int size()

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:int

the number of bits currently in this bit set.

streamback to summary
public IntStream stream()

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:IntStream

a stream of integers representing set indices.

Since
1.8
toByteArrayback to summary
public byte[] toByteArray()

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:byte[]

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

toLongArrayback to summary
public long[] toLongArray()

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:long[]

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

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
xorback to summary
public FluentBitSet xor(final BitSet set)

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:BitSet

a bit set

Returns:FluentBitSet

this instance.

xorback to summary
public FluentBitSet xor(final FluentBitSet set)

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:FluentBitSet

a bit set

Returns:FluentBitSet

this instance.