com.aliasi.util
Class BinaryMap<E>

java.lang.Object
  extended by java.util.AbstractMap<E,Integer>
      extended by com.aliasi.util.BinaryMap<E>
Type Parameters:
E - the type of keys in the map
All Implemented Interfaces:
Serializable, Map<E,Integer>

public class BinaryMap<E>
extends AbstractMap<E,Integer>
implements Serializable

The BinaryMap class implements a map from objects to integer objects where the only value is the integer with value 1. Instances of this class are typically returned by boolean feature extractors, who return a map with only 1 values, because the 0 values are implicit.

Binary maps are based on a set of keys that map to 1. Thus they are more space efficient than Java's utility maps such as tree maps or hash maps. The underlying set implementation is pluggable, but must be mutable if the resulting binary map is to be mutable.

Modifiability through the entry set, key set, and values collection is fully supported through their respective iterators and through the collections themselves. The map entries making up the entry set may not have their values modified.

Equality and Hash Codes

Binary maps satisfy the requirements laid out for hash codes and object equality specified in Map.

Thread Safety

Binary maps have the same thread safety as their underlying sets.

Serialization

A binary map may be serialized if its underlying positive set is serializable.

Since:
LingPipe3.9.1
Version:
3.9.1
Author:
Bob Carpenter
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
static Integer ONE
          The constant used for the Integer with value 1.
 
Constructor Summary
BinaryMap()
          Construct a binary map with an initial capacity of zero.
BinaryMap(int initialCapacity)
          Construct a binary map with the specified initial capacity.
BinaryMap(Set<E> positiveSet)
          Construct a binary map based on the specified set of positive values.
 
Method Summary
 boolean add(E e)
          Adds the specified element to the map with value 1.
 void clear()
          Removes all of the mappings from this map.
 boolean containsKey(Object o)
          Returns true if this mapping contains a mapping for the specified object.
 boolean containsValue(Object o)
          Returns true if this map contains a mapping from some key to the specified value.
 Set<Map.Entry<E,Integer>> entrySet()
          Return a set view of the mappings in this map.
 Integer get(Object key)
          Returns the Integer with value 1 if the specified argument is mapped to 1 by this map, and returns null otherwise.
 boolean isEmpty()
          Returns true if this mapping is empty.
 Set<E> keySet()
          Returns the set of keys mapping to 1.
 Integer put(E e, Integer n)
          Adds the mapping of the specified object to the specified value if the specified number is the Integer with value 1.
 Integer remove(Object key)
          Remove the mapping with the specified key if it is present, returning the previous value, or null if it was previously undefined.
 int size()
          Returns the size of this mapping.
 Collection<Integer> values()
          Returns an immutable collection view of the values for this map.
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, putAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ONE

public static final Integer ONE
The constant used for the Integer with value 1. The value is defined by Integer.valueOf(1), so may be the same object as returned by other calls to valueOf().

Constructor Detail

BinaryMap

public BinaryMap()
Construct a binary map with an initial capacity of zero.

This is a convenience method delegating to BinaryMap(int) with an initial capacity argument of 1; see that constructor's documentation for more information


BinaryMap

public BinaryMap(int initialCapacity)
Construct a binary map with the specified initial capacity.

This is a convenience method delegating to BinaryMap(Set) with a new instance of CompactHashSet of the specified initial capacity. Compact hash sets support the full range of add and remove operations, but does not support null elements.

Parameters:
initialCapacity - Initial size of backing array for the binary map's entries.
Throws:
IllegalArgumentException - If the initial capacity is negative.
OutOfMemoryError - If the JVM cannot allocate an object array with the specified initial capacity.

BinaryMap

public BinaryMap(Set<E> positiveSet)
Construct a binary map based on the specified set of positive values. The set is stored as is rather than copied. The set must be mutable if the constructed map is to be mutable.

Parameters:
positiveSet -
Method Detail

add

public boolean add(E e)
Adds the specified element to the map with value 1.

Note that this method is not part of the Map interface.

Parameters:
e - Element added to the map with value 1.
Returns:
true if the map didn't already contain the element.
Throws:
UnsupportedOperationException - If the underlying set of positive elements does not support Collection.add.

keySet

public Set<E> keySet()
Returns the set of keys mapping to 1. This set is backed by this map, so changes to the set are reflected in the map and vice-versa.

If the underlying set is not modifiable, attempts to modify the key set will raise unsupported operation exceptions.

