Worldwind public release 0.2.1
[worldwind-tracker.git] / gov / nasa / worldwind / AVListImpl.java
blobc164873b823e76218b7af433c3d4c987544d6f6d
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind;
9 import java.util.*;
11 /**
12 * An implementation class for the {@link AVList} interface. Classes implementing <code>AVList</code> can subclass or
13 * aggreate this class to provide default <code>AVList</code> functionality. This class maintains a hash table of
14 * attribute-value pairs.
15 * <p/>
16 * This class implements a notification mechanism for attribute-value changes. The mechanism provides a means for
17 * objects to observe attribute changes or queries for certain keys without explicitly monitoring all keys. See {@link
18 * java.beans.PropertyChangeSupport}.
20 * @author Tom Gaskins
21 * @version $Id: AVListImpl.java 2215 2007-07-04 15:38:23Z tgaskins $
23 public class AVListImpl implements AVList, java.beans.PropertyChangeListener
25 // TODO: Make thread-safe
26 /**
27 * Available to sub-classes for further exposure of property-change functionality.
29 protected final java.beans.PropertyChangeSupport changeSupport;// = new java.beans.PropertyChangeSupport(this);
31 // To avoid unnecessary overhead, this object's hash map is created only if needed.
32 private java.util.Map<String, Object> avList;
34 /**
35 * Creates an empty attribute-value list.
37 public AVListImpl()
39 this.changeSupport = new java.beans.PropertyChangeSupport(this);
42 /**
43 * Constructor enabling aggregation
44 * @param sourceBean The bean to be given as the soruce for any events.
46 public AVListImpl(Object sourceBean)
48 // TODO: check arg for non-null
49 this.changeSupport = new java.beans.PropertyChangeSupport(sourceBean);
52 private boolean hasAvList()
54 return this.avList != null;
57 private void createAvList()
59 if (!this.hasAvList())
61 this.avList = new java.util.HashMap<String, Object>();
65 private java.util.Map<String, Object> avList(boolean createIfNone)
67 if (createIfNone && !this.hasAvList())
68 this.createAvList();
70 return this.avList;
73 public final Object getValue(String key)
75 if (key == null)
77 String message = WorldWind.retrieveErrMsg("nullValue.AttributeKeyIsNull");
78 WorldWind.logger().log(java.util.logging.Level.FINE, message);
79 throw new IllegalArgumentException(message);
82 if (this.hasAvList())
83 return this.avList.get(key);
85 return null;
88 public Set<Map.Entry<String, Object>> getValues()
90 return this.hasAvList() ? this.avList.entrySet() : null;
93 public final String getStringValue(String key)
95 if (key == null)
97 String msg = WorldWind.retrieveErrMsg("nullValue.AttributeKeyIsNull");
98 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
99 throw new IllegalStateException(msg);
103 return (String) this.getValue(key);
105 catch (ClassCastException e)
107 String msg = WorldWind.retrieveErrMsg("AVAAccessibleImpl.AttributeValueForKeyIsNotAString") + key;
109 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
110 throw new WWRuntimeException(msg, e);
114 public final void setValue(String key, Object value)
116 if (key == null)
118 String message = WorldWind.retrieveErrMsg("nullValue.AttributeKeyIsNull");
119 WorldWind.logger().log(java.util.logging.Level.FINE, message);
120 throw new IllegalArgumentException(message);
122 // Capture the existing value if there is one, then set the new value.
123 this.avList(true).put(key, value);
126 public final void setValues(AVList list)
128 if (list == null)
130 String message = WorldWind.retrieveErrMsg("nullValue.AttributesIsNull");
131 WorldWind.logger().log(java.util.logging.Level.FINE, message);
132 throw new IllegalArgumentException(message);
135 Set<Map.Entry<String, Object>> entries = list.getValues();
136 for (Map.Entry<String, Object> entry : entries)
137 this.setValue(entry.getKey(), entry.getValue());
140 public final boolean hasKey(String key)
142 if (key == null)
144 String message = WorldWind.retrieveErrMsg("nullValue.KeyIsNull");
145 WorldWind.logger().log(java.util.logging.Level.FINE, message);
146 throw new IllegalArgumentException(message);
149 return this.hasAvList() && this.avList.containsKey(key);
152 public final void removeKey(String key)
154 if (key == null)
156 String message = WorldWind.retrieveErrMsg("nullValue.KeyIsNull");
157 WorldWind.logger().log(java.util.logging.Level.FINE, message);
158 throw new IllegalArgumentException(message);
161 if (this.hasKey(key))
162 this.avList.remove(key);
165 public AVList copy()
167 AVListImpl clone = new AVListImpl();
169 clone.createAvList();
170 clone.avList.putAll(this.avList);
172 return clone;
175 public void addPropertyChangeListener(String propertyName, java.beans.PropertyChangeListener listener)
177 if (propertyName == null)
179 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyNameIsNull");
180 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
181 throw new IllegalArgumentException(msg);
183 if (listener == null)
185 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
186 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
187 throw new IllegalArgumentException(msg);
189 this.changeSupport.addPropertyChangeListener(propertyName, listener);
192 public void removePropertyChangeListener(String propertyName, java.beans.PropertyChangeListener listener)
194 if (propertyName == null)
196 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyNameIsNull");
197 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
198 throw new IllegalArgumentException(msg);
200 if (listener == null)
202 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
203 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
204 throw new IllegalArgumentException(msg);
206 this.changeSupport.removePropertyChangeListener(propertyName, listener);
209 public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
211 if (listener == null)
213 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
214 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
215 throw new IllegalArgumentException(msg);
217 this.changeSupport.addPropertyChangeListener(listener);
220 public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
222 if (listener == null)
224 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
225 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
226 throw new IllegalArgumentException(msg);
228 this.changeSupport.removePropertyChangeListener(listener);
231 public void firePropertyChange(java.beans.PropertyChangeEvent propertyChangeEvent)
233 if (propertyChangeEvent == null)
235 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyChangeEventIsNull");
236 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
237 throw new IllegalArgumentException(msg);
239 this.changeSupport.firePropertyChange(propertyChangeEvent);
242 public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
244 if (propertyName == null)
246 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyNameIsNull");
247 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
248 throw new IllegalArgumentException(msg);
250 this.changeSupport.firePropertyChange(propertyName, oldValue, newValue);
254 * The property change listener for <em>this</em> instance.
255 * Recieves property change notifications that this instance has registered with other proprty change notifiers.
256 * @param propertyChangeEvent the event
257 * @throws IllegalArgumentException if <code>propertyChangeEvent</code> is null
259 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent)
261 if (propertyChangeEvent == null)
263 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyChangeEventIsNull");
264 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
265 throw new IllegalArgumentException(msg);
268 // Notify all *my* listeners of the change that I caught
269 this.changeSupport.firePropertyChange(propertyChangeEvent);
273 // Static AVList utilities.
274 public static String getStringValue(AVList avList, String key, String defaultValue)
276 String v = getStringValue(avList, key);
277 return v != null ? v : defaultValue;
280 public static String getStringValue(AVList avList, String key)
284 return avList.getStringValue(key);
286 catch (Exception e)
288 return null;
292 public static Integer getIntegerValue(AVList avList, String key, Integer defaultValue)
294 Integer v = getIntegerValue(avList, key);
295 return v != null ? v : defaultValue;
298 public static Integer getIntegerValue(AVList avList, String key)
300 Object o = avList.getValue(key);
301 if (o == null)
302 return null;
304 if (o instanceof Integer)
305 return (Integer) o;
307 String v = getStringValue(avList, key);
308 if (v == null)
309 return null;
313 return Integer.parseInt(v);
315 catch (NumberFormatException e)
317 String message = WorldWind.retrieveErrMsg("Configuration.ConversionError") + v;
318 WorldWind.logger().log(java.util.logging.Level.FINE, message);
320 return null;
324 public static Long getLongValue(AVList avList, String key, Long defaultValue)
326 Long v = getLongValue(avList, key);
327 return v != null ? v : defaultValue;
330 public static Long getLongValue(AVList avList, String key)
332 Object o = avList.getValue(key);
333 if (o == null)
334 return null;
336 if (o instanceof Long)
337 return (Long) o;
339 String v = getStringValue(avList, key);
340 if (v == null)
341 return null;
345 return Long.parseLong(v);
347 catch (NumberFormatException e)
349 String message = WorldWind.retrieveErrMsg("Configuration.ConversionError") + v;
350 WorldWind.logger().log(java.util.logging.Level.FINE, message);
352 return null;
356 public static Double getDoubleValue(AVList avList, String key, Double defaultValue)
358 Double v = getDoubleValue(avList, key);
359 return v != null ? v : defaultValue;
362 public static Double getDoubleValue(AVList avList, String key)
364 Object o = avList.getValue(key);
365 if (o == null)
366 return null;
368 if (o instanceof Double)
369 return (Double) o;
371 String v = getStringValue(avList, key);
372 if (v == null)
373 return null;
377 return Double.parseDouble(v);
379 catch (NumberFormatException e)
381 String message = WorldWind.retrieveErrMsg("Configuration.ConversionError") + v;
382 WorldWind.logger().log(java.util.logging.Level.FINE, message);
384 return null;