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

NameValue

public class NameValue extends GenericObject
Generic structure for storing name-value pairs.
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
protected boolean
isQuotedString
FLag to indicate the value is a quoted string.
protected String
separator
Field separator for name value encoding.
protected String
quotes
Characters used for quoting strings.
protected String
name
The label for the name value pair.
protected Object
value
The value for this data element.
Constructors Summary
public NameValue()
Default constructor.

        name = null; value = null;
        separator = Separators.EQUALS;
        this.quotes = "";
    
public NameValue(String n, Object v)
Constructs a name value pair from initial strings.

param
n the name of the key
param
v the value for the pair

        name = n;
        separator = Separators.EQUALS;
        quotes = "";
        setValue(v);
    
Methods Summary
public java.lang.Objectclone()
Makes a copy of the current instance.

return
copy of current object

        NameValue retval = new NameValue();
        retval.separator = this.separator;
        retval.isQuotedString = this.isQuotedString;
        retval.quotes = this.quotes;
        retval.name = this.name;
        if (value != null && value instanceof GenericObject) {
            retval.value = ((GenericObject)this.value).clone();
        } else retval.value = this.value;
        return retval;
    
public java.lang.Stringencode()
Gets the encoded representation of this namevalue object. Added doublequote for encoding doublequoted values

since
1.0
return
an encoded name value (eg. name=value) string.

        if (name != null && value != null) {
            return name + separator + quotes +
                    value.toString() +  quotes;
        } else if (name == null && value != null) {
            return quotes + value.toString() + quotes;
        } else if (name != null && value == null) {
            return name;
        } else return "";
    
public booleanequals(java.lang.Object other)
Equality comparison predicate.

param
other the object for comparison
return
true if the instances are equivalent

        if (! other.getClass().equals(this.getClass()))
            return false;
        NameValue that = (NameValue) other;
        if (this == that)
            return true;
        if (this.name  == null && that.name != null ||
                this.name != null && that.name == null) return false;
        if (this.name != null && that.name != null &&
                this.name.toLowerCase().compareTo
                (that.name.toLowerCase()) != 0)
            return false;
        if (this.value != null && that.value == null ||
                this.value == null && that.value != null)
            return false;
        if (this.value == that.value)
            return true;
        if (value instanceof String) {
            // Quoted string comparisions are case sensitive.
            if (isQuotedString)
                return this.value.equals(that.value);
            String val = (String) this.value;
            String val1 = (String) that.value;
            return val.toLowerCase().
                    equals(val1.toLowerCase());
        } else return this.value.equals(that.value);
    
public java.lang.StringgetName()
Gets the name.

return
the name

        return name;
    
public java.lang.ObjectgetUnquotedValue()
Gets the unquoted value.

return
the value

        return value;
    
public java.lang.ObjectgetValue()
Gets the value.

return
the value

        if (isValueQuoted()) {
            return "\"" + value + "\"";
        } else {
            return value;
        }
    
public booleanisValueQuoted()
Returns true if the value is quoted in doublequotes.

return
true if the value is of type quoted string

        return isQuotedString;
    
public voidsetName(java.lang.String n)
Sets the name member.

param
n the name for the key

        name = n;
    
public voidsetQuotedValue()
Sets the type of the field to be a quoted string. A flag that indicates that doublequotes should be put around the value when encoded (for example name=value when value is doublequoted).

        isQuotedString = true;
        this.quotes = Separators.DOUBLE_QUOTE;
    
public voidsetSeparator(java.lang.String sep)
Sets the separator for the encoding method below.

param
sep the field separator for the encoded string

        separator = sep;
    
public voidsetValue(java.lang.Object v)
Sets the value member.

param
v the value for the pair

        value = v;
        if (value != null) {
            if (value instanceof String) {
                String str = (String)value;
                if (str.startsWith("\"") && str.endsWith("\"")) {
                    setQuotedValue();
                    str = str.substring(1, str.length() - 1);
                    value = (Object)str;
                }
            }
        }
    
public java.lang.StringtoString()
Converts the contents to a string.

return
the encoded string contenets

        return this.encode();