001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------
028 * AxisLocation.java
029 * -----------------
030 * (C) Copyright 2003-2020, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Nick Guenther;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.ObjectStreamException;
040import java.io.Serializable;
041import org.jfree.chart.util.Args;
042
043/**
044 * Used to indicate the location of an axis on a 2D plot, prior to knowing the
045 * orientation of the plot.
046 */
047public final class AxisLocation implements Serializable {
048
049    /** For serialization. */
050    private static final long serialVersionUID = -3276922179323563410L;
051
052    /** Axis at the top or left. */
053    public static final AxisLocation TOP_OR_LEFT = new AxisLocation(
054            "AxisLocation.TOP_OR_LEFT");
055
056    /** Axis at the top or right. */
057    public static final AxisLocation TOP_OR_RIGHT = new AxisLocation(
058            "AxisLocation.TOP_OR_RIGHT");
059
060    /** Axis at the bottom or left. */
061    public static final AxisLocation BOTTOM_OR_LEFT = new AxisLocation(
062            "AxisLocation.BOTTOM_OR_LEFT");
063
064    /** Axis at the bottom or right. */
065    public static final AxisLocation BOTTOM_OR_RIGHT = new AxisLocation(
066            "AxisLocation.BOTTOM_OR_RIGHT");
067
068    /** The name. */
069    private String name;
070
071    /**
072     * Private constructor.
073     *
074     * @param name  the name.
075     */
076    private AxisLocation(String name) {
077        this.name = name;
078    }
079
080    /**
081     * Returns the location that is opposite to this location.
082     *
083     * @return The opposite location.
084     *
085     * @since 1.0.5
086     */
087    public AxisLocation getOpposite() {
088        return getOpposite(this);
089    }
090
091    /**
092     * Returns a string representing the object.
093     *
094     * @return The string.
095     */
096    @Override
097    public String toString() {
098        return this.name;
099    }
100
101    /**
102     * Returns {@code true} if this object is equal to the specified
103     * object, and {@code false} otherwise.
104     *
105     * @param obj  the other object ({@code null} permitted).
106     *
107     * @return A boolean.
108     */
109    @Override
110    public boolean equals(Object obj) {
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     * Returns a hash code for this instance.
126     * 
127     * @return A hash code.
128     */
129    @Override
130    public int hashCode() {
131        int hash = 5;
132        hash = 83 * hash + this.name.hashCode();
133        return hash;
134    }
135
136    /**
137     * Returns the location that is opposite to the supplied location.
138     *
139     * @param location  the location ({@code null} not permitted).
140     *
141     * @return The opposite location.
142     */
143    public static AxisLocation getOpposite(AxisLocation location) {
144        Args.nullNotPermitted(location, "location");
145        AxisLocation result = null;
146        if (location == AxisLocation.TOP_OR_LEFT) {
147            result = AxisLocation.BOTTOM_OR_RIGHT;
148        }
149        else if (location == AxisLocation.TOP_OR_RIGHT) {
150            result = AxisLocation.BOTTOM_OR_LEFT;
151        }
152        else if (location == AxisLocation.BOTTOM_OR_LEFT) {
153            result = AxisLocation.TOP_OR_RIGHT;
154        }
155        else if (location == AxisLocation.BOTTOM_OR_RIGHT) {
156            result = AxisLocation.TOP_OR_LEFT;
157        }
158        else {
159            throw new IllegalStateException("AxisLocation not recognised.");
160        }
161        return result;
162    }
163
164    /**
165     * Ensures that serialization returns the unique instances.
166     *
167     * @return The object.
168     *
169     * @throws ObjectStreamException if there is a problem.
170     */
171    private Object readResolve() throws ObjectStreamException {
172        if (this.equals(AxisLocation.TOP_OR_RIGHT)) {
173            return AxisLocation.TOP_OR_RIGHT;
174        }
175        else if (this.equals(AxisLocation.BOTTOM_OR_RIGHT)) {
176            return AxisLocation.BOTTOM_OR_RIGHT;
177        }
178        else if (this.equals(AxisLocation.TOP_OR_LEFT)) {
179            return AxisLocation.TOP_OR_LEFT;
180        }
181        else if (this.equals(AxisLocation.BOTTOM_OR_LEFT)) {
182            return AxisLocation.BOTTOM_OR_LEFT;
183        }
184        return null;
185    }
186
187}