2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
.avlist
;
9 import gov
.nasa
.worldwind
.exception
.WWRuntimeException
;
10 import gov
.nasa
.worldwind
.util
.Logging
;
13 import java
.util
.logging
.Level
;
16 * An implementation class for the {@link AVList} interface. Classes implementing <code>AVList</code> can subclass or
17 * aggreate this class to provide default <code>AVList</code> functionality. This class maintains a hash table of
18 * attribute-value pairs.
20 * This class implements a notification mechanism for attribute-value changes. The mechanism provides a means for
21 * objects to observe attribute changes or queries for certain keys without explicitly monitoring all keys. See {@link
22 * java.beans.PropertyChangeSupport}.
25 * @version $Id: AVListImpl.java 2471 2007-07-31 21:50:57Z tgaskins $
27 public class AVListImpl
implements AVList
, java
.beans
.PropertyChangeListener
29 // TODO: Make thread-safe
31 * Available to sub-classes for further exposure of property-change functionality.
33 protected final java
.beans
.PropertyChangeSupport changeSupport
;// = new java.beans.PropertyChangeSupport(this);
35 // To avoid unnecessary overhead, this object's hash map is created only if needed.
36 private Map
<String
, Object
> avList
;
39 * Creates an empty attribute-value list.
43 this.changeSupport
= new java
.beans
.PropertyChangeSupport(this);
47 * Constructor enabling aggregation
48 * @param sourceBean The bean to be given as the soruce for any events.
50 public AVListImpl(Object sourceBean
)
52 // TODO: check arg for non-null
53 this.changeSupport
= new java
.beans
.PropertyChangeSupport(sourceBean
);
56 private boolean hasAvList()
58 return this.avList
!= null;
61 private Map
<String
, Object
> createAvList()
63 if (!this.hasAvList())
65 this.avList
= new java
.util
.HashMap
<String
, Object
>();
71 private Map
<String
, Object
> avList(boolean createIfNone
)
73 if (createIfNone
&& !this.hasAvList())
79 public final Object
getValue(String key
)
83 String message
= Logging
.getMessage("nullValue.AttributeKeyIsNull");
84 Logging
.logger().severe(message
);
85 throw new IllegalArgumentException(message
);
89 return this.avList
.get(key
);
94 public final Collection
<Object
> getValues()
96 return this.hasAvList() ?
this.avList
.values() : this.createAvList().values();
99 public Set
<Map
.Entry
<String
, Object
>> getEntries()
101 return this.hasAvList() ?
this.avList
.entrySet() : this.createAvList().entrySet();
104 public final String
getStringValue(String key
)
108 String msg
= Logging
.getMessage("nullValue.AttributeKeyIsNull");
109 Logging
.logger().severe(msg
);
110 throw new IllegalStateException(msg
);
114 return (String
) this.getValue(key
);
116 catch (ClassCastException e
)
118 String msg
= Logging
.getMessage("AVAAccessibleImpl.AttributeValueForKeyIsNotAString", key
);
119 Logging
.logger().severe(msg
);
120 throw new WWRuntimeException(msg
, e
);
124 public final void setValue(String key
, Object value
)
128 String message
= Logging
.getMessage("nullValue.AttributeKeyIsNull");
129 Logging
.logger().severe(message
);
130 throw new IllegalArgumentException(message
);
132 // Capture the existing value if there is one, then set the new value.
133 this.avList(true).put(key
, value
);
136 public final void setValues(AVList list
)
140 String message
= Logging
.getMessage("nullValue.AttributesIsNull");
141 Logging
.logger().severe(message
);
142 throw new IllegalArgumentException(message
);
145 Set
<Map
.Entry
<String
, Object
>> entries
= list
.getEntries();
146 for (Map
.Entry
<String
, Object
> entry
: entries
)
147 this.setValue(entry
.getKey(), entry
.getValue());
150 public final boolean hasKey(String key
)
154 String message
= Logging
.getMessage("nullValue.KeyIsNull");
155 Logging
.logger().severe(message
);
156 throw new IllegalArgumentException(message
);
159 return this.hasAvList() && this.avList
.containsKey(key
);
162 public final void removeKey(String key
)
166 String message
= Logging
.getMessage("nullValue.KeyIsNull");
167 Logging
.logger().severe(message
);
168 throw new IllegalArgumentException(message
);
171 if (this.hasKey(key
))
172 this.avList
.remove(key
);
177 AVListImpl clone
= new AVListImpl();
179 clone
.createAvList();
180 clone
.avList
.putAll(this.avList
);
185 public final AVList
clearList()
187 if (this.hasAvList())
192 public void addPropertyChangeListener(String propertyName
, java
.beans
.PropertyChangeListener listener
)
194 if (propertyName
== null)
196 String msg
= Logging
.getMessage("nullValue.PropertyNameIsNull");
197 Logging
.logger().severe(msg
);
198 throw new IllegalArgumentException(msg
);
200 if (listener
== null)
202 String msg
= Logging
.getMessage("nullValue.ListenerIsNull");
203 Logging
.logger().severe(msg
);
204 throw new IllegalArgumentException(msg
);
206 this.changeSupport
.addPropertyChangeListener(propertyName
, listener
);
209 public void removePropertyChangeListener(String propertyName
, java
.beans
.PropertyChangeListener listener
)
211 if (propertyName
== null)
213 String msg
= Logging
.getMessage("nullValue.PropertyNameIsNull");
214 Logging
.logger().severe(msg
);
215 throw new IllegalArgumentException(msg
);
217 if (listener
== null)
219 String msg
= Logging
.getMessage("nullValue.ListenerIsNull");
220 Logging
.logger().severe(msg
);
221 throw new IllegalArgumentException(msg
);
223 this.changeSupport
.removePropertyChangeListener(propertyName
, listener
);
226 public void addPropertyChangeListener(java
.beans
.PropertyChangeListener listener
)
228 if (listener
== null)
230 String msg
= Logging
.getMessage("nullValue.ListenerIsNull");
231 Logging
.logger().severe(msg
);
232 throw new IllegalArgumentException(msg
);
234 this.changeSupport
.addPropertyChangeListener(listener
);
237 public void removePropertyChangeListener(java
.beans
.PropertyChangeListener listener
)
239 if (listener
== null)
241 String msg
= Logging
.getMessage("nullValue.ListenerIsNull");
242 Logging
.logger().severe(msg
);
243 throw new IllegalArgumentException(msg
);
245 this.changeSupport
.removePropertyChangeListener(listener
);
248 public void firePropertyChange(java
.beans
.PropertyChangeEvent propertyChangeEvent
)
250 if (propertyChangeEvent
== null)
252 String msg
= Logging
.getMessage("nullValue.PropertyChangeEventIsNull");
253 Logging
.logger().severe(msg
);
254 throw new IllegalArgumentException(msg
);
256 this.changeSupport
.firePropertyChange(propertyChangeEvent
);
259 public void firePropertyChange(String propertyName
, Object oldValue
, Object newValue
)
261 if (propertyName
== null)
263 String msg
= Logging
.getMessage("nullValue.PropertyNameIsNull");
264 Logging
.logger().severe(msg
);
265 throw new IllegalArgumentException(msg
);
267 this.changeSupport
.firePropertyChange(propertyName
, oldValue
, newValue
);
271 * The property change listener for <em>this</em> instance.
272 * Recieves property change notifications that this instance has registered with other proprty change notifiers.
273 * @param propertyChangeEvent the event
274 * @throws IllegalArgumentException if <code>propertyChangeEvent</code> is null
276 public void propertyChange(java
.beans
.PropertyChangeEvent propertyChangeEvent
)
278 if (propertyChangeEvent
== null)
280 String msg
= Logging
.getMessage("nullValue.PropertyChangeEventIsNull");
281 Logging
.logger().severe(msg
);
282 throw new IllegalArgumentException(msg
);
285 // Notify all *my* listeners of the change that I caught
286 this.changeSupport
.firePropertyChange(propertyChangeEvent
);
290 // Static AVList utilities.
291 public static String
getStringValue(AVList avList
, String key
, String defaultValue
)
293 String v
= getStringValue(avList
, key
);
294 return v
!= null ? v
: defaultValue
;
297 public static String
getStringValue(AVList avList
, String key
)
301 return avList
.getStringValue(key
);
309 public static Integer
getIntegerValue(AVList avList
, String key
, Integer defaultValue
)
311 Integer v
= getIntegerValue(avList
, key
);
312 return v
!= null ? v
: defaultValue
;
315 public static Integer
getIntegerValue(AVList avList
, String key
)
317 Object o
= avList
.getValue(key
);
321 if (o
instanceof Integer
)
324 String v
= getStringValue(avList
, key
);
330 return Integer
.parseInt(v
);
332 catch (NumberFormatException e
)
334 Logging
.logger().log(Level
.SEVERE
, "Configuration.ConversionError", v
);
339 public static Long
getLongValue(AVList avList
, String key
, Long defaultValue
)
341 Long v
= getLongValue(avList
, key
);
342 return v
!= null ? v
: defaultValue
;
345 public static Long
getLongValue(AVList avList
, String key
)
347 Object o
= avList
.getValue(key
);
351 if (o
instanceof Long
)
354 String v
= getStringValue(avList
, key
);
360 return Long
.parseLong(v
);
362 catch (NumberFormatException e
)
364 Logging
.logger().log(Level
.SEVERE
, "Configuration.ConversionError", v
);
369 public static Double
getDoubleValue(AVList avList
, String key
, Double defaultValue
)
371 Double v
= getDoubleValue(avList
, key
);
372 return v
!= null ? v
: defaultValue
;
375 public static Double
getDoubleValue(AVList avList
, String key
)
377 Object o
= avList
.getValue(key
);
381 if (o
instanceof Double
)
384 String v
= getStringValue(avList
, key
);
390 return Double
.parseDouble(v
);
392 catch (NumberFormatException e
)
394 Logging
.logger().log(Level
.SEVERE
, "Configuration.ConversionError", v
);