X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=lib%2Fjetty%2Forg%2Feclipse%2Fjetty%2Futil%2FAttributesMap.java;fp=lib%2Fjetty%2Forg%2Feclipse%2Fjetty%2Futil%2FAttributesMap.java;h=c605e0995026e02ef03032c38f966a5786f9d8f5;hp=0000000000000000000000000000000000000000;hb=73ef54a38e3930a1a789cdc6b5fa23cdd4c9d086;hpb=515007c7c1351045420669d65b59c08fa46850f2 diff --git a/lib/jetty/org/eclipse/jetty/util/AttributesMap.java b/lib/jetty/org/eclipse/jetty/util/AttributesMap.java new file mode 100644 index 00000000..c605e099 --- /dev/null +++ b/lib/jetty/org/eclipse/jetty/util/AttributesMap.java @@ -0,0 +1,151 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicReference; + +public class AttributesMap implements Attributes +{ + private final AtomicReference> _map = new AtomicReference<>(); + + public AttributesMap() + { + } + + public AttributesMap(AttributesMap attributes) + { + ConcurrentMap map = attributes.map(); + if (map != null) + _map.set(new ConcurrentHashMap<>(map)); + } + + private ConcurrentMap map() + { + return _map.get(); + } + + private ConcurrentMap ensureMap() + { + while (true) + { + ConcurrentMap map = map(); + if (map != null) + return map; + map = new ConcurrentHashMap<>(); + if (_map.compareAndSet(null, map)) + return map; + } + } + + @Override + public void removeAttribute(String name) + { + Map map = map(); + if (map != null) + map.remove(name); + } + + @Override + public void setAttribute(String name, Object attribute) + { + if (attribute == null) + removeAttribute(name); + else + ensureMap().put(name, attribute); + } + + @Override + public Object getAttribute(String name) + { + Map map = map(); + return map == null ? null : map.get(name); + } + + @Override + public Enumeration getAttributeNames() + { + return Collections.enumeration(getAttributeNameSet()); + } + + public Set getAttributeNameSet() + { + return keySet(); + } + + public Set> getAttributeEntrySet() + { + Map map = map(); + return map == null ? Collections.>emptySet() : map.entrySet(); + } + + public static Enumeration getAttributeNamesCopy(Attributes attrs) + { + if (attrs instanceof AttributesMap) + return Collections.enumeration(((AttributesMap)attrs).keySet()); + + List names = new ArrayList<>(); + names.addAll(Collections.list(attrs.getAttributeNames())); + return Collections.enumeration(names); + } + + @Override + public void clearAttributes() + { + Map map = map(); + if (map != null) + map.clear(); + } + + public int size() + { + Map map = map(); + return map == null ? 0 : map.size(); + } + + @Override + public String toString() + { + Map map = map(); + return map == null ? "{}" : map.toString(); + } + + private Set keySet() + { + Map map = map(); + return map == null ? Collections.emptySet() : map.keySet(); + } + + public void addAll(Attributes attributes) + { + Enumeration e = attributes.getAttributeNames(); + while (e.hasMoreElements()) + { + String name = e.nextElement(); + setAttribute(name, attributes.getAttribute(name)); + } + } +}