1 9 package com.vladium.util; 10 11 import java.io.PrintStream ; 12 import java.io.PrintWriter ; 13 import java.util.ArrayList ; 14 import java.util.Enumeration ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Properties ; 19 import java.util.Set ; 20 import java.util.TreeSet ; 21 22 26 public 27 interface IProperties 28 { 29 31 37 interface IMapper 38 { 39 String getMappedKey (final String key); 40 41 } 43 44 String getProperty (String key); 45 String getProperty (String key, String dflt); 46 boolean isOverridden (String key); 47 48 IProperties copy (); 49 Iterator properties (); 50 Properties toProperties (); 51 54 String [] toAppArgsForm (final String prefix); 55 boolean isEmpty (); 56 void list (PrintStream out); 57 void list (PrintWriter out); 58 59 String setProperty (String key, String value); 60 61 62 abstract class Factory 63 { 64 70 public static IProperties create (final IMapper mapper) 71 { 72 return new PropertiesImpl (null, mapper); 73 } 74 75 85 public static IProperties wrap (final Properties properties, final IMapper mapper) 86 { 87 final HashMap map = new HashMap (); 88 89 91 for (Enumeration names = properties.propertyNames (); names.hasMoreElements (); ) 92 { 93 final String n = (String ) names.nextElement (); 94 final String v = properties.getProperty (n); 95 96 map.put (n, v); 97 } 98 99 return new PropertiesImpl (map, mapper); } 101 102 112 public static IProperties combine (final IProperties overrides, final IProperties base) 113 { 114 final IProperties result = overrides != null 115 ? overrides.copy () 116 : create (null); 117 118 120 ((PropertiesImpl) result).getLastProperties ().setDelegate ((PropertiesImpl) base); 121 122 return result; 123 } 124 125 128 private static final class PropertiesImpl implements IProperties, Cloneable 129 { 130 132 public String getProperty (final String key) 133 { 134 return getProperty (key, null); 135 } 136 137 public String getProperty (final String key, final String dflt) 138 { 139 String value = (String ) m_valueMap.get (key); 140 141 if ((value == null) && (m_mapper != null)) 143 { 144 final String mappedKey = m_mapper.getMappedKey (key); 145 146 if (mappedKey != null) 147 value = (String ) m_valueMap.get (mappedKey); 148 } 149 150 if ((value == null) && (m_delegate != null)) 152 { 153 value = m_delegate.getProperty (key, null); 154 } 155 156 return value != null ? value : dflt; 157 } 158 159 public boolean isOverridden (final String key) 160 { 161 return m_valueMap.containsKey (key); 162 } 163 164 public IProperties copy () 165 { 166 final PropertiesImpl _clone; 167 try 168 { 169 _clone = (PropertiesImpl) super.clone (); 170 } 171 catch (CloneNotSupportedException cnse) 172 { 173 throw new Error (cnse.toString ()); } 175 176 _clone.m_valueMap = (HashMap ) m_valueMap.clone (); 177 _clone.m_unmappedKeySet = null; 178 179 181 PropertiesImpl scan = _clone; 182 for (PropertiesImpl delegate = m_delegate; delegate != null; delegate = delegate.m_delegate) 183 { 184 final PropertiesImpl _delegateClone; 185 try 186 { 187 _delegateClone = (PropertiesImpl) delegate.clone (); 188 } 189 catch (CloneNotSupportedException cnse) 190 { 191 throw new Error (cnse.toString ()); } 193 194 198 _delegateClone.m_valueMap = (HashMap ) delegate.m_valueMap.clone (); 199 _delegateClone.m_unmappedKeySet = null; 201 scan.setDelegate (_delegateClone); 202 scan = _delegateClone; 203 } 204 205 return _clone; 206 } 207 208 public Iterator properties () 209 { 210 return unmappedKeySet ().iterator (); 211 } 212 213 public Properties toProperties () 214 { 215 final Properties result = new Properties (); 216 217 for (Iterator i = properties (); i.hasNext (); ) 218 { 219 final String n = (String ) i.next (); 220 final String v = getProperty (n); 221 222 result.setProperty (n, v); 223 } 224 225 return result; 226 } 227 228 public boolean isEmpty () 229 { 230 return m_valueMap.isEmpty () && ((m_delegate == null) || ((m_delegate != null) && m_delegate.isEmpty ())); 231 } 232 233 public String [] toAppArgsForm (final String prefix) 234 { 235 if (isEmpty ()) 236 return IConstants.EMPTY_STRING_ARRAY; 237 else 238 { 239 if (prefix == null) 240 throw new IllegalArgumentException ("null input: prefix"); 241 242 final List _result = new ArrayList (); 243 for (Iterator names = properties (); names.hasNext (); ) 244 { 245 final String name = (String ) names.next (); 246 final String value = getProperty (name, ""); 247 248 _result.add (prefix.concat (name).concat ("=").concat (value)); 249 } 250 251 final String [] result = new String [_result.size ()]; 252 _result.toArray (result); 253 254 return result; 255 } 256 } 257 258 public void list (final PrintStream out) 259 { 260 if (out != null) 261 { 262 for (Iterator i = properties (); i.hasNext (); ) 263 { 264 final String n = (String ) i.next (); 265 final String v = getProperty (n); 266 267 out.println (n + ":\t[" + v + "]"); 268 } 269 } 270 } 271 272 public void list (final PrintWriter out) 273 { 274 if (out != null) 275 { 276 for (Iterator i = properties (); i.hasNext (); ) 277 { 278 final String n = (String ) i.next (); 279 final String v = getProperty (n); 280 281 out.println (n + ":\t[" + v + "]"); 282 } 283 } 284 } 285 286 288 public String setProperty (final String key, final String value) 289 { 290 if (value == null) 291 throw new IllegalArgumentException ("null input: value"); 292 293 m_unmappedKeySet = null; 294 295 return (String ) m_valueMap.put (key, value); 296 } 297 298 299 PropertiesImpl (final HashMap values, final IMapper mapper) 300 { 301 m_mapper = mapper; 302 m_valueMap = values != null ? values : new HashMap (); 303 304 m_delegate = null; 305 } 306 307 308 Set unmappedKeySet () 309 { 310 Set result = m_unmappedKeySet; 311 if (result == null) 312 { 313 result = new TreeSet (); 314 result.addAll (m_valueMap.keySet ()); 315 if (m_delegate != null) 316 result.addAll (m_delegate.unmappedKeySet ()); 317 318 m_unmappedKeySet = result; 319 return result; 320 } 321 322 return result; 323 } 324 325 327 PropertiesImpl getLastProperties () 328 { 329 PropertiesImpl result = this; 330 331 for (PropertiesImpl delegate = m_delegate; delegate != null; delegate = delegate.m_delegate) 332 { 333 if (delegate == this) 335 throw new IllegalStateException ("cyclic delegation detected"); 336 337 result = delegate; 338 } 339 340 return result; 341 } 342 343 345 void setDelegate (final PropertiesImpl delegate) 346 { 347 m_delegate = delegate; 348 349 m_unmappedKeySet = null; 350 } 351 352 353 private final IMapper m_mapper; 354 private HashMap m_valueMap; 356 private PropertiesImpl m_delegate; 357 private transient Set m_unmappedKeySet; 358 359 } 361 } 363 365 367 369 } | Popular Tags |