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 * Rotation.java
029 * -------------
030 * (C)opyright 2003-2005, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * $Id: Rotation.java,v 1.5 2005/11/16 15:58:41 taqua Exp $
036 *
037 * Changes
038 * -------
039 * 19-Aug-2003 : Version 1 (DG);
040 * 20-Feb-2004 : Updated Javadocs (DG);
041 *
042 */
043
044 package org.jfree.util;
045
046 import java.io.ObjectStreamException;
047 import java.io.Serializable;
048
049 /**
050 * Represents a direction of rotation (<code>CLOCKWISE</code> or
051 * <code>ANTICLOCKWISE</code>).
052 * @author David Gilbert
053 */
054 public final class Rotation implements Serializable {
055
056 /** For serialization. */
057 private static final long serialVersionUID = -4662815260201591676L;
058
059 /** Clockwise. */
060 public static final Rotation CLOCKWISE
061 = new Rotation("Rotation.CLOCKWISE", -1.0);
062
063 /** The reverse order renders the primary dataset first. */
064 public static final Rotation ANTICLOCKWISE
065 = new Rotation("Rotation.ANTICLOCKWISE", 1.0);
066
067 /** The name. */
068 private String name;
069
070 /**
071 * The factor (-1.0 for <code>CLOCKWISE</code> and 1.0 for
072 * <code>ANTICLOCKWISE</code>).
073 */
074 private double factor;
075
076 /**
077 * Private constructor.
078 *
079 * @param name the name.
080 * @param factor the rotation factor.
081 */
082 private Rotation(final String name, final double factor) {
083 this.name = name;
084 this.factor = factor;
085 }
086
087 /**
088 * Returns a string representing the object.
089 *
090 * @return the string (never <code>null</code>).
091 */
092 public String toString() {
093 return this.name;
094 }
095
096 /**
097 * Returns the rotation factor, which is -1.0 for <code>CLOCKWISE</code>
098 * and 1.0 for <code>ANTICLOCKWISE</code>.
099 *
100 * @return the rotation factor.
101 */
102 public double getFactor() {
103 return this.factor;
104 }
105
106 /**
107 * Compares this object for equality with an other object.
108 * Implementation note: This simply compares the factor instead
109 * of the name.
110 *
111 * @param o the other object
112 * @return true or false
113 */
114 public boolean equals(final Object o) {
115 if (this == o) {
116 return true;
117 }
118 if (!(o instanceof Rotation)) {
119 return false;
120 }
121
122 final Rotation rotation = (Rotation) o;
123
124 if (this.factor != rotation.factor) {
125 return false;
126 }
127
128 return true;
129 }
130
131 /**
132 * Returns a hash code value for the object.
133 *
134 * @return the hashcode
135 */
136 public int hashCode() {
137 final long temp = Double.doubleToLongBits(this.factor);
138 return (int) (temp ^ (temp >>> 32));
139 }
140
141 /**
142 * Ensures that serialization returns the unique instances.
143 *
144 * @return the object.
145 *
146 * @throws ObjectStreamException if there is a problem.
147 */
148 private Object readResolve() throws ObjectStreamException {
149 if (this.equals(Rotation.CLOCKWISE)) {
150 return Rotation.CLOCKWISE;
151 }
152 else if (this.equals(Rotation.ANTICLOCKWISE)) {
153 return Rotation.ANTICLOCKWISE;
154 }
155 return null;
156 }
157
158 }