Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Conditions Of Use
   *
   * This software was developed by employees of the National Institute of
   * Standards and Technology (NIST), an agency of the Federal Government.
   * Pursuant to title 15 Untied States Code Section 105, works of NIST
   * employees are not subject to copyright protection in the United States
   * and are considered to be in the public domain.  As a result, a formal
   * license is not needed to use the software.
  *
  * This software is provided by NIST as a service and is expressly
  * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
  * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
  * AND DATA ACCURACY.  NIST does not warrant or make any representations
  * regarding the use of the software or the results thereof, including but
  * not limited to the correctness, accuracy, reliability or usefulness of
  * the software.
  *
  * Permission to use this software is contingent upon your acceptance
  * of the terms of this agreement
  *
  * .
  *
  */
Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * /
 
 package gov.nist.core;
 
 
 /*
  * Bug reports and fixes: Kirby Kiem, Jeroen van Bemmel.
  */

Generic structure for storing name-value pairs.

Author(s):
M. Ranganathan
Version:
1.2
 
 public class NameValue extends GenericObject implements Entry<String,String> {
 
     private static final long serialVersionUID = -1857729012596437950L;
 
     protected boolean isQuotedString;
 
     protected final boolean isFlagParameter;
 
     private String separator;
 
     private String quotes;
 
     private String name;
 
     private Object value;
 
     public NameValue() {
          = null;
          = "";
          = .;
         this. = "";
         this. = false;
     }

    
New constructor, taking a boolean which is set if the NV pair is a flag

Parameters:
n
v
isFlag
 
     public NameValue(String nObject vboolean isFlag) {
 
         // assert (v != null ); // I dont think this assertion is correct mranga
 
          = n;
          = v;
          = .;
          = "";
         this. = isFlag;
     }

    
Original constructor, sets isFlagParameter to 'false'

Parameters:
n
v
 
     public NameValue(String nObject v) {
         this(nvfalse);
     }

    
Set the separator for the encoding method below.
    public void setSeparator(String sep) {
         = sep;
    }

    
A flag that indicates that doublequotes should be put around the value when encoded (for example name=value when value is doublequoted).
    public void setQuotedValue() {
         = true;
        this. = .;
    }

    
Return true if the value is quoted in doublequotes.
    public boolean isValueQuoted() {
        return ;
    }
    public String getName() {
        return ;
    }
    public Object getValueAsObject() {
        if()
            return ""// never return null for flag params
        
        return ;
    }

    
Set the name member
    public void setName(String n) {
         = n;
    }

    
Set the value member
    public void setValueAsObject(Object v) {
         = v;
    }

    
Get the encoded representation of this namevalue object. Added doublequote for encoding doublequoted values. Bug: RFC3261 stipulates that an opaque parameter in authenticate header has to be: opaque = "opaque" EQUAL quoted-string so returning just the name is not acceptable. (e.g. LinkSys phones are picky about this)

Returns:
an encoded name value (eg. name=value) string.
Since:
1.0
    public String encode() {
        return encode(new StringBuffer()).toString();
    }
    public StringBuffer encode(StringBuffer buffer) {
        if ( != null &&  != null && !) {
            if (GenericObject.isMySubclass(.getClass())) {
                GenericObject gv = (GenericObject;
                buffer.append().append().append();
                gv.encode(buffer);
                buffer.append();
                return buffer;
            } else if (GenericObjectList.isMySubclass(.getClass())) {
                GenericObjectList gvlist = (GenericObjectList;
                buffer.append().append().append(gvlist.encode());
                return buffer;
            } else if ( .toString().length() == 0) {
                // opaque="" bug fix - pmusgrave
                /*if (name.toString().equals(gov.nist.javax.sip.header.ParameterNames.OPAQUE))
                    return name + separator + quotes + quotes;
                else
                    return name;*/
                if ( this. ) {
                    buffer.append().append().append().append();
                    return buffer;
                } else {
                    buffer.append().append(); // JvB: fix, case: "sip:host?subject="
                    return buffer;
                }
            } else {
                buffer.append().append().append().append(.toString()).append();
                return buffer;
            }
        } else if ( == null &&  != null) {
            if (GenericObject.isMySubclass(.getClass())) {
                GenericObject gv = (GenericObject;
                gv.encode(buffer);
                return buffer;
            } else if (GenericObjectList.isMySubclass(.getClass())) {
                GenericObjectList gvlist = (GenericObjectList;
                buffer.append(gvlist.encode());
                return buffer;
            } else {
                buffer.append().append(.toString()).append();
                return buffer;
            }
        } else if ( != null && ( == null || )) {
            buffer.append();
            return buffer;
        } else {
            return buffer;
        }
    }
    public Object clone() {
        NameValue retval = (NameValuesuper.clone();
        if ( != null)
            retval.value = makeClone();
        return retval;
    }

    
Equality comparison predicate.
    public boolean equals(Object other) {
        if (other == null ) return false;
        if (!other.getClass().equals(this.getClass()))
            return false;
        NameValue that = (NameValueother;
        if (this == that)
            return true;
        if (this. == null && that.name != null || this. != null
                && that.name == null)
            return false;
        if (this. != null && that.name != null
                && this..compareToIgnoreCase(that.name) != 0)
            return false;
        if (this. != null && that.value == null || this. == null
                && that.value != null)
            return false;
        if (this. == that.value)
            return true;
        if ( instanceof String) {
            // Quoted string comparisions are case sensitive.
            if ()
                return this..equals(that.value);
            String val = (Stringthis.;
            String val1 = (Stringthat.value;
            return val.compareToIgnoreCase(val1) == 0;
        } else
            return this..equals(that.value);
    }
    /*
     * (non-Javadoc)
     * @see java.util.Map$Entry#getKey()
     */
    public String getKey() {
        return this.;
    }
    /*
     * (non-Javadoc)
     * @see java.util.Map$Entry#getValue()
     */
    public String getValue() {
        if( == null)
        	return null;
        
        return .toString();
    }
    /*
     * (non-Javadoc)
     * @see java.util.Map$Entry#setValue(java.lang.Object)
     */
    public String setValue(String value) {
        String retval = this. == null ? null : value;
        this. = value;
        return retval;
    }
    
    @Override
    public int hashCode() {
        return this.encode().toLowerCase().hashCode();
    }
New to GrepCode? Check out our FAQ X