001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ClassFactoryCollector.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.parser.ext.factory.base;
030:
031: import java.util.ArrayList;
032: import java.util.Iterator;
033:
034: import org.jfree.util.Configuration;
035:
036: /**
037: * A class factory collector.
038: *
039: * @author Thomas Morgner
040: */
041: public class ClassFactoryCollector extends ClassFactoryImpl {
042:
043: /** Storage for the class factories. */
044: private ArrayList factories;
045:
046: /**
047: * Creates a new class factory collector.
048: */
049: public ClassFactoryCollector() {
050: this .factories = new ArrayList();
051: }
052:
053: /**
054: * Adds a class factory to the collection.
055: *
056: * @param factory the factory.
057: */
058: public void addFactory(final ClassFactory factory) {
059: this .factories.add(factory);
060: if (getConfig() != null) {
061: factory.configure(getConfig());
062: }
063: }
064:
065: /**
066: * Returns an iterator the provides access to all the factories in the collection.
067: *
068: * @return The iterator.
069: */
070: public Iterator getFactories() {
071: return this .factories.iterator();
072: }
073:
074: /**
075: * Returns an object description for a class.
076: *
077: * @param c the class.
078: *
079: * @return The object description.
080: */
081: public ObjectDescription getDescriptionForClass(final Class c) {
082: for (int i = 0; i < this .factories.size(); i++) {
083: final ClassFactory f = (ClassFactory) this .factories.get(i);
084: final ObjectDescription od = f.getDescriptionForClass(c);
085: if (od != null) {
086: return od;
087: }
088: }
089: return super .getDescriptionForClass(c);
090: }
091:
092: /**
093: * Returns an object-description for the super class of a class.
094: *
095: * @param d the class.
096: * @param knownSuperClass the last known super class or null.
097: * @return The object description.
098: */
099: public ObjectDescription getSuperClassObjectDescription(
100: final Class d, ObjectDescription knownSuperClass) {
101: for (int i = 0; i < this .factories.size(); i++) {
102: final ClassFactory f = (ClassFactory) this .factories.get(i);
103: final ObjectDescription od = f
104: .getSuperClassObjectDescription(d, knownSuperClass);
105: if (od != null) {
106: if (knownSuperClass == null) {
107: knownSuperClass = od;
108: } else {
109: if (getComparator().isComparable(
110: knownSuperClass.getObjectClass(),
111: od.getObjectClass())) {
112: if (getComparator().compare(
113: knownSuperClass.getObjectClass(),
114: od.getObjectClass()) < 0) {
115: knownSuperClass = od;
116: }
117: }
118: }
119: }
120: }
121: return super .getSuperClassObjectDescription(d, knownSuperClass);
122: }
123:
124: /**
125: * Returns an iterator that provices access to the registered classes.
126: *
127: * @return The iterator.
128: */
129: public Iterator getRegisteredClasses() {
130: final ArrayList list = new ArrayList();
131: for (int i = 0; i < this .factories.size(); i++) {
132: final ClassFactory f = (ClassFactory) this .factories.get(i);
133: final Iterator iterator = f.getRegisteredClasses();
134: while (iterator.hasNext()) {
135: list.add(iterator.next());
136: }
137: }
138: return list.iterator();
139: }
140:
141: /**
142: * Configures this factory. The configuration contains several keys and
143: * their defined values. The given reference to the configuration object
144: * will remain valid until the report parsing or writing ends.
145: * <p>
146: * The configuration contents may change during the reporting.
147: *
148: * @param config the configuration, never null
149: */
150: public void configure(final Configuration config) {
151: if (getConfig() != null) {
152: // already configured ...
153: return;
154: }
155: super .configure(config);
156:
157: final Iterator it = this .factories.iterator();
158: while (it.hasNext()) {
159: final ClassFactory od = (ClassFactory) it.next();
160: od.configure(config);
161: }
162: }
163:
164: /**
165: * Tests for equality.
166: *
167: * @param o the object to test.
168: *
169: * @return A boolean.
170: */
171: public boolean equals(final Object o) {
172: if (this == o) {
173: return true;
174: }
175: if (!(o instanceof ClassFactoryCollector)) {
176: return false;
177: }
178: if (!super .equals(o)) {
179: return false;
180: }
181:
182: final ClassFactoryCollector classFactoryCollector = (ClassFactoryCollector) o;
183:
184: if (!this .factories.equals(classFactoryCollector.factories)) {
185: return false;
186: }
187:
188: return true;
189: }
190:
191: /**
192: * Returns a hash code for the object.
193: *
194: * @return The hash code.
195: */
196: public int hashCode() {
197: int result = super .hashCode();
198: result = 29 * result + this.factories.hashCode();
199: return result;
200: }
201: }
|