All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.specimpl.MultivaluedTreeMap Maven / Gradle / Ivy

There is a newer version: 4.0.0.Beta5
Show newest version
package org.jboss.resteasy.specimpl;

import javax.ws.rs.core.MultivaluedMap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class MultivaluedTreeMap implements MultivaluedMap, Serializable
{
   private final Map> map;

   public MultivaluedTreeMap()
   {
      map = new TreeMap>();
   }

   /**
    * Used to create a CaseInsensitiveMap.
    *
    * @param keyComparator key comparator
    */

   public MultivaluedTreeMap(final Comparator keyComparator)
   {
      map = new TreeMap>(keyComparator);
   }

   public MultivaluedTreeMap(final Map map)
   {
      this();
      for (K key : map.keySet())
      {
         add(key, map.get(key));
      }
   }

   public void add(K key, V value)
   {
      List list = getOrCreate(key);
      list.add(value);
   }

   public V getFirst(K key)
   {
      List list = get(key);
      if (list == null || list.size() == 0)
      {
         return null;
      }
      return list.get(0);
   }

   public void putSingle(K key, V value)
   {
      List list = getOrCreate(key);
      list.clear();
      list.add(value);
   }

   private List getOrCreate(K key)
   {
      List list = this.get(key);
      if (list == null)
      {
         list = createValueList(key);
         this.put(key, list);
      }
      return list;
   }

   private List createValueList(K key)
   {
      return new ArrayList();
   }

   public MultivaluedTreeMap clone()
   {
      return clone(this);
   }

   public static  MultivaluedTreeMap clone(MultivaluedMap src)
   {
      MultivaluedTreeMap clone = new MultivaluedTreeMap();
      copy(src, clone);
      return clone;
   }

   public static  void copy(MultivaluedMap src, MultivaluedMap dest)
   {
      for (K key : src.keySet())
      {
         List value = src.get(key);
         List newValue = new ArrayList();
         newValue.addAll(value);
         dest.put(key, newValue);
      }
   }

   public static  void addAll(MultivaluedMap src, MultivaluedMap dest)
   {
      for (K key : src.keySet())
      {
         List srcList = src.get(key);
         List destList = dest.get(key);
         if (destList == null)
         {
            destList = new ArrayList(srcList.size());
            dest.put(key, destList);
         }
         destList.addAll(srcList);
      }
   }

   @Override
   public String toString()
   {
      return "[" + toString(this, ",") + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
   }

   public static String toString(MultivaluedMap map, String delimiter)
   {
      StringBuilder result = new StringBuilder();
      MultivaluedMap params = map;
      String delim = ""; //$NON-NLS-1$
      for (Object name : params.keySet())
      {
         for (Object value : params.get(name))
         {
            result.append(delim);
            if (name == null)
            {
               result.append("null"); //$NON-NLS-1$
            }
            else
            {
               result.append(name.toString());
            }
            if (value != null)
            {
               result.append('=');
               result.append(value.toString());
            }
            delim = delimiter;
         }
      }
      return result.toString();
   }

   public void clear()
   {
      map.clear();
   }

   public boolean containsKey(Object key)
   {
      return map.containsKey(key);
   }

   public boolean containsValue(Object value)
   {
      return map.containsValue(value);
   }

   public Set>> entrySet()
   {
      return map.entrySet();
   }

   public boolean equals(Object o)
   {
      return map.equals(o);
   }

   public List get(Object key)
   {
      return map.get(key);
   }

   public int hashCode()
   {
      return map.hashCode();
   }

   public boolean isEmpty()
   {
      return map.isEmpty();
   }

   public Set keySet()
   {
      return map.keySet();
   }

   public List put(K key, List value)
   {
      return map.put(key, value);
   }

   public void putAll(Map> t)
   {
      map.putAll(t);
   }

   public List remove(Object key)
   {
      return map.remove(key);
   }

   public int size()
   {
      return map.size();
   }

   public Collection> values()
   {
      return map.values();
   }

   @SuppressWarnings(value = "unchecked")
   @Override
   public void addAll(K key, V... newValues)
   {
      for (V value : newValues)
      {
         add(key, value);
      }
   }

   @Override
   public void addAll(K key, List valueList)
   {
      for (V value : valueList)
      {
         add(key, value);
      }
   }

   @Override
   public void addFirst(K key, V value)
   {
      List list = get(key);
      if (list == null)
      {
         add(key, value);
         return;
      }
      else
      {
         list.add(0, value);
      }
   }

   @Override
   public boolean equalsIgnoreValueOrder(MultivaluedMap omap)
   {
      if (this == omap)
      {
         return true;
      }
      if (!keySet().equals(omap.keySet()))
      {
         return false;
      }
      for (Map.Entry> e : entrySet())
      {
         List olist = omap.get(e.getKey());
         if (e.getValue().size() != olist.size())
         {
            return false;
         }
         for (V v : e.getValue())
         {
            if (!olist.contains(v))
            {
               return false;
            }
         }
      }
      return true;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy