FileDocCategorySizeDatePackage
IntToIntMapper.javaAPI DocphoneME MR2 API (J2ME)7864Wed May 02 18:00:12 BST 2007com.sun.midp.rms

IntToIntMapper

public class IntToIntMapper extends Object
This class implements a mapping int --> int. (Analogs: vector, hash table, etc.) The implementation uses two arrays: one for keys and one for values. The methods are analogous to those of the java.util.Vector class.

Fields Summary
protected int[]
elementData
The array buffer into which the values to be returned are stored. The capacity of the mapper is the length of this array buffer.
protected int[]
elementKey
The array buffer into which the values used as keys are stored. The capacity of the mapper is the length of this array buffer.
protected int
elementCount
The number of valid components in the mapper.
protected int
capacityIncrement
The amount by which the capacity is automatically incremented when its size becomes greater than its capacity. If the capacity increment is 0, the capacity is doubled each time it needs to grow.
public int
defaultValue
The value returned if no value is associated with the key searched for.
Constructors Summary
public IntToIntMapper(int initialCapacity, int defaultElement, int capacityIncrement)
Constructs an empty mapper with the specified initial capacity and capacity increment.

param
initialCapacity the initial capacity of the mapper.
param
defaultElement the value that gets returned for keys that are not there
param
capacityIncrement the amount by which the capacity is increased when the mapper overflows. (0 means "to be doubled")
exception
IllegalArgumentException if the specified initial capacity is negative

        super();
        if (initialCapacity < 0) {
            throw new IllegalArgumentException();
        }
        this.elementData = new int[initialCapacity];
        this.elementKey = new int[initialCapacity];
        this.capacityIncrement = capacityIncrement;
        this.defaultValue = defaultElement;
    
Methods Summary
private voidaddElement(int obj, int key)
Adds the specified component to the end of this mapper, increasing its size by one. The capacity of this mapper is increased if its size becomes greater than its capacity.

param
obj the component to be added.
param
key the key for that component.

        int newcount = elementCount + 1;
        if (newcount > elementData.length) {
            ensureCapacityHelper(newcount);
        }
        elementKey[elementCount] = key;
        elementData[elementCount++] = obj;
    
public synchronized intelementAt(int key)
Returns the component at the specified key.

param
key a key identifying an object in the mapper
return
the component at the specified key, or the default value if an invalid key was given.

        for (int i = 0; i < elementCount; i++) {
            if (key == elementKey[i]) {
                return elementData[i];
            }
        }
        return defaultValue;
    
private voidensureCapacityHelper(int minCapacity)
This implements the unsynchronized semantics of ensureCapacity. Synchronized methods in this class can internally call this method for ensuring capacity without incurring the cost of an extra synchronization. This function increases the size of the mapper according to the value of capacityIncrement, and makes sure that the new size is not less than minCapacity.

param
minCapacity the desired minimum capacity.

        int oldCapacity = elementData.length;
        int oldData[] = elementData;
        int oldKey[] = elementKey;
        int newCapacity = (capacityIncrement > 0) ?
            (oldCapacity + capacityIncrement) : (oldCapacity * 2);
        if (newCapacity < minCapacity) {
            newCapacity = minCapacity;
        }

        elementData = new int[newCapacity];
        elementKey = new int[newCapacity];
        System.arraycopy(oldData, 0, elementData, 0, elementCount);
        System.arraycopy(oldKey, 0, elementKey, 0, elementCount);
    
public booleanisEmpty()
Tests if this mapper has no components.

return
true if this mapper has no components; false otherwise.

        return elementCount == 0;
    
public synchronized voidremoveElementAt(int key)
Deletes the component at the specified key. Nothing happens if no component has been associated with the key.

param
key the key of the object to remove.

        final int nowhere = -1;
        int where = nowhere;
        for (int i = 0; i < elementCount; i++)
        {
            if (key == elementKey[i]) {
                where = i;
                break;
            }
        }
        if (where == nowhere) {
            return;
        }

        // breaking the order :(
        if (where < elementCount--)
        {
            elementKey[where] = elementKey[elementCount];
            elementData[where] = elementData[elementCount];
        }
    
public synchronized voidsetElementAt(int obj, int key)
Sets the component at the specified key of this mapper to be the specified value. The previous component at that position is discarded.

param
obj what the component is to be set to.
param
key the key for that component.

        for (int i = 0; i < elementCount; i++)
            if (key == elementKey[i]) {
                elementData[i] = obj;
                return;
        }
        addElement(obj, key);
    
public intsize()
Returns the number of components in this mapper.

return
the number of components in this mapper.

        return elementCount;