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