1 43 44 package org.jfree.xml.factory.objects; 45 46 import java.util.ArrayList ; 47 import java.util.Collections ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 51 import org.jfree.util.Configuration; 52 import org.jfree.util.Log; 53 import org.jfree.util.ReadOnlyIterator; 54 55 60 public abstract class AbstractObjectDescription implements ObjectDescription, Cloneable { 61 62 63 private Class className; 64 65 66 private HashMap parameters; 67 68 69 private HashMap parameterDefs; 70 71 72 private Configuration config; 73 74 79 public AbstractObjectDescription(final Class className) { 80 this.className = className; 81 this.parameters = new HashMap (); 82 this.parameterDefs = new HashMap (); 83 } 84 85 92 public Class getParameterDefinition(final String name) { 93 return (Class ) this.parameterDefs.get(name); 94 } 95 96 102 public void setParameterDefinition(final String name, final Class obj) { 103 if (obj == null) { 104 this.parameterDefs.remove(name); 105 } 106 else { 107 this.parameterDefs.put(name, obj); 108 } 109 } 110 111 118 public static Class convertPrimitiveClass(final Class obj) { 119 if (!obj.isPrimitive()) { 120 return obj; 121 } 122 if (obj == Boolean.TYPE) { 123 return Boolean .class; 124 } 125 if (obj == Byte.TYPE) { 126 return Byte .class; 127 } 128 if (obj == Character.TYPE) { 129 return Character .class; 130 } 131 if (obj == Short.TYPE) { 132 return Short .class; 133 } 134 if (obj == Integer.TYPE) { 135 return Integer .class; 136 } 137 if (obj == Long.TYPE) { 138 return Long .class; 139 } 140 if (obj == Float.TYPE) { 141 return Float .class; 142 } 143 if (obj == Double.TYPE) { 144 return Double .class; 145 } 146 throw new IllegalArgumentException ("Class 'void' is not allowed here"); 147 } 148 149 155 public void setParameter(final String name, final Object value) { 156 if (getParameterDefinition(name) == null) { 157 throw new IllegalArgumentException ("No such Parameter defined: " + name 158 + " in class " + getObjectClass()); 159 } 160 final Class parameterClass = convertPrimitiveClass(getParameterDefinition(name)); 161 if (!parameterClass.isAssignableFrom(value.getClass())) { 162 throw new ClassCastException ("In Object " + getObjectClass() 163 + ": Value is not assignable: " + value.getClass() 164 + " is not assignable from " + parameterClass); 165 } 166 this.parameters.put(name, value); 167 } 168 169 174 public synchronized Iterator getParameterNames() { 175 final ArrayList parameterNames = new ArrayList (this.parameterDefs.keySet()); 176 Collections.sort(parameterNames); 177 return new ReadOnlyIterator (parameterNames.iterator()); 178 } 179 180 185 protected Iterator getDefinedParameterNames() { 186 return new ReadOnlyIterator (this.parameters.keySet().iterator()); 187 } 188 189 196 public Object getParameter(final String name) { 197 return this.parameters.get(name); 198 } 199 200 205 public Class getObjectClass() { 206 return this.className; 207 } 208 209 221 public ObjectDescription getInstance() { 222 try { 223 final AbstractObjectDescription c = (AbstractObjectDescription) super.clone(); 224 c.parameters = (HashMap ) this.parameters.clone(); 225 return c; 226 } 227 catch (Exception e) { 228 Log.error("Should not happen: Clone Error: ", e); 229 return null; 230 } 231 } 232 233 234 246 public ObjectDescription getUnconfiguredInstance() { 247 try { 248 final AbstractObjectDescription c = (AbstractObjectDescription) super.clone(); 249 c.parameters = (HashMap ) this.parameters.clone(); 250 c.config = null; 251 return c; 252 } 253 catch (Exception e) { 254 Log.error("Should not happen: Clone Error: ", e); 255 return null; 256 } 257 } 258 259 268 public void configure(final Configuration config) { 269 if (config == null) { 270 throw new NullPointerException ("The given configuration is null"); 271 } 272 this.config = config; 273 } 274 275 280 public Configuration getConfig() { 281 return this.config; 282 } 283 284 291 public boolean equals(final Object o) { 292 if (this == o) { 293 return true; 294 } 295 if (!(o instanceof AbstractObjectDescription)) { 296 return false; 297 } 298 299 final AbstractObjectDescription abstractObjectDescription = (AbstractObjectDescription) o; 300 301 if (!this.className.equals(abstractObjectDescription.className)) { 302 return false; 303 } 304 305 return true; 306 } 307 308 313 public int hashCode() { 314 return this.className.hashCode(); 315 } 316 } 317 | Popular Tags |