Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 /* ------------------------------------------------------------ */
A multi valued Map. This Map specializes HashMap and provides methods that operate on multi valued items.

Implemented as a map of LazyList values

Parameters:
<K> The key type of the map.
See also:
LazyList
 
 public class MultiMap<K> implements ConcurrentMap<K,Object>, Serializable
 {
     private static final long serialVersionUID = -6878723138353851005L;
     Map<K,Object_map;
     ConcurrentMap<K, Object_cmap;
 
     public MultiMap()
     {
         =new HashMap<K, Object>();
     }
     
     public MultiMap(Map<K,Objectmap)
     {
         if (map instanceof ConcurrentMap)
             ==new ConcurrentHashMap<K, Object>(map);
         else
             =new HashMap<K, Object>(map);
     }
     
     public MultiMap(MultiMap<K> map)
     {
         if (map._cmap!=null)
             ==new ConcurrentHashMap<K, Object>(map._cmap);
         else
             =new HashMap<K,Object>(map._map);
     }
     
     public MultiMap(int capacity)
     {
         =new HashMap<K, Object>(capacity);
     }
     
     public MultiMap(boolean concurrent)
     {
         if (concurrent)
             ==new ConcurrentHashMap<K, Object>();
         else
             =new HashMap<K, Object>();
     }
     
 
     /* ------------------------------------------------------------ */
    
Get multiple values. Single valued entries are converted to singleton lists.

Parameters:
name The entry key.
Returns:
Unmodifieable List of values.
 
     public List getValues(Object name)
     {
         return LazyList.getList(.get(name),true);
     }
     
     /* ------------------------------------------------------------ */
    
Get a value from a multiple value. If the value is not a multivalue, then index 0 retrieves the value or null.

Parameters:
name The entry key.
i Index of element to get.
Returns:
Unmodifieable List of values.
    public Object getValue(Object name,int i)
    {
        Object l=.get(name);
        if (i==0 && LazyList.size(l)==0)
            return null;
        return LazyList.get(l,i);
    }
    
    
    /* ------------------------------------------------------------ */
    
Get value as String. Single valued items are converted to a String with the toString() Object method. Multi valued entries are converted to a comma separated List. No quoting of commas within values is performed.

Parameters:
name The entry key.
Returns:
String value.
    public String getString(Object name)
    {
        Object l=.get(name);
        switch(LazyList.size(l))
        {
          case 0:
              return null;
          case 1:
              Object o=LazyList.get(l,0);
              return o==null?null:o.toString();
          default:
          {
              StringBuilder values=new StringBuilder(128);
              for (int i=0; i<LazyList.size(l); i++)              
              {
                  Object e=LazyList.get(l,i);
                  if (e!=null)
                  {
                      if (values.length()>0)
                          values.append(',');
                      values.append(e.toString());
                  }
              }   
              return values.toString();
          }
        }
    }
    
    /* ------------------------------------------------------------ */
    public Object get(Object name
    {
        Object l=.get(name);
        switch(LazyList.size(l))
        {
          case 0:
              return null;
          case 1:
              Object o=LazyList.get(l,0);
              return o;
          default:
              return LazyList.getList(l,true);
        }
    }
    
    /* ------------------------------------------------------------ */
    
Put and entry into the map.

Parameters:
name The entry key.
value The entry value.
Returns:
The previous value or null.
    public Object put(K nameObject value
    {
        return .put(name,LazyList.add(null,value));
    }
    /* ------------------------------------------------------------ */
    
Put multi valued entry.

Parameters:
name The entry key.
values The List of multiple values.
Returns:
The previous value or null.
    public Object putValues(K nameList<? extends Objectvalues
    {
        return .put(name,values);
    }
    
    /* ------------------------------------------------------------ */
    
Put multi valued entry.

Parameters:
name The entry key.
values The String array of multiple values.
Returns:
The previous value or null.
    public Object putValues(K nameString... values
    {
        Object list=null;
        for (int i=0;i<values.length;i++)
            list=LazyList.add(list,values[i]);
        return .put(name,list);
    }
    
    
    /* ------------------------------------------------------------ */
    
Add value to multi valued entry. If the entry is single valued, it is converted to the first value of a multi valued entry.

Parameters:
name The entry key.
value The entry value.
    public void add(K nameObject value
    {
        Object lo = .get(name);
        Object ln = LazyList.add(lo,value);
        if (lo!=ln)
            .put(name,ln);
    }
    /* ------------------------------------------------------------ */
    
Add values to multi valued entry. If the entry is single valued, it is converted to the first value of a multi valued entry.

Parameters:
name The entry key.
values The List of multiple values.
    public void addValues(K nameList<? extends Objectvalues
    {
        Object lo = .get(name);
        Object ln = LazyList.addCollection(lo,values);
        if (lo!=ln)
            .put(name,ln);
    }
    
    /* ------------------------------------------------------------ */
    
Add values to multi valued entry. If the entry is single valued, it is converted to the first value of a multi valued entry.

Parameters:
name The entry key.
values The String array of multiple values.
    public void addValues(K nameString[] values
    {
        Object lo = .get(name);
        Object ln = LazyList.addCollection(lo,Arrays.asList(values));
        if (lo!=ln)
            .put(name,ln);
    }
    
    /* ------------------------------------------------------------ */
    
Remove value.

Parameters:
name The entry key.
value The entry value.
Returns:
true if it was removed.
    public boolean removeValue(K name,Object value)
    {
        Object lo = .get(name);
        Object ln=lo;
        int s=LazyList.size(lo);
        if (s>0)
        {
            ln=LazyList.remove(lo,value);
            if (ln==null)
                .remove(name);
            else
                .put(nameln);
        }
        return LazyList.size(ln)!=s;
    }
    
    
    /* ------------------------------------------------------------ */
    
Put all contents of map.

Parameters:
m Map
    public void putAll(Map<? extends K, ? extends Objectm)
    {
        boolean multi = (m instanceof MultiMap);
        if (multi)
        {
            for (Map.Entry<? extends K, ? extends Objectentry : m.entrySet())
            {
                .put(entry.getKey(),LazyList.clone(entry.getValue()));
            }
        }
        else
        {
            .putAll(m);
        }
    }
    /* ------------------------------------------------------------ */
    

Returns:
Map of String arrays
    public Map<K,String[]> toStringArrayMap()
    {
        HashMap<K,String[]> map = new HashMap<K,String[]>(.size()*3/2)
        {
            public String toString()
            {
                StringBuilder b=new StringBuilder();
                b.append('{');
                for (K k:keySet())
                {
                    if(b.length()>1)
                        b.append(',');
                    b.append(k);
                    b.append('=');
                    b.append(Arrays.asList(get(k)));
                }
                b.append('}');
                return b.toString();
            }
        };
        
        for(Map.Entry<K,Objectentry.entrySet())
        {
            String[] a = LazyList.toStringArray(entry.getValue());
            map.put(entry.getKey(),a);
        }
        return map;
    }
    @Override
    public String toString()
    {
        return ==null?.toString():.toString();
    }
    
    public void clear()
    {
        .clear();
    }
    public boolean containsKey(Object key)
    {
        return .containsKey(key);
    }
    public boolean containsValue(Object value)
    {
        return .containsValue(value);
    }
    public Set<Entry<K, Object>> entrySet()
    {
        return .entrySet();
    }
    @Override
    public boolean equals(Object o)
    {
        return .equals(o);
    }
    @Override
    public int hashCode()
    {
        return .hashCode();
    }
    public boolean isEmpty()
    {
        return .isEmpty();
    }
    public Set<K> keySet()
    {
        return .keySet();
    }
    public Object remove(Object key)
    {
        return .remove(key);
    }
    public int size()
    {
        return .size();
    }
    public Collection<Objectvalues()
    {
        return .values();
    }
    
    
    public Object putIfAbsent(K keyObject value)
    {
        if (==null)
            throw new UnsupportedOperationException();
        return .putIfAbsent(key,value);
    }
    public boolean remove(Object keyObject value)
    {
        if (==null)
            throw new UnsupportedOperationException();
        return .remove(key,value);
    }
    public boolean replace(K keyObject oldValueObject newValue)
    {
        if (==null)
            throw new UnsupportedOperationException();
        return .replace(key,oldValue,newValue);
    }
    public Object replace(K keyObject value)
    {
        if (==null)
            throw new UnsupportedOperationException();
        return .replace(key,value);
    }
New to GrepCode? Check out our FAQ X