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      public AttributesMap(AttributesMap map)
45      {
46          _map=new HashMap<String,Object>(map._map);
47      }
48      
49      /* ------------------------------------------------------------ */
50      /* 
51       * @see org.eclipse.jetty.util.Attributes#removeAttribute(java.lang.String)
52       */
53      public void removeAttribute(String name)
54      {
55          _map.remove(name);
56      }
57  
58      /* ------------------------------------------------------------ */
59      /* 
60       * @see org.eclipse.jetty.util.Attributes#setAttribute(java.lang.String, java.lang.Object)
61       */
62      public void setAttribute(String name, Object attribute)
63      {
64          if (attribute==null)
65              _map.remove(name);
66          else
67              _map.put(name, attribute);
68      }
69  
70      /* ------------------------------------------------------------ */
71      /* 
72       * @see org.eclipse.jetty.util.Attributes#getAttribute(java.lang.String)
73       */
74      public Object getAttribute(String name)
75      {
76          return _map.get(name);
77      }
78  
79      /* ------------------------------------------------------------ */
80      /* 
81       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
82       */
83      public Enumeration<String> getAttributeNames()
84      {
85          return Collections.enumeration(_map.keySet());
86      }
87  
88      /* ------------------------------------------------------------ */
89      /* 
90       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
91       */
92      public Set<String> getAttributeNameSet()
93      {
94          return _map.keySet();
95      }
96  
97      /* ------------------------------------------------------------ */
98      /* 
99       * @see org.eclipse.jetty.util.Attributes#getAttributeNames()
100      */
101     public static Enumeration<String> getAttributeNamesCopy(Attributes attrs)
102     {
103         if (attrs instanceof AttributesMap)
104             return Collections.enumeration(((AttributesMap)attrs)._map.keySet());
105         ArrayList names = new ArrayList();
106         Enumeration e = attrs.getAttributeNames();
107         while (e.hasMoreElements())
108             names.add(e.nextElement());
109         return Collections.enumeration(names);
110     }
111 
112     /* ------------------------------------------------------------ */
113     /* 
114      * @see org.eclipse.jetty.util.Attributes#clear()
115      */
116     public void clearAttributes()
117     {
118         _map.clear();
119     }
120 
121     /* ------------------------------------------------------------ */
122     @Override
123     public String toString()
124     {
125         return _map.toString();
126     }
127     
128     /* ------------------------------------------------------------ */
129     public Set<String> keySet()
130     {
131         return _map.keySet();
132     }
133 
134 }