DESY ACOP Beans Home

com.cosylab.util
Class NameValueList

java.lang.Object
  extended by com.cosylab.util.NameValueList
All Implemented Interfaces:
java.util.Map

public class NameValueList
extends java.lang.Object
implements java.util.Map

A simple name-value list. Names are strings and values any Java objects (where primitives are packed in their respective encapsulation objects). Names and values are stored in arrays. The initial dimension of the array can be specified or a default can be used ( DEFAULT_CAPACITY = 10 elements). If more elements are added, the arrays grow in DEFAULT_CAPACITY increments. The addition is therefore a fast operation, if the arrays are not resized. The removal, on the other hand, resizes the arrays each time and is a much slower operation. Lookup of value by name uses linear search and string comparison. This object does not allow duplicate name entries. If a duplicate name is provided, the old value will be overwritten and returned as a return value from add method.

This object is thread safe.

The primary purpose of this object is to store small (10 or less) name-value pairs. In this case, the algorithms (including the linear search) are quite effective. An example would be the use for passing the argument values to the method calls.

This class does preserve the order during insertion and deletion operations.

This class implements the collection Map interface.

Version:
$id$
Author:
Gasper Tkacik

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
NameValueList()
          Creates a new instance of this object with the DEFAULT_CAPACITY equal to 10.
NameValueList(int initialCapacity)
          Creates a new instance of this object with the initial capacity specified by the user.
 
Method Summary
 void clear()
          Sets all elements to null, resets the current pointer to the initial position thereby effectively removing all elements from this map.
 boolean containsKey(java.lang.Object name)
          Returns true if this object contains the name-value mapping with the speficied name.
 boolean containsValue(java.lang.Object value)
          Returns true if this map contains a given value.
 java.util.Set entrySet()
          Returns the set of entries, as described in java.util.Map.Entry.
 java.lang.Object get(java.lang.Object name)
          Returns an object stored under name.
 java.lang.String[] getNames()
          Returns an array of names stored in this data structure.
 boolean isEmpty()
          Returns true iff this map does not contain any name-value pairs.
 java.util.Set keySet()
          Returns a set of all names.
 java.lang.Object put(java.lang.Object name, java.lang.Object value)
          Adds a new name-value pair.
 void putAll(java.util.Map t)
          Puts all nave-value pairs from provided Map to this Map.
 java.lang.Object remove(java.lang.Object name)
          Removes a value stored under name and returns it.
 int size()
          Returns the actual number of entries stored in this data structure.
 java.lang.String toString()
          Lists all objects in this list by calling their toString() methods.
 java.util.Collection values()
          Returns a collection of all values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

NameValueList

public NameValueList()
Creates a new instance of this object with the DEFAULT_CAPACITY equal to 10.


NameValueList

public NameValueList(int initialCapacity)
Creates a new instance of this object with the initial capacity specified by the user.

Parameters:
initialCapacity - the size of the names and values array at construction
Method Detail

size

public int size()
Returns the actual number of entries stored in this data structure.

Specified by:
size in interface java.util.Map
Returns:
int the size of the name-value list

put

public java.lang.Object put(java.lang.Object name,
                            java.lang.Object value)
Adds a new name-value pair. If the name already exists in the structure, its value is replaced by the new value and the old value is returned as a result of this method.

Specified by:
put in interface java.util.Map
Parameters:
name - the name to store the value under
value - a new value for the name
Returns:
Object the value that was previously stored under name
Throws:
java.lang.ClassCastException - if parameter name is not of type String

remove

public java.lang.Object remove(java.lang.Object name)
Removes a value stored under name and returns it. This method resizes the arrays by decreasing them by 1.

Specified by:
remove in interface java.util.Map
Parameters:
name - the name of the value to remove
Returns:
Object the removed value or null if the value with the given name did not exist in the structure
Throws:
java.lang.ClassCastException - if parameter name is not of type String

get

public java.lang.Object get(java.lang.Object name)
Returns an object stored under name.

Specified by:
get in interface java.util.Map
Parameters:
name - the name to lookup
Returns:
Object the object with name or null if such object does not exist
Throws:
java.lang.ClassCastException - if parameter name is not of type String

getNames

public java.lang.String[] getNames()
Returns an array of names stored in this data structure. This involves creating a new string array, which may be modified by the user. All values in this array are guaranteed to be non-null.

Returns:
String[] an array of names of this list

clear

public void clear()
Sets all elements to null, resets the current pointer to the initial position thereby effectively removing all elements from this map.

Specified by:
clear in interface java.util.Map
See Also:
Map.clear()

containsKey

public boolean containsKey(java.lang.Object name)
Returns true if this object contains the name-value mapping with the speficied name.

Specified by:
containsKey in interface java.util.Map
Parameters:
name - the name to lookup
Returns:
boolean true iff the mapping with the given name exists
Throws:
java.lang.NullPointerException - if parameter name is null
See Also:
Map.containsKey(Object)

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map contains a given value. This is determined as in java.util.Map specifications.

Specified by:
containsValue in interface java.util.Map
Parameters:
value - test if this map contains the value
Returns:
true iff the test succeeds
See Also:
Map.containsValue(Object)

entrySet

public java.util.Set entrySet()
Returns the set of entries, as described in java.util.Map.Entry.

Specified by:
entrySet in interface java.util.Map
Returns:
Set a set of all entries
See Also:
Map.entrySet()

isEmpty

public boolean isEmpty()
Returns true iff this map does not contain any name-value pairs.

Specified by:
isEmpty in interface java.util.Map
Returns:
boolean true iff empty
See Also:
Map.isEmpty()

keySet

public java.util.Set keySet()
Returns a set of all names.

Specified by:
keySet in interface java.util.Map
Returns:
Set a set of all names
See Also:
Map.keySet()

putAll

public void putAll(java.util.Map t)
Puts all nave-value pairs from provided Map to this Map.

Specified by:
putAll in interface java.util.Map
Parameters:
t - a map argument with which to perform the union operation
See Also:
Map.putAll(Map)

values

public java.util.Collection values()
Returns a collection of all values.

Specified by:
values in interface java.util.Map
Returns:
a collection of all values
See Also:
Map.values()

toString

public java.lang.String toString()
Lists all objects in this list by calling their toString() methods.

Overrides:
toString in class java.lang.Object
Returns:
String the contents of this list


Copyright © 2010. All Rights Reserved.