1 44 45 package org.jfree.xml.factory.objects; 46 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 50 import org.jfree.util.Configuration; 51 import org.jfree.util.ClassComparator; 52 57 public abstract class ClassFactoryImpl implements ClassFactory { 58 59 60 private HashMap classes; 61 62 private ClassComparator comparator; 63 64 private Configuration config; 65 66 69 public ClassFactoryImpl() { 70 this.classes = new HashMap (); 71 this.comparator = new ClassComparator(); 72 } 73 74 79 public ClassComparator getComparator() { 80 return this.comparator; 81 } 82 83 90 public ObjectDescription getDescriptionForClass(final Class c) { 91 final ObjectDescription od = (ObjectDescription) this.classes.get(c); 92 if (od == null) { 93 return null; 94 } 95 return od.getInstance(); 96 } 97 98 107 public ObjectDescription getSuperClassObjectDescription 108 (final Class d, ObjectDescription knownSuperClass) { 109 110 if (d == null) { 111 throw new NullPointerException ("Description class must not be null."); 112 } 113 final Iterator iterator = this.classes.keySet().iterator(); 114 while (iterator.hasNext()) { 115 final Class keyClass = (Class ) iterator.next(); 116 if (keyClass.isAssignableFrom(d)) { 117 final ObjectDescription od = (ObjectDescription) this.classes.get(keyClass); 118 if (knownSuperClass == null) { 119 knownSuperClass = od; 120 } 121 else { 122 if (this.comparator.isComparable 123 (knownSuperClass.getObjectClass(), od.getObjectClass())) { 124 if (this.comparator.compare 125 (knownSuperClass.getObjectClass(), od.getObjectClass()) < 0) { 126 knownSuperClass = od; 127 } 128 } 129 } 130 } 131 } 132 if (knownSuperClass == null) { 133 return null; 134 } 135 return knownSuperClass.getInstance(); 136 } 137 138 144 protected void registerClass(final Class key, final ObjectDescription od) { 145 this.classes.put(key, od); 146 if (this.config != null) { 147 od.configure(this.config); 148 } 149 } 150 151 156 public Iterator getRegisteredClasses() { 157 return this.classes.keySet().iterator(); 158 } 159 160 161 170 public void configure(final Configuration config) { 171 if (config == null) { 172 throw new NullPointerException ("The given configuration is null"); 173 } 174 if (this.config != null) { 175 return; 177 } 178 179 this.config = config; 180 final Iterator it = this.classes.values().iterator(); 181 while (it.hasNext()) { 182 final ObjectDescription od = (ObjectDescription) it.next(); 183 od.configure(config); 184 } 185 } 186 187 192 public Configuration getConfig() { 193 return this.config; 194 } 195 196 203 public boolean equals(final Object o) { 204 if (this == o) { 205 return true; 206 } 207 if (!(o instanceof ClassFactoryImpl)) { 208 return false; 209 } 210 211 final ClassFactoryImpl classFactory = (ClassFactoryImpl) o; 212 213 if (!this.classes.equals(classFactory.classes)) { 214 return false; 215 } 216 217 return true; 218 } 219 220 225 public int hashCode() { 226 return this.classes.hashCode(); 227 } 228 } 229 | Popular Tags |