Source for org.jfree.xml.factory.objects.ClassFactoryCollector

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * --------------------------
  28:  * ClassFactoryCollector.java
  29:  * --------------------------
  30:  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ClassFactoryCollector.java,v 1.6 2005/11/14 11:00:23 mungady Exp $
  36:  *
  37:  * Changes (from 19-Feb-2003)
  38:  * -------------------------
  39:  * 19-Feb-2003 : Added standard header and Javadocs (DG);
  40:  * 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon
  41:  * 03-Jun-2003 : Adding factories configures the new factory.
  42:  * 29-Jul-2004 : Replaced 'enum' variable name (reserved word in JDK 1.5) (DG);
  43:  */
  44: 
  45: package org.jfree.xml.factory.objects;
  46: 
  47: import java.util.ArrayList;
  48: import java.util.Iterator;
  49: 
  50: import org.jfree.util.Configuration;
  51: 
  52: /**
  53:  * A class factory collector.
  54:  *
  55:  * @author Thomas Morgner
  56:  */
  57: public class ClassFactoryCollector extends ClassFactoryImpl {
  58: 
  59:     /** Storage for the class factories. */
  60:     private ArrayList factories;
  61: 
  62:     /**
  63:      * Creates a new class factory collector.
  64:      */
  65:     public ClassFactoryCollector() {
  66:         this.factories = new ArrayList();
  67:     }
  68: 
  69:     /**
  70:      * Adds a class factory to the collection.
  71:      *
  72:      * @param factory  the factory.
  73:      */
  74:     public void addFactory(final ClassFactory factory) {
  75:         this.factories.add(factory);
  76:         if (getConfig() != null) {
  77:             factory.configure(getConfig());
  78:         }
  79:     }
  80: 
  81:     /**
  82:      * Returns an iterator the provides access to all the factories in the collection.
  83:      *
  84:      * @return The iterator.
  85:      */
  86:     public Iterator getFactories() {
  87:         return this.factories.iterator();
  88:     }
  89: 
  90:     /**
  91:      * Returns an object description for a class.
  92:      *
  93:      * @param c  the class.
  94:      *
  95:      * @return The object description.
  96:      */
  97:     public ObjectDescription getDescriptionForClass(final Class c) {
  98:         for (int i = 0; i < this.factories.size(); i++) {
  99:             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.getSuperClassObjectDescription(d, knownSuperClass);
 120:             if (od != null) {
 121:                 if (knownSuperClass == null) {
 122:                     knownSuperClass = od;
 123:                 }
 124:                 else {
 125:                     if (getComparator().isComparable(knownSuperClass.getObjectClass(),
 126:                         od.getObjectClass())) {
 127:                         if (getComparator().compare(knownSuperClass.getObjectClass(),
 128:                             od.getObjectClass()) < 0) {
 129:                             knownSuperClass = od;
 130:                         }
 131:                     }
 132:                 }
 133:             }
 134:         }
 135:         return super.getSuperClassObjectDescription(d, knownSuperClass);
 136:     }
 137: 
 138:     /**
 139:      * Returns an iterator that provices access to the registered classes.
 140:      *
 141:      * @return The iterator.
 142:      */
 143:     public Iterator getRegisteredClasses() {
 144:         final ArrayList list = new ArrayList();
 145:         for (int i = 0; i < this.factories.size(); i++) {
 146:             final ClassFactory f = (ClassFactory) this.factories.get(i);
 147:             final Iterator iterator = f.getRegisteredClasses();
 148:             while (iterator.hasNext()) {
 149:                 list.add(iterator.next());
 150:             }
 151:         }
 152:         return list.iterator();
 153:     }
 154: 
 155:     /**
 156:      * Configures this factory. The configuration contains several keys and
 157:      * their defined values. The given reference to the configuration object
 158:      * will remain valid until the report parsing or writing ends.
 159:      * <p>
 160:      * The configuration contents may change during the reporting.
 161:      *
 162:      * @param config the configuration, never null
 163:      */
 164:     public void configure(final Configuration config) {
 165:         if (getConfig() != null) {
 166:             // already configured ...
 167:             return;
 168:         }
 169:         super.configure(config);
 170: 
 171:         final Iterator it = this.factories.iterator();
 172:         while (it.hasNext()) {
 173:             final ClassFactory od = (ClassFactory) it.next();
 174:             od.configure(config);
 175:         }
 176:     }
 177: 
 178:     /**
 179:      * Tests for equality.
 180:      * 
 181:      * @param o  the object to test.
 182:      * 
 183:      * @return A boolean.
 184:      */
 185:     public boolean equals(final Object o) {
 186:         if (this == o) {
 187:             return true;
 188:         }
 189:         if (!(o instanceof ClassFactoryCollector)) {
 190:             return false;
 191:         }
 192:         if (!super.equals(o)) {
 193:             return false;
 194:         }
 195: 
 196:         final ClassFactoryCollector classFactoryCollector = (ClassFactoryCollector) o;
 197: 
 198:         if (!this.factories.equals(classFactoryCollector.factories)) {
 199:             return false;
 200:         }
 201: 
 202:         return true;
 203:     }
 204: 
 205:     /**
 206:      * Returns a hash code for the object.
 207:      * 
 208:      * @return The hash code.
 209:      */
 210:     public int hashCode() {
 211:         int result = super.hashCode();
 212:         result = 29 * result + this.factories.hashCode();
 213:         return result;
 214:     }
 215: }