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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.List;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29 import java.util.concurrent.atomic.AtomicReference;
31 public class AttributesMap implements Attributes
33 private final AtomicReference<ConcurrentMap<String, Object>> _map = new AtomicReference<>();
35 public AttributesMap()
39 public AttributesMap(AttributesMap attributes)
41 ConcurrentMap<String, Object> map = attributes.map();
43 _map.set(new ConcurrentHashMap<>(map));
46 private ConcurrentMap<String, Object> map()
51 private ConcurrentMap<String, Object> ensureMap()
55 ConcurrentMap<String, Object> map = map();
58 map = new ConcurrentHashMap<>();
59 if (_map.compareAndSet(null, map))
65 public void removeAttribute(String name)
67 Map<String, Object> map = map();
73 public void setAttribute(String name, Object attribute)
75 if (attribute == null)
76 removeAttribute(name);
78 ensureMap().put(name, attribute);
82 public Object getAttribute(String name)
84 Map<String, Object> map = map();
85 return map == null ? null : map.get(name);
89 public Enumeration<String> getAttributeNames()
91 return Collections.enumeration(getAttributeNameSet());
94 public Set<String> getAttributeNameSet()
99 public Set<Map.Entry<String, Object>> getAttributeEntrySet()
101 Map<String, Object> map = map();
102 return map == null ? Collections.<Map.Entry<String, Object>>emptySet() : map.entrySet();
105 public static Enumeration<String> getAttributeNamesCopy(Attributes attrs)
107 if (attrs instanceof AttributesMap)
108 return Collections.enumeration(((AttributesMap)attrs).keySet());
110 List<String> names = new ArrayList<>();
111 names.addAll(Collections.list(attrs.getAttributeNames()));
112 return Collections.enumeration(names);
116 public void clearAttributes()
118 Map<String, Object> map = map();
125 Map<String, Object> map = map();
126 return map == null ? 0 : map.size();
130 public String toString()
132 Map<String, Object> map = map();
133 return map == null ? "{}" : map.toString();
136 private Set<String> keySet()
138 Map<String, Object> map = map();
139 return map == null ? Collections.<String>emptySet() : map.keySet();
142 public void addAll(Attributes attributes)
144 Enumeration<String> e = attributes.getAttributeNames();
145 while (e.hasMoreElements())
147 String name = e.nextElement();
148 setAttribute(name, attributes.getAttribute(name));