001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------
028: * ClassFactoryImpl.java
029: * ---------------------
030: * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ClassFactoryImpl.java,v 1.6 2006/01/25 23:15:03 taqua Exp $
036: *
037: * Changes (from 19-Feb-2003)
038: * -------------------------
039: * 19-Feb-2003 : Added standard header and Javadocs (DG);
040: * 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon
041: * 29-Jul-2004 : Replaced 'enum' variable name (reserved word in JDK 1.5) (DG);
042: *
043: */
044:
045: package org.jfree.xml.factory.objects;
046:
047: import java.util.HashMap;
048: import java.util.Iterator;
049:
050: import org.jfree.util.Configuration;
051: import org.jfree.util.ClassComparator;
052:
053: /**
054: * An abstract class that implements the {@link ClassFactory} interface.
055: *
056: * @author Thomas Morgner.
057: */
058: public abstract class ClassFactoryImpl implements ClassFactory {
059:
060: /** Storage for the classes. */
061: private HashMap classes;
062: /** A class comparator for searching the super class */
063: private ClassComparator comparator;
064: /** The parser/report configuration */
065: private Configuration config;
066:
067: /**
068: * Creates a new class factory.
069: */
070: public ClassFactoryImpl() {
071: this .classes = new HashMap();
072: this .comparator = new ClassComparator();
073: }
074:
075: /**
076: * Returns the class comparator used to sort the super classes of an object.
077: *
078: * @return the class comparator.
079: */
080: public ClassComparator getComparator() {
081: return this .comparator;
082: }
083:
084: /**
085: * Returns an object-description for a class.
086: *
087: * @param c the class.
088: *
089: * @return An object description.
090: */
091: public ObjectDescription getDescriptionForClass(final Class c) {
092: final ObjectDescription od = (ObjectDescription) this .classes
093: .get(c);
094: if (od == null) {
095: return null;
096: }
097: return od.getInstance();
098: }
099:
100: /**
101: * Returns the most concrete object-description for the super class of a class.
102: *
103: * @param d the class.
104: * @param knownSuperClass a known supported superclass or null, if no superclass
105: * is known yet.
106: *
107: * @return The object description.
108: */
109: public ObjectDescription getSuperClassObjectDescription(
110: final Class d, ObjectDescription knownSuperClass) {
111:
112: if (d == null) {
113: throw new NullPointerException(
114: "Description class must not be null.");
115: }
116: final Iterator iterator = this .classes.keySet().iterator();
117: while (iterator.hasNext()) {
118: final Class keyClass = (Class) iterator.next();
119: if (keyClass.isAssignableFrom(d)) {
120: final ObjectDescription od = (ObjectDescription) this .classes
121: .get(keyClass);
122: if (knownSuperClass == null) {
123: knownSuperClass = od;
124: } else {
125: if (this .comparator.isComparable(knownSuperClass
126: .getObjectClass(), od.getObjectClass())) {
127: if (this .comparator.compare(knownSuperClass
128: .getObjectClass(), od.getObjectClass()) < 0) {
129: knownSuperClass = od;
130: }
131: }
132: }
133: }
134: }
135: if (knownSuperClass == null) {
136: return null;
137: }
138: return knownSuperClass.getInstance();
139: }
140:
141: /**
142: * Registers an object description with the factory.
143: *
144: * @param key the key.
145: * @param od the object description.
146: */
147: protected void registerClass(final Class key,
148: final ObjectDescription od) {
149: this .classes.put(key, od);
150: if (this .config != null) {
151: od.configure(this .config);
152: }
153: }
154:
155: /**
156: * Returns an iterator that provides access to the registered object definitions.
157: *
158: * @return The iterator.
159: */
160: public Iterator getRegisteredClasses() {
161: return this .classes.keySet().iterator();
162: }
163:
164: /**
165: * Configures this factory. The configuration contains several keys and
166: * their defined values. The given reference to the configuration object
167: * will remain valid until the report parsing or writing ends.
168: * <p>
169: * The configuration contents may change during the reporting.
170: *
171: * @param config the configuration, never null
172: */
173: public void configure(final Configuration config) {
174: if (config == null) {
175: throw new NullPointerException(
176: "The given configuration is null");
177: }
178: if (this .config != null) {
179: // already configured ... ignored
180: return;
181: }
182:
183: this .config = config;
184: final Iterator it = this .classes.values().iterator();
185: while (it.hasNext()) {
186: final ObjectDescription od = (ObjectDescription) it.next();
187: od.configure(config);
188: }
189: }
190:
191: /**
192: * Returns the currently set configuration or null, if none was set.
193: *
194: * @return the configuration.
195: */
196: public Configuration getConfig() {
197: return this .config;
198: }
199:
200: /**
201: * Tests for equality.
202: *
203: * @param o the object to test.
204: *
205: * @return A boolean.
206: */
207: public boolean equals(final Object o) {
208: if (this == o) {
209: return true;
210: }
211: if (!(o instanceof ClassFactoryImpl)) {
212: return false;
213: }
214:
215: final ClassFactoryImpl classFactory = (ClassFactoryImpl) o;
216:
217: if (!this .classes.equals(classFactory.classes)) {
218: return false;
219: }
220:
221: return true;
222: }
223:
224: /**
225: * Returns a hash code.
226: *
227: * @return A hash code.
228: */
229: public int hashCode() {
230: return this.classes.hashCode();
231: }
232: }
|