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: * ClassFactoryCollector.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: ClassFactoryCollector.java,v 1.6 2005/11/14 11:00:23 mungady 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: * 03-Jun-2003 : Adding factories configures the new factory.
042: * 29-Jul-2004 : Replaced 'enum' variable name (reserved word in JDK 1.5) (DG);
043: */
044:
045: package org.jfree.xml.factory.objects;
046:
047: import java.util.ArrayList;
048: import java.util.Iterator;
049:
050: import org.jfree.util.Configuration;
051:
052: /**
053: * A class factory collector.
054: *
055: * @author Thomas Morgner
056: */
057: public class ClassFactoryCollector extends ClassFactoryImpl {
058:
059: /** Storage for the class factories. */
060: private ArrayList factories;
061:
062: /**
063: * Creates a new class factory collector.
064: */
065: public ClassFactoryCollector() {
066: this .factories = new ArrayList();
067: }
068:
069: /**
070: * Adds a class factory to the collection.
071: *
072: * @param factory the factory.
073: */
074: public void addFactory(final ClassFactory factory) {
075: this .factories.add(factory);
076: if (getConfig() != null) {
077: factory.configure(getConfig());
078: }
079: }
080:
081: /**
082: * Returns an iterator the provides access to all the factories in the collection.
083: *
084: * @return The iterator.
085: */
086: public Iterator getFactories() {
087: return this .factories.iterator();
088: }
089:
090: /**
091: * Returns an object description for a class.
092: *
093: * @param c the class.
094: *
095: * @return The object description.
096: */
097: public ObjectDescription getDescriptionForClass(final Class c) {
098: for (int i = 0; i < this .factories.size(); i++) {
099: final ClassFactory f = (ClassFactory) this .factories.get(i);
100: final ObjectDescription od = f.getDescriptionForClass(c);
101: if (od != null) {
102: return od;
103: }
104: }
105: return super .getDescriptionForClass(c);
106: }
107:
108: /**
109: * Returns an object-description for the super class of a class.
110: *
111: * @param d the class.
112: * @param knownSuperClass the last known super class or null.
113: * @return The object description.
114: */
115: public ObjectDescription getSuperClassObjectDescription(
116: final Class d, ObjectDescription knownSuperClass) {
117: for (int i = 0; i < this .factories.size(); i++) {
118: final ClassFactory f = (ClassFactory) this .factories.get(i);
119: final ObjectDescription od = f
120: .getSuperClassObjectDescription(d, knownSuperClass);
121: if (od != null) {
122: if (knownSuperClass == null) {
123: knownSuperClass = od;
124: } else {
125: if (getComparator().isComparable(
126: knownSuperClass.getObjectClass(),
127: od.getObjectClass())) {
128: if (getComparator().compare(
129: knownSuperClass.getObjectClass(),
130: od.getObjectClass()) < 0) {
131: knownSuperClass = od;
132: }
133: }
134: }
135: }
136: }
137: return super .getSuperClassObjectDescription(d, knownSuperClass);
138: }
139:
140: /**
141: * Returns an iterator that provices access to the registered classes.
142: *
143: * @return The iterator.
144: */
145: public Iterator getRegisteredClasses() {
146: final ArrayList list = new ArrayList();
147: for (int i = 0; i < this .factories.size(); i++) {
148: final ClassFactory f = (ClassFactory) this .factories.get(i);
149: final Iterator iterator = f.getRegisteredClasses();
150: while (iterator.hasNext()) {
151: list.add(iterator.next());
152: }
153: }
154: return list.iterator();
155: }
156:
157: /**
158: * Configures this factory. The configuration contains several keys and
159: * their defined values. The given reference to the configuration object
160: * will remain valid until the report parsing or writing ends.
161: * <p>
162: * The configuration contents may change during the reporting.
163: *
164: * @param config the configuration, never null
165: */
166: public void configure(final Configuration config) {
167: if (getConfig() != null) {
168: // already configured ...
169: return;
170: }
171: super .configure(config);
172:
173: final Iterator it = this .factories.iterator();
174: while (it.hasNext()) {
175: final ClassFactory od = (ClassFactory) it.next();
176: od.configure(config);
177: }
178: }
179:
180: /**
181: * Tests for equality.
182: *
183: * @param o the object to test.
184: *
185: * @return A boolean.
186: */
187: public boolean equals(final Object o) {
188: if (this == o) {
189: return true;
190: }
191: if (!(o instanceof ClassFactoryCollector)) {
192: return false;
193: }
194: if (!super .equals(o)) {
195: return false;
196: }
197:
198: final ClassFactoryCollector classFactoryCollector = (ClassFactoryCollector) o;
199:
200: if (!this .factories.equals(classFactoryCollector.factories)) {
201: return false;
202: }
203:
204: return true;
205: }
206:
207: /**
208: * Returns a hash code for the object.
209: *
210: * @return The hash code.
211: */
212: public int hashCode() {
213: int result = super .hashCode();
214: result = 29 * result + this.factories.hashCode();
215: return result;
216: }
217: }
|