Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / avlist / AVListImpl.java
blob99cbe940fdd3d09ea60d77bbf36b427252dddf41
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.avlist;
9 import gov.nasa.worldwind.exception.WWRuntimeException;
10 import gov.nasa.worldwind.util.Logging;
12 import java.util.*;
13 import java.util.logging.Level;
15 /**
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.
19 * <p/>
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}.
24 * @author Tom Gaskins
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
30 /**
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;
38 /**
39 * Creates an empty attribute-value list.
41 public AVListImpl()
43 this.changeSupport = new java.beans.PropertyChangeSupport(this);
46 /**
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>();
68 return this.avList;
71 private Map<String, Object> avList(boolean createIfNone)
73 if (createIfNone && !this.hasAvList())
74 this.createAvList();
76 return this.avList;
79 public final Object getValue(String key)
81 if (key == null)
83 String message = Logging.getMessage("nullValue.AttributeKeyIsNull");
84 Logging.logger().severe(message);
85 throw new IllegalArgumentException(message);
88 if (this.hasAvList())
89 return this.avList.get(key);
91 return null;
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)
106 if (key == null)
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)
126 if (key == null)
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)
138 if (list == null)
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)
152 if (key == null)
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)
164 if (key == null)
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);
175 public AVList copy()
177 AVListImpl clone = new AVListImpl();
179 clone.createAvList();
180 clone.avList.putAll(this.avList);
182 return clone;
185 public final AVList clearList()
187 if (this.hasAvList())
188 this.avList.clear();
189 return this;
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);
303 catch (Exception e)
305 return null;
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);
318 if (o == null)
319 return null;
321 if (o instanceof Integer)
322 return (Integer) o;
324 String v = getStringValue(avList, key);
325 if (v == null)
326 return null;
330 return Integer.parseInt(v);
332 catch (NumberFormatException e)
334 Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
335 return null;
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);
348 if (o == null)
349 return null;
351 if (o instanceof Long)
352 return (Long) o;
354 String v = getStringValue(avList, key);
355 if (v == null)
356 return null;
360 return Long.parseLong(v);
362 catch (NumberFormatException e)
364 Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
365 return null;
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);
378 if (o == null)
379 return null;
381 if (o instanceof Double)
382 return (Double) o;
384 String v = getStringValue(avList, key);
385 if (v == null)
386 return null;
390 return Double.parseDouble(v);
392 catch (NumberFormatException e)
394 Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
395 return null;