    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
    045package org.jfree.xml.factory.objects;
    046
    047import java.util.ArrayList;
    048import java.util.Iterator;
    049
    050import org.jfree.util.Configuration;
    051
    052/**
    053 * A class factory collector.
    054 *
    055 * @author Thomas Morgner
    056 */
    057public 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.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}