    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.5 2005/11/14 11:00:48 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     * 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     * An abstract class that implements the {@link ClassFactory} interface.
    054     *
    055     * @author Thomas Morgner.
    056     */
    057    public abstract class ClassFactoryImpl implements ClassFactory {
    058
    059        /** Storage for the classes. */
    060        private HashMap classes;
    061        /** A class comparator for searching the super class */
    062        private ClassComparator comparator;
    063        /** The parser/report configuration */
    064        private Configuration config;
    065
    066        /**
    067         * Creates a new class factory.
    068         */
    069        public ClassFactoryImpl() {
    070            this.classes = new HashMap();
    071            this.comparator = new ClassComparator();
    072        }
    073
    074        /**
    075         * Returns the class comparator used to sort the super classes of an object.
    076         *
    077         * @return the class comparator.
    078         */
    079        public ClassComparator getComparator() {
    080            return this.comparator;
    081        }
    082
    083        /**
    084         * Returns an object-description for a class.
    085         *
    086         * @param c  the class.
    087         *
    088         * @return An object description.
    089         */
    090        public ObjectDescription getDescriptionForClass(final Class c) {
    091            final ObjectDescription od = (ObjectDescription) this.classes.get(c);
    092            if (od == null) {
    093                return null;
    094            }
    095            return od.getInstance();
    096        }
    097
    098        /**
    099         * Returns the most concrete object-description for the super class of a class.
    100         *
    101         * @param d  the class.
    102         * @param knownSuperClass a known supported superclass or null, if no superclass
    103         * is known yet.
    104         *
    105         * @return The object description.
    106         */
    107        public ObjectDescription getSuperClassObjectDescription
    108            (final Class d, ObjectDescription knownSuperClass) {
    109            final Iterator iterator = this.classes.keySet().iterator();
    110            while (iterator.hasNext()) {
    111                final Class keyClass = (Class) iterator.next();
    112                if (keyClass.isAssignableFrom(d)) {
    113                    final ObjectDescription od = (ObjectDescription) this.classes.get(keyClass);
    114                    if (knownSuperClass == null) {
    115                        knownSuperClass = od;
    116                    }
    117                    else {
    118                        if (this.comparator.isComparable
    119                            (knownSuperClass.getObjectClass(), od.getObjectClass())) {
    120                            if (this.comparator.compare
    121                                (knownSuperClass.getObjectClass(), od.getObjectClass()) < 0) {
    122                                knownSuperClass = od;
    123                            }
    124                        }
    125                    }
    126                }
    127            }
    128            if (knownSuperClass == null) {
    129                return null;
    130            }
    131            return knownSuperClass.getInstance();
    132        }
    133
    134        /**
    135         * Registers an object description with the factory.
    136         *
    137         * @param key  the key.
    138         * @param od  the object description.
    139         */
    140        protected void registerClass(final Class key, final ObjectDescription od) {
    141            this.classes.put(key, od);
    142            if (this.config != null) {
    143                od.configure(this.config);
    144            }
    145        }
    146
    147        /**
    148         * Returns an iterator that provides access to the registered object definitions.
    149         *
    150         * @return The iterator.
    151         */
    152        public Iterator getRegisteredClasses() {
    153            return this.classes.keySet().iterator();
    154        }
    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 (config == null) {
    168                throw new NullPointerException("The given configuration is null");
    169            }
    170            if (this.config != null) {
    171                // already configured ... ignored
    172                return;
    173            }
    174
    175            this.config = config;
    176            final Iterator it = this.classes.values().iterator();
    177            while (it.hasNext()) {
    178                final ObjectDescription od = (ObjectDescription) it.next();
    179                od.configure(config);
    180            }
    181        }
    182
    183        /**
    184         * Returns the currently set configuration or null, if none was set.
    185         *
    186         * @return the configuration.
    187         */
    188        public Configuration getConfig() {
    189            return this.config;
    190        }
    191
    192        /**
    193         * Tests for equality.
    194         *
    195         * @param o  the object to test.
    196         *
    197         * @return A boolean.
    198         */
    199        public boolean equals(final Object o) {
    200            if (this == o) {
    201                return true;
    202            }
    203            if (!(o instanceof ClassFactoryImpl)) {
    204                return false;
    205            }
    206
    207            final ClassFactoryImpl classFactory = (ClassFactoryImpl) o;
    208
    209            if (!this.classes.equals(classFactory.classes)) {
    210                return false;
    211            }
    212
    213            return true;
    214        }
    215
    216        /**
    217         * Returns a hash code.
    218         *
    219         * @return A hash code.
    220         */
    221        public int hashCode() {
    222            return this.classes.hashCode();
    223        }
    224    }