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.Map;
21  import java.util.Set;
22  
23  /* ------------------------------------------------------------ */
24  /** AttributesMap.
25   * 
26   *
27   */
28  public class AttributesMap implements Attributes
29  {
30      protected final Map<String,Object> _map;
31  
32      /* ------------------------------------------------------------ */
33      public AttributesMap()
34      {
35          _map=new HashMap<String,Object>();
36      }
37      
38      /* ------------------------------------------------------------ */
39      public AttributesMap(Map<String,Object> map)
40      {
41          _map=map;
42      }
43  
44      /* ------------------------------------------------------------ */
45      public AttributesMap(AttributesMap map)
46      {
47          _map=new HashMap<String,Object>(map._map);
48      }
49      
50      /* ------------------------------------------------------------ */
51      /* 
52       * @see org.eclipse.jetty.util.Attributes#removeAttribute(java.lang.String)
53       */
54      public void removeAttribute(String name)
55      {
56          _map.remove(name);
57      }
58  
59      /* ------------------------------------------------------------ */
60      /* 
61       * @see org.eclipse.jetty.util.Attributes#setAttribute(java.lang.String, java.lang.Object)
62       */
63      public void setAttribute(String name, Object attribute)
64      {
65          if (attribute==null)
66              _map.remove(name);
67          else
68              _map.put(name, attribute);
69      }
70  
71      /* ------------------------------------------------------------ */
72      /* 
73       * @see org.eclipse.jetty.util.Attributes#getAttribute(java.lang.String)
74       */
75      public Object getAttribute(String name)
76      {
77          return _map.get(name);
78      }
79  
80      /* ------------------------------------------------------------ */
81      /* 
82       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
83       */
84      public Enumeration<String> getAttributeNames()
85      {
86          return Collections.enumeration(_map.keySet());
87      }
88  
89      /* ------------------------------------------------------------ */
90      /* 
91       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
92       */
93      public Set<String> getAttributeNameSet()
94      {
95          return _map.keySet();
96      }
97      
98      /* ------------------------------------------------------------ */
99      public Set<Map.Entry<String, Object>> getAttributeEntrySet()
100     {
101         return _map.entrySet();
102     }
103     
104     /* ------------------------------------------------------------ */
105     /* 
106      * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
107      */
108     public static Enumeration<String> getAttributeNamesCopy(Attributes attrs)
109     {
110         if (attrs instanceof AttributesMap)
111             return Collections.enumeration(((AttributesMap)attrs)._map.keySet());
112         ArrayList names = new ArrayList();
113         Enumeration e = attrs.getAttributeNames();
114         while (e.hasMoreElements())
115             names.add(e.nextElement());
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 }