Home | History | Annotate | Download | only in core
      1 /*
      2  * Conditions Of Use
      3  *
      4  * This software was developed by employees of the National Institute of
      5  * Standards and Technology (NIST), an agency of the Federal Government.
      6  * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7  * employees are not subject to copyright protection in the United States
      8  * and are considered to be in the public domain.  As a result, a formal
      9  * license is not needed to use the software.
     10  *
     11  * This software is provided by NIST as a service and is expressly
     12  * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13  * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15  * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16  * regarding the use of the software or the results thereof, including but
     17  * not limited to the correctness, accuracy, reliability or usefulness of
     18  * the software.
     19  *
     20  * Permission to use this software is contingent upon your acceptance
     21  * of the terms of this agreement.
     22  *
     23  */
     24 package gov.nist.core;
     25 
     26 import java.io.Serializable;
     27 import java.util.Collection;
     28 import java.util.Iterator;
     29 import java.util.Map;
     30 import java.util.Set;
     31 
     32 /**
     33  *
     34  *
     35  * This is a Duplicate Name Value List that will allow multiple values map to the same key.
     36  *
     37  * The parsing and encoding logic for it is the same as that of NameValueList. Only the HashMap
     38  * container is different.
     39  *
     40  * @author aayush.bhatnagar
     41  * @since 2.0
     42  *
     43  */
     44 public class DuplicateNameValueList implements Serializable, Cloneable {
     45 
     46     private MultiValueMapImpl<NameValue> nameValueMap = new MultiValueMapImpl<NameValue>();
     47     private String separator;
     48 
     49     private static final long serialVersionUID = -5611332957903796952L;
     50 
     51     public DuplicateNameValueList()
     52 
     53     {
     54         this.separator = ";";
     55     }
     56 
     57     // ------------------
     58 
     59     public void setSeparator(String separator) {
     60         this.separator = separator;
     61     }
     62 
     63     /**
     64      * Encode the list in semicolon separated form.
     65      *
     66      * @return an encoded string containing the objects in this list.
     67      * @since v1.0
     68      */
     69     public String encode() {
     70         return encode(new StringBuffer()).toString();
     71     }
     72 
     73     public StringBuffer encode(StringBuffer buffer) {
     74         if (!nameValueMap.isEmpty()) {
     75             Iterator<NameValue> iterator = nameValueMap.values().iterator();
     76             if (iterator.hasNext()) {
     77                 while (true) {
     78                     Object obj = iterator.next();
     79                     if (obj instanceof GenericObject) {
     80                         GenericObject gobj = (GenericObject) obj;
     81                         gobj.encode(buffer);
     82                     } else {
     83                         buffer.append(obj.toString());
     84                     }
     85                     if (iterator.hasNext())
     86                         buffer.append(separator);
     87                     else
     88                         break;
     89                 }
     90             }
     91         }
     92         return buffer;
     93     }
     94 
     95     public String toString() {
     96         return this.encode();
     97     }
     98 
     99     /**
    100      * Set a namevalue object in this list.
    101      */
    102 
    103     public void set(NameValue nv) {
    104         this.nameValueMap.put(nv.getName().toLowerCase(), nv);
    105     }
    106 
    107     /**
    108      * Set a namevalue object in this list.
    109      */
    110     public void set(String name, Object value) {
    111         NameValue nameValue = new NameValue(name, value);
    112         nameValueMap.put(name.toLowerCase(), nameValue);
    113 
    114     }
    115 
    116     /**
    117      * Compare if two NameValue lists are equal.
    118      *
    119      * @param otherObject is the object to compare to.
    120      * @return true if the two objects compare for equality.
    121      */
    122     public boolean equals(Object otherObject) {
    123         if ( otherObject == null ) {
    124             return false;
    125         }
    126         if (!otherObject.getClass().equals(this.getClass())) {
    127             return false;
    128         }
    129         DuplicateNameValueList other = (DuplicateNameValueList) otherObject;
    130 
    131         if (nameValueMap.size() != other.nameValueMap.size()) {
    132             return false;
    133         }
    134         Iterator<String> li = this.nameValueMap.keySet().iterator();
    135 
    136         while (li.hasNext()) {
    137             String key = (String) li.next();
    138             Collection nv1 = this.getNameValue(key);
    139             Collection nv2 = (Collection) other.nameValueMap.get(key);
    140             if (nv2 == null)
    141                 return false;
    142             else if (!nv2.equals(nv1))
    143                 return false;
    144         }
    145         return true;
    146     }
    147 
    148     /**
    149      * Do a lookup on a given name and return value associated with it.
    150      */
    151     public Object getValue(String name) {
    152         Collection nv = this.getNameValue(name.toLowerCase());
    153         if (nv != null)
    154             return nv;
    155         else
    156             return null;
    157     }
    158 
    159     /**
    160      * Get the NameValue record given a name.
    161      *
    162      */
    163     public Collection getNameValue(String name) {
    164         return (Collection) this.nameValueMap.get(name.toLowerCase());
    165     }
    166 
    167     /**
    168      * Returns a boolean telling if this NameValueList has a record with this name
    169      */
    170     public boolean hasNameValue(String name) {
    171         return nameValueMap.containsKey(name.toLowerCase());
    172     }
    173 
    174     /**
    175      * Remove the element corresponding to this name.
    176      */
    177     public boolean delete(String name) {
    178         String lcName = name.toLowerCase();
    179         if (this.nameValueMap.containsKey(lcName)) {
    180             this.nameValueMap.remove(lcName);
    181             return true;
    182         } else {
    183             return false;
    184         }
    185 
    186     }
    187 
    188     public Object clone() {
    189         DuplicateNameValueList retval = new DuplicateNameValueList();
    190         retval.setSeparator(this.separator);
    191         Iterator<NameValue> it = this.nameValueMap.values().iterator();
    192         while (it.hasNext()) {
    193             retval.set((NameValue) ((NameValue) it.next()).clone());
    194         }
    195         return retval;
    196     }
    197 
    198     /**
    199      * Return an iterator for the name-value pairs of this list.
    200      *
    201      * @return the iterator.
    202      */
    203     public Iterator<NameValue> iterator() {
    204         return this.nameValueMap.values().iterator();
    205     }
    206 
    207     /**
    208      * Get a list of parameter names.
    209      *
    210      * @return a list iterator that has the names of the parameters.
    211      */
    212     public Iterator<String> getNames() {
    213         return this.nameValueMap.keySet().iterator();
    214 
    215     }
    216 
    217     /**
    218      * Get the parameter as a String.
    219      *
    220      * @return the parameter as a string.
    221      */
    222     public String getParameter(String name) {
    223         Object val = this.getValue(name);
    224         if (val == null)
    225             return null;
    226         if (val instanceof GenericObject)
    227             return ((GenericObject) val).encode();
    228         else
    229             return val.toString();
    230     }
    231 
    232     public void clear() {
    233         nameValueMap.clear();
    234 
    235     }
    236 
    237     public boolean isEmpty() {
    238         return this.nameValueMap.isEmpty();
    239     }
    240 
    241     public NameValue put(String key, NameValue value) {
    242         return (NameValue) this.nameValueMap.put(key, value);
    243     }
    244 
    245     public NameValue remove(Object key) {
    246         return (NameValue) this.nameValueMap.remove(key);
    247     }
    248 
    249     public int size() {
    250         return this.nameValueMap.size();
    251     }
    252 
    253     public Collection<NameValue> values() {
    254         return this.nameValueMap.values();
    255     }
    256 
    257     public int hashCode() {
    258         return this.nameValueMap.keySet().hashCode();
    259     }
    260 
    261 }
    262