001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * Outlier.java
029 * ------------
030 * (C) Copyright 2003-2008, by David Browning and Contributors.
031 *
032 * Original Author:  David Browning (for Australian Institute of Marine
033 *                   Science);
034 * Contributor(s):   David Gilbert (for Object Refinery Limited);
035 *
036 * Changes
037 * -------
038 * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
039 * 28-Aug-2003 : Minor tidy-up (DG);
040 * ------------- JFREECHART 1.0.x ---------------------------------------------
041 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
042 * 21-Nov-2007 : Implemented equals() to shut up FindBugs (DG);
043 *
044 */
045
046package org.jfree.chart.renderer;
047
048import java.awt.geom.Point2D;
049
050/**
051 * Represents one outlier in the box and whisker plot.
052 * <P>
053 * All the coordinates in this class are in Java2D space.
054 */
055public class Outlier implements Comparable {
056
057    /**
058     * The xy coordinates of the bounding box containing the outlier ellipse.
059     */
060    private Point2D point;
061
062    /** The radius of the ellipse */
063    private double radius;
064
065    /**
066     * Constructs an outlier item consisting of a point and the radius of the
067     * outlier ellipse
068     *
069     * @param xCoord  the x coordinate of the point.
070     * @param yCoord  the y coordinate of the point.
071     * @param radius  the radius of the ellipse.
072     */
073    public Outlier(double xCoord, double yCoord, double radius) {
074        this.point = new Point2D.Double(xCoord - radius, yCoord - radius);
075        this.radius = radius;
076    }
077
078    /**
079     * Returns the xy coordinates of the bounding box containing the outlier
080     * ellipse.
081     *
082     * @return The location of the outlier ellipse.
083     */
084    public Point2D getPoint() {
085        return this.point;
086    }
087
088    /**
089     * Sets the xy coordinates of the bounding box containing the outlier
090     * ellipse.
091     *
092     * @param point  the location.
093     */
094    public void setPoint(Point2D point) {
095        this.point = point;
096    }
097
098    /**
099     * Returns the x coordinate of the bounding box containing the outlier
100     * ellipse.
101     *
102     * @return The x coordinate.
103     */
104    public double getX() {
105        return getPoint().getX();
106    }
107
108    /**
109     * Returns the y coordinate of the bounding box containing the outlier
110     * ellipse.
111     *
112     * @return The y coordinate.
113     */
114    public double getY() {
115        return getPoint().getY();
116    }
117
118    /**
119     * Returns the radius of the outlier ellipse.
120     *
121     * @return The radius.
122     */
123    public double getRadius() {
124        return this.radius;
125    }
126
127    /**
128     * Sets the radius of the outlier ellipse.
129     *
130     * @param radius  the new radius.
131     */
132    public void setRadius(double radius) {
133        this.radius = radius;
134    }
135
136    /**
137     * Compares this object with the specified object for order, based on
138     * the outlier's point.
139     *
140     * @param   o the Object to be compared.
141     * @return A negative integer, zero, or a positive integer as this object
142     *      is less than, equal to, or greater than the specified object.
143     *
144     */
145    @Override
146    public int compareTo(Object o) {
147        Outlier outlier = (Outlier) o;
148        Point2D p1 = getPoint();
149        Point2D p2 = outlier.getPoint();
150        if (p1.equals(p2)) {
151            return 0;
152        }
153        else if ((p1.getX() < p2.getX()) || (p1.getY() < p2.getY())) {
154            return -1;
155        }
156        else {
157            return 1;
158        }
159    }
160
161    /**
162     * Returns a true if outlier is overlapped and false if it is not.
163     * Overlapping is determined by the respective bounding boxes plus
164     * a small margin.
165     *
166     * @param other  the other outlier.
167     *
168     * @return A <code>boolean</code> indicating whether or not an overlap has
169     *         occurred.
170     */
171    public boolean overlaps(Outlier other) {
172        return ((other.getX() >= getX() - (this.radius * 1.1))
173                && (other.getX() <= getX() + (this.radius * 1.1))
174                && (other.getY() >= getY() - (this.radius * 1.1))
175                && (other.getY() <= getY() + (this.radius * 1.1)));
176    }
177
178    /**
179     * Tests this outlier for equality with an arbitrary object.
180     *
181     * @param obj  the object (<code>null</code> permitted).
182     *
183     * @return A boolean.
184     */
185    @Override
186    public boolean equals(Object obj) {
187        if (obj == this) {
188            return true;
189        }
190        if (!(obj instanceof Outlier)) {
191            return false;
192        }
193        Outlier that = (Outlier) obj;
194        if (!this.point.equals(that.point)) {
195            return false;
196        }
197        if (this.radius != that.radius) {
198            return false;
199        }
200        return true;
201    }
202
203    /**
204     * Returns a textual representation of the outlier.
205     *
206     * @return A <code>String</code> representing the outlier.
207     */
208    @Override
209    public String toString() {
210        return "{" + getX() + "," + getY() + "}";
211    }
212
213}