1 22 package org.jboss.util.property; 23 24 import java.util.Properties ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Iterator ; 30 31 import org.jboss.util.NullArgumentException; 32 33 45 public class PropertyGroup extends PropertyMap 46 { 47 48 private static final long serialVersionUID = -2557641199743063159L; 49 50 protected final String basename; 51 52 60 public PropertyGroup(final String basename, final Properties container) 61 { 62 super(container); 63 64 if (basename == null) 65 throw new NullArgumentException("basename"); 66 67 this.basename = basename; 68 } 69 70 75 public final String getBaseName() 76 { 77 return basename; 78 } 79 80 85 private String makePropertyName(final String suffix) 86 { 87 return basename + PropertyMap.PROPERTY_NAME_SEPARATOR + suffix; 88 } 89 90 95 private String makePropertyName(final Object suffix) 96 { 97 return makePropertyName(String.valueOf(suffix)); 98 } 99 100 106 public boolean containsKey(final Object name) 107 { 108 if (name == null) 109 throw new NullArgumentException("name"); 110 111 return super.containsKey(makePropertyName(name)); 112 } 113 114 121 public Object put(final Object name, final Object value) 122 { 123 if (name == null) 124 throw new NullArgumentException("name"); 125 126 return super.put(makePropertyName(name), value); 127 } 128 129 135 public Object get(final Object name) 136 { 137 if (name == null) 138 throw new NullArgumentException("name"); 139 140 return super.get(makePropertyName(name)); 141 } 142 143 149 public Object remove(final Object name) 150 { 151 if (name == null) 152 throw new NullArgumentException("name"); 153 154 return super.remove(makePropertyName(name)); 155 } 156 157 164 public Set entrySet() 165 { 166 final Set superSet = super.entrySet(true); 167 168 return new java.util.AbstractSet () 169 { 170 private boolean isInGroup(Map.Entry entry) 171 { 172 String key = (String ) entry.getKey(); 173 return key.startsWith(basename); 174 } 175 176 public int size() 177 { 178 Iterator iter = superSet.iterator(); 179 int count = 0; 180 while (iter.hasNext()) 181 { 182 Map.Entry entry = (Map.Entry ) iter.next(); 183 if (isInGroup(entry)) 184 { 185 count++; 186 } 187 } 188 189 return count; 190 } 191 192 public Iterator iterator() 193 { 194 return new Iterator () 195 { 196 private Iterator iter = superSet.iterator(); 197 198 private Object next; 199 200 public boolean hasNext() 201 { 202 if (next != null) 203 return true; 204 205 while (next == null) 206 { 207 if (iter.hasNext()) 208 { 209 Map.Entry entry = (Map.Entry ) iter.next(); 210 if (isInGroup(entry)) 211 { 212 next = entry; 213 return true; 214 } 215 } 216 else 217 { 218 break; 219 } 220 } 221 222 return false; 223 } 224 225 public Object next() 226 { 227 if (next == null) 228 throw new java.util.NoSuchElementException (); 229 230 Object obj = next; 231 next = null; 232 233 return obj; 234 } 235 236 public void remove() 237 { 238 iter.remove(); 239 } 240 }; 241 } 242 }; 243 } 244 245 253 protected void addPropertyListener(final BoundPropertyListener listener) 254 { 255 String name = makePropertyName(listener.getPropertyName()); 257 258 List list = (List ) boundListeners.get(name); 260 261 if (list == null) 263 { 264 list = new ArrayList (); 265 boundListeners.put(name, list); 266 } 267 268 if (!list.contains(listener)) 270 { 271 list.add(listener); 272 listener.propertyBound(this); 274 } 275 } 276 277 286 protected boolean removePropertyListener(final BoundPropertyListener listener) 287 { 288 String name = makePropertyName(listener.getPropertyName()); 290 291 List list = (List ) boundListeners.get(name); 293 boolean removed = false; 294 if (list != null) 295 { 296 removed = list.remove(listener); 297 298 if (removed) 300 listener.propertyUnbound(this); 301 } 302 303 return removed; 304 } 305 } 306 | Popular Tags |