net.java.truecommons.shed.BitField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truecommons-shed Show documentation
Show all versions of truecommons-shed Show documentation
Hosts small but useful tools and utilities that didn't fit anywhere else.
/*
* Copyright (C) 2005-2012 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truecommons.shed;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
/**
* 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.
*
* @param The type of {@link Enum} objects contained in this set.
* @author Christian Schlichtherle
*/
// TODO: Add more modifying methods.
@Immutable
public final class BitField>
implements Iterable, Serializable {
private static final long serialVersionUID = 3203876204846746524L;
private final EnumSet bits;
/**
* Returns a bit field which can contain the given element type and is
* initially empty.
*
* This could be used like this:
*
{@code
* BitField
* where {@code Option} is an arbitrary enum type.
*/
public static > BitField
noneOf(Class elementType) {
return new BitField<>(elementType, false);
}
/**
* Returns a bit field which contains all enums of the given element type.
*
* This could be used like this:
*
{@code
* BitField
* where {@code Option} is an arbitrary enum type.
*/
public static > BitField
allOf(Class elementType) {
return new BitField<>(elementType, true);
}
/**
* Returns a bit field which contains the given bit.
*
* This could be used like this:
*
{@code
* BitField
* where {@code Option.ONE} is an arbitrary enum.
*/
public static > BitField
of(E bit) {
return new BitField<>(bit);
}
/**
* Returns a bit field which contains the given bits.
*
* This could be used like this:
*
{@code
* BitField
* where {@code Option.ONE} and {@code Option.TWO} are arbitrary enums.
*/
public static > BitField
of(E bit, E... bits) {
return new BitField<>(bit, bits);
}
/**
* Returns a bit field which contains the same bits as the given collection
* of enums.
*
* This could be used like this:
*
{@code
* BitField
* where {@code bits} is an {@code EnumSet