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 Map _map; 31 32 /* ------------------------------------------------------------ */ 33 public AttributesMap() 34 { 35 _map=new HashMap(); 36 } 37 38 /* ------------------------------------------------------------ */ 39 public AttributesMap(Map map) 40 { 41 _map=map; 42 } 43 44 public AttributesMap(AttributesMap map) 45 { 46 _map=new HashMap(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 getAttributeNames() 84 { 85 return Collections.enumeration(_map.keySet()); 86 } 87 88 /* ------------------------------------------------------------ */ 89 /* 90 * @see org.eclipse.jetty.util.Attributes#getAttributeNames() 91 */ 92 public static Enumeration getAttributeNamesCopy(Attributes attrs) 93 { 94 if (attrs instanceof AttributesMap) 95 return Collections.enumeration(((AttributesMap)attrs)._map.keySet()); 96 ArrayList names = new ArrayList(); 97 Enumeration e = attrs.getAttributeNames(); 98 while (e.hasMoreElements()) 99 names.add(e.nextElement()); 100 return Collections.enumeration(names); 101 } 102 103 /* ------------------------------------------------------------ */ 104 /* 105 * @see org.eclipse.jetty.util.Attributes#clear() 106 */ 107 public void clearAttributes() 108 { 109 _map.clear(); 110 } 111 112 /* ------------------------------------------------------------ */ 113 public String toString() 114 { 115 return _map.toString(); 116 } 117 118 /* ------------------------------------------------------------ */ 119 public Set<String> keySet() 120 { 121 return _map.keySet(); 122 } 123 124 }