    001    /* ===========================================================
    002     * JFreeChart : a free chart 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/jfreechart/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     * AxisLocation.java
    029     * -----------------
    030     * (C) Copyright 2003-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: AxisLocation.java,v 1.4.2.1 2005/10/25 20:37:34 mungady Exp $
    036     *
    037     * Changes:
    038     * --------
    039     * 02-May-2003 : Version 1 (DG);
    040     * 03-Jul-2003 : Added isTopOrBottom() and isLeftOrRight() methods (DG);
    041     * 13-Aug-2003 : Fixed readResolve() bug (id=788202) (DG);
    042     * 24-Mar-2004 : Added getOpposite() method (DG);
    043     *
    044     */
    045
    046    package org.jfree.chart.axis;
    047
    048    import java.io.ObjectStreamException;
    049    import java.io.Serializable;
    050
    051    /**
    052     * Used to indicate the location of an axis on a 2D plot, prior to knowing the
    053     * orientation of the plot.
    054     */
    055    public final class AxisLocation implements Serializable {
    056
    057        /** For serialization. */
    058        private static final long serialVersionUID = -3276922179323563410L;
    059
    060        /** Axis at the top or left. */
    061        public static final AxisLocation TOP_OR_LEFT = new AxisLocation(
    062            "AxisLocation.TOP_OR_LEFT"
    063        );
    064
    065        /** Axis at the top or right. */
    066        public static final AxisLocation TOP_OR_RIGHT = new AxisLocation(
    067            "AxisLocation.TOP_OR_RIGHT"
    068        );
    069
    070        /** Axis at the bottom or left. */
    071        public static final AxisLocation BOTTOM_OR_LEFT = new AxisLocation(
    072            "AxisLocation.BOTTOM_OR_LEFT"
    073        );
    074
    075        /** Axis at the bottom or right. */
    076        public static final AxisLocation BOTTOM_OR_RIGHT = new AxisLocation(
    077            "AxisLocation.BOTTOM_OR_RIGHT"
    078        );
    079
    080        /** The name. */
    081        private String name;
    082
    083        /**
    084         * Private constructor.
    085         *
    086         * @param name  the name.
    087         */
    088        private AxisLocation(String name) {
    089            this.name = name;
    090        }
    091
    092        /**
    093         * Returns a string representing the object.
    094         *
    095         * @return The string.
    096         */
    097        public String toString() {
    098            return this.name;
    099        }
    100
    101        /**
    102         * Returns <code>true</code> if this object is equal to the specified
    103         * object, and <code>false</code> otherwise.
    104         *
    105         * @param obj  the other object (<code>null</code> permitted).
    106         *
    107         * @return A boolean.
    108         */
    109        public boolean equals(Object obj) {
    110
    111            if (this == obj) {
    112                return true;
    113            }
    114            if (!(obj instanceof AxisLocation)) {
    115                return false;
    116            }
    117            AxisLocation location = (AxisLocation) obj;
    118            if (!this.name.equals(location.toString())) {
    119                return false;
    120            }
    121            return true;
    122
    123        }
    124
    125        /**
    126         * Returns the location that is opposite to the supplied location.
    127         *
    128         * @param location  the location (<code>null</code> not permitted).
    129         *
    130         * @return The opposite location.
    131         */
    132        public static AxisLocation getOpposite(AxisLocation location) {
    133            if (location == null) {
    134                throw new IllegalArgumentException("Null 'location' argument.");
    135            }
    136            AxisLocation result = null;
    137            if (location == AxisLocation.TOP_OR_LEFT) {
    138                result = AxisLocation.BOTTOM_OR_RIGHT;
    139            }
    140            else if (location == AxisLocation.TOP_OR_RIGHT) {
    141                result = AxisLocation.BOTTOM_OR_LEFT;
    142            }
    143            else if (location == AxisLocation.BOTTOM_OR_LEFT) {
    144                result = AxisLocation.TOP_OR_RIGHT;
    145            }
    146            else if (location == AxisLocation.BOTTOM_OR_RIGHT) {
    147                result = AxisLocation.TOP_OR_LEFT;
    148            }
    149            else {
    150                throw new IllegalStateException("AxisLocation not recognised.");
    151            }
    152            return result;
    153        }
    154
    155        /**
    156         * Ensures that serialization returns the unique instances.
    157         *
    158         * @return The object.
    159         *
    160         * @throws ObjectStreamException if there is a problem.
    161         */
    162        private Object readResolve() throws ObjectStreamException {
    163            if (this.equals(AxisLocation.TOP_OR_RIGHT)) {
    164                return AxisLocation.TOP_OR_RIGHT;
    165            }
    166            else if (this.equals(AxisLocation.BOTTOM_OR_RIGHT)) {
    167                return AxisLocation.BOTTOM_OR_RIGHT;
    168            }
    169            else if (this.equals(AxisLocation.TOP_OR_LEFT)) {
    170                return AxisLocation.TOP_OR_LEFT;
    171            }
    172            else if (this.equals(AxisLocation.BOTTOM_OR_LEFT)) {
    173                return AxisLocation.BOTTOM_OR_LEFT;
    174            }
    175            return null;
    176        }
    177
    178    }