1 23 24 38 39 package com.sun.enterprise.admin.monitor.registry.spi; 40 import java.util.Map ; 41 import java.util.ArrayList ; 42 import java.util.Set ; 43 import java.util.HashSet ; 44 import java.util.HashMap ; 45 import java.util.Collection ; 46 import java.util.Iterator ; 47 48 import com.sun.enterprise.util.i18n.StringManager; 49 63 public final class ValueListMap implements Map { 64 65 public final static Class LISTENER_CLASS = com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener.class; 66 67 private static final StringManager sm = StringManager.getManager(ValueListMap.class); 68 private final Class valueClass; 69 private final Map map; 70 71 79 public ValueListMap(Class valueClass) { 80 if (valueClass == null) { 81 final String msg = sm.getString("gen.illegal_arg"); 82 throw new IllegalArgumentException (msg); 83 } 84 this.map = new HashMap (); 85 this.valueClass = valueClass; 86 } 87 88 92 public ValueListMap () { 93 this(LISTENER_CLASS); 94 } 95 96 public void clear() { 97 map.clear(); 98 } 99 100 public boolean containsKey(Object key) { 101 return ( map.containsKey(key) ); 102 } 103 104 107 public boolean containsValue(Object value){ 108 throw new UnsupportedOperationException ("ValueListMap:containsValue() - Not supported"); 109 } 110 111 public Set entrySet() { 112 return ( map.entrySet() ); 113 } 114 115 122 public Object get(Object key) { 123 return ( map.get(key) ); 124 } 125 126 public boolean isEmpty() { 127 return ( map.isEmpty() ); 128 } 129 130 public Set keySet() { 131 return ( map.keySet() ); 132 } 133 134 146 public Object put(Object key, Object value) { 147 String msg = null; 148 if (key == null || value == null) { 149 msg = sm.getString("sm.illegal_arg"); 150 throw new IllegalArgumentException ("Null Argument"); 151 } 152 if (! implementsValueClass(value)) { 153 msg = sm.getString("sm.illegal_arg_class", valueClass.getName()); 154 throw new IllegalArgumentException (msg); 155 } 156 Map mm = (Map ) map.get(key); if (mm == null) { 158 mm = addNewKey(key); 159 } 160 return mm.put(value, new Integer (1)); } 162 163 166 public void putAll(Map t) { 167 throw new UnsupportedOperationException ("ValueListMap:putAll() - Not supported"); 168 } 169 170 174 public Object remove(Object keyOrValue) { 175 Collection removed = null; 176 if (valueClass.isAssignableFrom(keyOrValue.getClass())) { 177 final Object value = keyOrValue; removed = removeValues(value); 179 } 180 else { 181 final Object key = keyOrValue; removed = removeKeyedValues(key); 183 map.remove(key); 184 } 185 return ( removed ); 186 } 187 188 private Collection removeValues(Object value) { 189 final Collection list = new ArrayList (); 190 final Iterator iter = map.keySet().iterator(); 191 while (iter.hasNext()) { 192 final Map mm = (Map ) map.get(iter.next()); mm.remove(value); list.add(value); 195 } 196 return ( list ); 197 } 198 199 private Collection removeKeyedValues(Object key) { 200 Collection list = new ArrayList (); 201 final Object value = this.get(key); 202 if (value != null && value instanceof Map ) { 203 list = ((Map )value).keySet(); 204 } 205 return ( list ); 206 } 207 208 public int size() { 209 return ( map.size() ); 210 } 211 212 217 public Collection values() { 218 final Collection values = new ArrayList (); 220 final Iterator iter = this.keySet().iterator(); 221 while (iter.hasNext()) { 222 final Map mm = (Map ) map.get(iter.next()); 223 values.addAll(mm.keySet()); 224 } 225 return ( values ); 226 } 227 228 private Map addNewKey(Object key) { 229 final Map v = new HashMap (); 230 map.put(key, v); 231 return ( v ); 232 } 233 234 private boolean implementsValueClass(Object value) { 235 boolean ivc = false; 236 final Class [] ics = value.getClass().getInterfaces(); 237 for (int i = 0 ; i < ics.length ; i++) { 238 if (valueClass.equals(ics[i])) { 239 ivc = true; 240 break; 241 } 242 } 243 return ( ivc ); 244 } 245 } 246 | Popular Tags |