Note that results are undefined in the middle of an iterator, which will likely throw concurrent modification, null pointer, or array index out of bounds exceptions. Therefore, access to the key set must be synchronized in the same way access to the underlying set.

Further note that unlike the specification in Map, the returned keyset supports the Collection.add(Object) and Collection.addAll(Collection). Adding elements to the key set is the same as adding them through the add(Object) method of this class.

Specified by:
keySet in interface Map<E,Integer>
Overrides:
keySet in class AbstractMap<E,Integer>
Returns:
The set of keys mapping to one.

entrySet

public Set<Map.Entry<E,Integer>> entrySet()
Return a set view of the mappings in this map. This set is backed by this map, so changes to the return set affect this set and vice-versa. The resulting set supports deletions through Iterator.remove, Collection.remove, Collection.removeAll, Collection.retainAll, and Collection.clear operations, but does not support the add operations.

If the underlying set of positive elements does not support these modification operations, they will throw an unsupported operation exception.

Implementation Note: The Map.Entry elements are created as necessary by the entry set using a relatively efficient implementation of entries that only stores the key. Accessing the key set is more efficient.

Specified by:
entrySet in interface Map<E,Integer>
Specified by:
entrySet in class AbstractMap<E,Integer>
Returns:
The set of mappings for this map.

get

public Integer get(Object key)
Returns the Integer with value 1 if the specified argument is mapped to 1 by this map, and returns null otherwise.

The constant ONE is used for the return value.

Specified by:
get in interface Map<E,Integer>
Overrides:
get in class AbstractMap<E,Integer>
Parameters:
key - The element whose value is returned.
Returns:
1 if the element is mapped to 1, and null otherwise.

remove

public Integer remove(Object key)
Remove the mapping with the specified key if it is present, returning the previous value, or null if it was previously undefined.

Specified by:
remove in interface Map<E,Integer>
Overrides:
remove in class AbstractMap<E,Integer>
Parameters:
key - Key of mapping to remove.
Returns:
The value 1 if the key is already present, and null otherwise.
Throws:
UnsupportedOperationException - If the underlying set for this map does not support the Set.remove(Object) operation.

size

public int size()
Returns the size of this mapping.

Specified by:
size in interface Map<E,Integer>
Overrides:
size in class AbstractMap<E,Integer>
Returns:
Size of this mapping.

values

public Collection<Integer> values()
Returns an immutable collection view of the values for this map. The resulting collecton will be empty if the map is entry, or contain the single value ONE if the map is not empty.

Specified by:
values in interface Map<E,Integer>
Overrides:
values in class AbstractMap<E,Integer>

clear

public void clear()
Removes all of the mappings from this map.

The implementation just delegates the clear operation to the contained set.

Specified by:
clear in interface Map<E,Integer>
Overrides:
clear in class AbstractMap<E,Integer>
Throws:
UnsupportedOperationException - If the clear operation is not supported by the contained set.

containsKey

public boolean containsKey(Object o)
Returns true if this mapping contains a mapping for the specified object.

This method delegates to the underlying positive set's Collection.contains method.

Specified by:
containsKey in interface Map<E,Integer>
Overrides:
containsKey in class AbstractMap<E,Integer>
Parameters:
o - Object to teset.
Returns:
true if it is mapped by this mapping.
Throws:
ClassCastException - If the underlying set throws a class cast when checking the specified object for membership.

containsValue

public boolean containsValue(Object o)
Returns true if this map contains a mapping from some key to the specified value.

Note that the only object for which this map may return true is the Integer with value 1.

Specified by:
containsValue in interface Map<E,Integer>
Overrides:
containsValue in class AbstractMap<E,Integer>
Parameters:
o - Object to test.
Returns:
true if this map contains a mapping from some object to this value.

isEmpty

public boolean isEmpty()
Returns true if this mapping is empty.

Specified by:
isEmpty in interface Map<E,Integer>
Overrides:
isEmpty in class AbstractMap<E,Integer>
Returns:
true if this mapping is empty.

put

public Integer put(E e,
                   Integer n)
Adds the mapping of the specified object to the specified value if the specified number is the Integer with value 1.

Specified by:
put in interface Map<E,Integer>
Overrides:
put in class AbstractMap<E,Integer>
Parameters:
e - Key for the mapping.
n - Value for the mapping.
Throws:
IllegalArgumentException - If the specified integer does not have value 1.