Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / AVListImpl.java
blobb2367e94714cc3549ec5036625cceca963fdcda2
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 /**
10 * An implementation class for the {@link AVList} interface. Classes implementing <code>AVList</code> can subclass or
11 * aggreate this class to provide default <code>AVList</code> functionality. This class maintains a hash table of
12 * attribute-value pairs.
13 * <p/>
14 * This class implements a notification mechanism for attribute-value changes. The mechanism provides a means for
15 * objects to observe attribute changes or queries for certain keys without explicitly monitoring all keys. See {@link
16 * java.beans.PropertyChangeSupport}.
18 * @author Tom Gaskins
19 * @version $Id: AVListImpl.java 1742 2007-05-06 16:34:29Z tgaskins $
21 public class AVListImpl implements AVList, java.beans.PropertyChangeListener
23 // TODO: Make thread-safe
24 /**
25 * Available to sub-classes for further exposure of property-change functionality.
27 protected final java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);
29 // To avoid unnecessary overhead, this object's hash map is created only if needed.
30 private java.util.Map<String, Object> avList;
32 /**
33 * Creates an empty attribute-value list.
35 public AVListImpl()
39 private boolean hasAvList()
41 return this.avList != null;
44 private void createAvList()
46 if (!this.hasAvList())
48 this.avList = new java.util.HashMap<String, Object>();
52 private java.util.Map<String, Object> avList(boolean createIfNone)
54 if (createIfNone && !this.hasAvList())
55 this.createAvList();
57 return this.avList;
60 public final Object getValue(String key)
62 if (key == null)
64 String message = WorldWind.retrieveErrMsg("nullValue.AttributeKeyIsNull");
65 WorldWind.logger().log(java.util.logging.Level.FINE, message);
66 throw new IllegalArgumentException(message);
69 if (this.hasAvList())
70 return this.avList.get(key);
72 return null;
75 public final String getStringValue(String key)
77 if (key == null)
79 String msg = WorldWind.retrieveErrMsg("nullValue.AttributeKeyIsNull");
80 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
81 throw new IllegalStateException(msg);
83 try
85 return (String) this.getValue(key);
87 catch (ClassCastException e)
89 String msg = WorldWind.retrieveErrMsg("AVAAccessibleImpl.AttributeValueForKeyIsNotAString") + key;
91 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
92 throw new WWRuntimeException(msg, e);
96 public final void setValue(String key, Object value)
98 if (key == null)
100 String message = WorldWind.retrieveErrMsg("nullValue.AttributeKeyIsNull");
101 WorldWind.logger().log(java.util.logging.Level.FINE, message);
102 throw new IllegalArgumentException(message);
104 // Capture the existing value if there is one, then set the new value.
105 this.avList(true).put(key, value);
108 public final boolean hasKey(String key)
110 if (key == null)
112 String message = WorldWind.retrieveErrMsg("nullValue.KeyIsNull");
113 WorldWind.logger().log(java.util.logging.Level.FINE, message);
114 throw new IllegalArgumentException(message);
117 return this.hasAvList() && this.avList.containsKey(key);
120 public final void removeKey(String key)
122 if (key == null)
124 String message = WorldWind.retrieveErrMsg("nullValue.KeyIsNull");
125 WorldWind.logger().log(java.util.logging.Level.FINE, message);
126 throw new IllegalArgumentException(message);
129 if (this.hasKey(key))
130 this.avList.remove(key);
133 public AVList copy()
135 AVListImpl clone = new AVListImpl();
137 clone.createAvList();
138 clone.avList.putAll(this.avList);
140 return clone;
143 public void addPropertyChangeListener(String propertyName, java.beans.PropertyChangeListener listener)
145 if (propertyName == null)
147 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyNameIsNull");
148 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
149 throw new IllegalArgumentException(msg);
151 if (listener == null)
153 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
154 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
155 throw new IllegalArgumentException(msg);
157 this.changeSupport.addPropertyChangeListener(propertyName, listener);
160 public void removePropertyChangeListener(String propertyName, java.beans.PropertyChangeListener listener)
162 if (propertyName == null)
164 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyNameIsNull");
165 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
166 throw new IllegalArgumentException(msg);
168 if (listener == null)
170 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
171 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
172 throw new IllegalArgumentException(msg);
174 this.changeSupport.removePropertyChangeListener(propertyName, listener);
177 public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
179 if (listener == null)
181 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
182 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
183 throw new IllegalArgumentException(msg);
185 this.changeSupport.addPropertyChangeListener(listener);
188 public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
190 if (listener == null)
192 String msg = WorldWind.retrieveErrMsg("nullValue.ListenerIsNull");
193 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
194 throw new IllegalArgumentException(msg);
196 this.changeSupport.removePropertyChangeListener(listener);
199 public void firePropertyChange(java.beans.PropertyChangeEvent propertyChangeEvent)
201 if (propertyChangeEvent == null)
203 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyChangeEventIsNull");
204 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
205 throw new IllegalArgumentException(msg);
207 this.changeSupport.firePropertyChange(propertyChangeEvent);
210 public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
212 if (propertyName == null)
214 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyNameIsNull");
215 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
216 throw new IllegalArgumentException(msg);
218 this.changeSupport.firePropertyChange(propertyName, oldValue, newValue);
222 * The property change listener for <em>this</em> instance.
223 * Recieves property change notifications that this instance has registered with other proprty change notifiers.
224 * @param propertyChangeEvent the event
225 * @throws IllegalArgumentException if <code>propertyChangeEvent</code> is null
227 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent)
229 if (propertyChangeEvent == null)
231 String msg = WorldWind.retrieveErrMsg("nullValue.PropertyChangeEventIsNull");
232 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
233 throw new IllegalArgumentException(msg);
236 // Notify all *my* listeners of the change that I caught
237 this.changeSupport.firePropertyChange(propertyChangeEvent);