001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert 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 029package org.jfree.chart.ui; 030 031import java.awt.geom.Rectangle2D; 032import java.io.ObjectStreamException; 033import java.io.Serializable; 034 035/** 036 * Used to indicate the edge of a rectangle. 037 */ 038public final class RectangleEdge implements Serializable { 039 040 /** For serialization. */ 041 private static final long serialVersionUID = -7400988293691093548L; 042 043 /** Top. */ 044 public static final RectangleEdge TOP 045 = new RectangleEdge("RectangleEdge.TOP"); 046 047 /** Bottom. */ 048 public static final RectangleEdge BOTTOM 049 = new RectangleEdge("RectangleEdge.BOTTOM"); 050 051 /** Left. */ 052 public static final RectangleEdge LEFT 053 = new RectangleEdge("RectangleEdge.LEFT"); 054 055 /** Right. */ 056 public static final RectangleEdge RIGHT 057 = new RectangleEdge("RectangleEdge.RIGHT"); 058 059 /** The name. */ 060 private String name; 061 062 /** 063 * Private constructor. 064 * 065 * @param name the name. 066 */ 067 private RectangleEdge(String name) { 068 this.name = name; 069 } 070 071 /** 072 * Returns a string representing the object. 073 * 074 * @return The string. 075 */ 076 @Override 077 public String toString() { 078 return this.name; 079 } 080 081 /** 082 * Returns {@code true} if this object is equal to the specified 083 * object, and {@code false} otherwise. 084 * 085 * @param o the other object. 086 * 087 * @return A boolean. 088 */ 089 @Override 090 public boolean equals(Object o) { 091 092 if (this == o) { 093 return true; 094 } 095 if (!(o instanceof RectangleEdge)) { 096 return false; 097 } 098 099 final RectangleEdge order = (RectangleEdge) o; 100 if (!this.name.equals(order.name)) { 101 return false; 102 } 103 104 return true; 105 106 } 107 108 /** 109 * Returns a hash code value for the object. 110 * 111 * @return the hashcode 112 */ 113 @Override 114 public int hashCode() { 115 return this.name.hashCode(); 116 } 117 118 /** 119 * Returns {@code true} if the edge is {@code TOP} or 120 * {@code BOTTOM}, and {@code false} otherwise. 121 * 122 * @param edge the edge. 123 * 124 * @return A boolean. 125 */ 126 public static boolean isTopOrBottom(RectangleEdge edge) { 127 return (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM); 128 } 129 130 /** 131 * Returns {@code true} if the edge is {@code LEFT} or 132 * {@code RIGHT}, and {@code false} otherwise. 133 * 134 * @param edge the edge. 135 * 136 * @return A boolean. 137 */ 138 public static boolean isLeftOrRight(RectangleEdge edge) { 139 return (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT); 140 } 141 142 /** 143 * Returns the opposite edge. 144 * 145 * @param edge an edge. 146 * 147 * @return The opposite edge. 148 */ 149 public static RectangleEdge opposite(RectangleEdge edge) { 150 RectangleEdge result = null; 151 if (edge == RectangleEdge.TOP) { 152 result = RectangleEdge.BOTTOM; 153 } 154 else if (edge == RectangleEdge.BOTTOM) { 155 result = RectangleEdge.TOP; 156 } 157 else if (edge == RectangleEdge.LEFT) { 158 result = RectangleEdge.RIGHT; 159 } 160 else if (edge == RectangleEdge.RIGHT) { 161 result = RectangleEdge.LEFT; 162 } 163 return result; 164 } 165 166 /** 167 * Returns the x or y coordinate of the specified edge. 168 * 169 * @param rectangle the rectangle. 170 * @param edge the edge. 171 * 172 * @return The coordinate. 173 */ 174 public static double coordinate(Rectangle2D rectangle, RectangleEdge edge) { 175 double result = 0.0; 176 if (edge == RectangleEdge.TOP) { 177 result = rectangle.getMinY(); 178 } 179 else if (edge == RectangleEdge.BOTTOM) { 180 result = rectangle.getMaxY(); 181 } 182 else if (edge == RectangleEdge.LEFT) { 183 result = rectangle.getMinX(); 184 } 185 else if (edge == RectangleEdge.RIGHT) { 186 result = rectangle.getMaxX(); 187 } 188 return result; 189 } 190 191 /** 192 * Ensures that serialization returns the unique instances. 193 * 194 * @return The object. 195 * 196 * @throws ObjectStreamException if there is a problem. 197 */ 198 private Object readResolve() throws ObjectStreamException { 199 RectangleEdge result = null; 200 if (this.equals(RectangleEdge.TOP)) { 201 result = RectangleEdge.TOP; 202 } 203 else if (this.equals(RectangleEdge.BOTTOM)) { 204 result = RectangleEdge.BOTTOM; 205 } 206 else if (this.equals(RectangleEdge.LEFT)) { 207 result = RectangleEdge.LEFT; 208 } 209 else if (this.equals(RectangleEdge.RIGHT)) { 210 result = RectangleEdge.RIGHT; 211 } 212 return result; 213 } 214 215}