FileDocCategorySizeDatePackage
NameValueList.javaAPI DocphoneME MR2 API (J2ME)9413Wed May 02 18:00:42 BST 2007gov.nist.core

NameValueList

public class NameValueList extends GenericObject
Implements a simple NameValue association with a quick lookup.
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
private Hashtable
nvList
Internal name value list.
private String
separator
Separator character.
Constructors Summary
public NameValueList(String listName)
Constructs a new list.

param
listName label for this list

        nvList = new Hashtable();
        this.separator = Separators.SEMICOLON;
    
public NameValueList()
Default constructor.

        nvList = new Hashtable();
	this.separator = Separators.SEMICOLON;
    
Methods Summary
public voidadd(NameValue nv)
Adds a name value pair to the list.

param
nv the data to be stored

        if (nv == null)
            throw new NullPointerException("null nv");
        nvList.put(nv.getName(), nv.getValue());
    
public voidadd(java.lang.String name, java.lang.Object obj)
Adds a name value record to this list.

param
name the label for the data element
param
obj the value for the data element

        if (name == null)
            throw new NullPointerException("name in null ! ");
        NameValue nv = new NameValue(name, obj);
        add(nv);
    
public java.lang.Objectclone()
Makes a copy of the current instance.

return
copy of current object

        NameValueList retval = new NameValueList();
        retval.separator = this.separator;
	Enumeration enumNames = nvList.keys();
        String currKey;
	while (enumNames.hasMoreElements()) {
            currKey = (String)enumNames.nextElement();
	    retval.add(currKey, nvList.get(currKey));
	}
        return retval;

    
public booleandelete(java.lang.String name)
Removes the element corresponding to this name.

param
name the label to find
return
true if successfully removed
since
1.0

        if (name == null) {
            return true;
        }

        String name1  = name.toLowerCase();
	nvList.remove(name1);

        return true;
    
public java.lang.Stringencode()
Encodes the contenst as a string.

return
the encoded text string.

        if (nvList.size() == 0)
            return "";

        StringBuffer encoding = new StringBuffer();
	Enumeration enumNames = nvList.keys();
        String currKey;
	Object currValue;

	while (enumNames.hasMoreElements()) {
            currKey = (String)enumNames.nextElement();
	    encoding.append(currKey);
	    currValue = nvList.get(currKey);

	    if (currValue != null) {
                if (currValue instanceof GenericObject) {
                    GenericObject gobj = (GenericObject) currValue;
                    encoding.append(Separators.EQUALS + gobj.encode());
                } else {
                    String s = currValue.toString();

                    if (s.length() > 0) {
                        encoding.append(Separators.EQUALS + s);
                    }
                }
	    }

	    if (enumNames.hasMoreElements()) // not last
	        encoding.append(separator);
	}

        return encoding.toString();
    
public java.lang.StringencodeWithSep()
Encodes the contenst as a string followd by separator.

return
the encoded text string.

        String retVal = encode();
        if (retVal.length() > 0) {
            retVal = separator + retVal;
        }
        return retVal; 
    
public booleanequals(java.lang.Object otherObject)
Compares if two NameValue lists are equal.

param
otherObject is the object to compare to.
return
true if the two objects compare for equality.

        if (!otherObject.getClass().equals
                (this.getClass())) {
            return false;
        }
        NameValueList other = (NameValueList) otherObject;

        if (this.nvList.size() != other.nvList.size()) {
            return false;
        }
	Enumeration enumNames = nvList.keys();
        String currKey;
	Object currValue, currValueOther;
	while (enumNames.hasMoreElements()) {
	    currKey = (String)enumNames.nextElement();
	    currValue = this.nvList.get(currKey);
	    currValueOther = other.nvList.get(currKey);
	    if (
	        (currValueOther == null) || !currValue.equals(currValueOther)) {
                return false;
	    }
	}
        return true;
    
public java.util.EnumerationgetKeys()
Gets the enumeration of key names.

return
enumeration of key names.

        return nvList.keys();
    
public NameValuegetNameValue(java.lang.String name)
Gets the NameValue record given a name.

param
name the data element laebl to find
return
the name value found or null if not found
since
1.0

        if (name == null)
            throw new NullPointerException("null arg!");
        String name1  = name.toLowerCase();
        NameValue returnValue = null;
	Object value = getValue(name1);
	if (value != null)
	    returnValue = new NameValue(name1, value);
        return returnValue;
    
public java.util.VectorgetNames()
Gets a list of key names.

return
list of key names.

        Vector names = new Vector();
	Enumeration enumNames = nvList.keys();

	while (enumNames.hasMoreElements()) {
	    names.addElement(enumNames.nextElement());
	}

        return names;
    
public java.lang.StringgetParameter(java.lang.String name)
Gets the parameter as a String.

param
name the label to find
return
the parameter as a string.

        Object val = this.getValue(name);
        if (val == null)
            return null;
        if (val instanceof GenericObject)
            return ((GenericObject)val).encode();
        else return val.toString();
    
public java.lang.ObjectgetValue(java.lang.String name)
Do a lookup on a given name and return value associated with it.

param
name to be looked up
return
the object that was found or null if not found

        return nvList.get(name.toLowerCase());
    
public java.lang.StringgetValueDefault(java.lang.String name, java.lang.String nameDefault)
Do a lookup on a given name and return value associated with it.

param
name to be looked up
param
nameDefault to be returned when name is not found
return
the object that was found or default if not found

        String returnValue = (String)nvList.get(name.toLowerCase());
	if (returnValue == null) returnValue = nameDefault;
        return returnValue;
    
public booleanhasNameValue(java.lang.String name)
Returns a boolean telling if this NameValueList has a record with this name.

param
name the label to find
return
true if the element includes a value
since
1.0

        return nvList.containsKey(name.toLowerCase());
    
public booleanisEmpty()
Checks if the listis empty.

return
true if the size of the list is zero

        return this.nvList.size() == 0;
    
public voidset(NameValue nv)
Sets a namevalue object in this list.

param
nv the data to be updated

        this.add(nv);
    
public voidset(java.lang.String name, java.lang.Object value)
Sets a namevalue object in this list.

param
name the label for the data element
param
value the value for the element

        NameValue nv = new NameValue(name, value);
        this.set(nv);
    
public voidsetSeparator(java.lang.String separator)
Sets the separator string to be used in formatted contents.

param
separator string to use between fields

        this.separator = separator;
    
public intsize()
Gets the size of the list.

return
the count of elements in the list

        return nvList.size();
    
public java.lang.StringtoString()
Converts contenets to a string.

return
contenets encoded in a text string

        return this.encode();