View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.util;
15  
16  import java.util.ArrayList;
17  import java.util.Collections;
18  import java.util.Enumeration;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  /* ------------------------------------------------------------ */
25  /** AttributesMap.
26   * 
27   *
28   */
29  public class AttributesMap implements Attributes
30  {
31      protected final Map<String,Object> _map;
32  
33      /* ------------------------------------------------------------ */
34      public AttributesMap()
35      {
36          _map=new HashMap<String,Object>();
37      }
38      
39      /* ------------------------------------------------------------ */
40      public AttributesMap(Map<String,Object> map)
41      {
42          _map=map;
43      }
44  
45      /* ------------------------------------------------------------ */
46      public AttributesMap(AttributesMap map)
47      {
48          _map=new HashMap<String,Object>(map._map);
49      }
50      
51      /* ------------------------------------------------------------ */
52      /* 
53       * @see org.eclipse.jetty.util.Attributes#removeAttribute(java.lang.String)
54       */
55      public void removeAttribute(String name)
56      {
57          _map.remove(name);
58      }
59  
60      /* ------------------------------------------------------------ */
61      /* 
62       * @see org.eclipse.jetty.util.Attributes#setAttribute(java.lang.String, java.lang.Object)
63       */
64      public void setAttribute(String name, Object attribute)
65      {
66          if (attribute==null)
67              _map.remove(name);
68          else
69              _map.put(name, attribute);
70      }
71  
72      /* ------------------------------------------------------------ */
73      /* 
74       * @see org.eclipse.jetty.util.Attributes#getAttribute(java.lang.String)
75       */
76      public Object getAttribute(String name)
77      {
78          return _map.get(name);
79      }
80  
81      /* ------------------------------------------------------------ */
82      /* 
83       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
84       */
85      public Enumeration<String> getAttributeNames()
86      {
87          return Collections.enumeration(_map.keySet());
88      }
89  
90      /* ------------------------------------------------------------ */
91      /* 
92       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
93       */
94      public Set<String> getAttributeNameSet()
95      {
96          return _map.keySet();
97      }
98      
99      /* ------------------------------------------------------------ */
100     public Set<Map.Entry<String, Object>> getAttributeEntrySet()
101     {
102         return _map.entrySet();
103     }
104     
105     /* ------------------------------------------------------------ */
106     /* 
107      * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
108      */
109     public static Enumeration<String> getAttributeNamesCopy(Attributes attrs)
110     {
111         if (attrs instanceof AttributesMap)
112             return Collections.enumeration(((AttributesMap)attrs)._map.keySet());
113         
114         List<String> names = new ArrayList<String>();
115         names.addAll(Collections.list(attrs.getAttributeNames()));
116         return Collections.enumeration(names);
117     }
118 
119     /* ------------------------------------------------------------ */
120     /* 
121      * @see org.eclipse.jetty.util.Attributes#clear()
122      */
123     public void clearAttributes()
124     {
125         _map.clear();
126     }
127     
128     /* ------------------------------------------------------------ */
129     public int size()
130     {
131         return _map.size();
132     }
133     
134     /* ------------------------------------------------------------ */
135     @Override
136     public String toString()
137     {
138         return _map.toString();
139     }
140     
141     /* ------------------------------------------------------------ */
142     public Set<String> keySet()
143     {
144         return _map.keySet();
145     }
146     
147     /* ------------------------------------------------------------ */
148     public void addAll(Attributes attributes)
149     {
150         Enumeration<String> e = attributes.getAttributeNames();
151         while (e.hasMoreElements())
152         {
153             String name=e.nextElement();
154             setAttribute(name,attributes.getAttribute(name));
155         }
156     }
157 
158 }