LimeWire collection component api

org.limewire.collection
Class FixedsizeHashMap<K,V>

java.lang.Object
  extended by org.limewire.collection.FixedsizeHashMap<K,V>

public class FixedsizeHashMap<K,V>
extends java.lang.Object

Implements a fixed size HashMap. If FixedsizeHashMap gets full, no new entry can be inserted into it, except by removing an entry first. An attempt to add new entry throws a NoMoreStorageException.

    try{

        FixedsizeHashMap<String, String> fhm = new FixedsizeHashMap<String, String>(3);
        fhm.put("myKey1", "Abby");
        fhm.put("myKey2", "Bob");
        fhm.put("myKey3", "Chris");
        System.out.println(fhm);    

        String ret;
        ret = fhm.put("myKey3", "replace");
        if(ret != null)
            System.out.println("put returned: " + ret);
        System.out.println(fhm);    

        fhm.put("myKey4", "Dan");
    } catch (Exception e) {
        System.out.println("Exception because of maximum size upon put myKey4 ... " + e.toString() );
    }   

    Output:
        {myKey2=Bob, myKey3=Chris, myKey1=Abby}
        put returned: Chris
        {myKey2=Bob, myKey3=replace, myKey1=Abby}
        Exception because of maximum size upon put myKey4 ... org.limewire.collection.NoMoreStorageException


Constructor Summary
FixedsizeHashMap(int size)
          Create a new hashMap that stores only the specified number of entries.
 
Method Summary
 void clear()
          Clears all entries from the map.
 V get(K key)
          Returns the value mapped to the given key.
 V put(K key, V value)
          Maps the given key to the given value.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FixedsizeHashMap

public FixedsizeHashMap(int size)
Create a new hashMap that stores only the specified number of entries.

Parameters:
size - the number of entries to hold
Throws:
java.lang.IllegalArgumentException - if size is less < 1.
Method Detail

put

public V put(K key,
             V value)
      throws NoMoreStorageException
Maps the given key to the given value. If adding the key would make this contain more elements than the size given at construction, the passed entry is not stored and NoMoreStorageException gets thrown.

Throws:
NoMoreStorageException - when no more space left in the storage ideally, before calling put method, it should be checked whether the map is already full or not

get

public V get(K key)
Returns the value mapped to the given key.

Parameters:
key - the given key
Returns:
the value given key maps to

clear

public void clear()
Clears all entries from the map.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
the string representation of the mappings

LimeWire collection component api

Copyright © 2009 Lime Wire LLC. All Rights Reserved.