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 * OutlierList.java
029 * ----------------
030 * (C) Copyright 2003-present, by David Browning and Contributors.
031 *
032 * Original Author:  David Browning (for Australian Institute of Marine
033 *                   Science);
034 * Contributor(s):   David Gilbert;
035 *
036 */
037
038package org.jfree.chart.renderer;
039
040import java.awt.geom.Point2D;
041import java.util.ArrayList;
042import java.util.Iterator;
043import java.util.List;
044
045/**
046 * A collection of outliers for a single entity in a box and whisker plot.
047 *
048 * Outliers are grouped in lists for each entity. Lists contain
049 * one or more outliers, determined by whether overlaps have
050 * occured. Overlapping outliers are grouped in the same list.
051 *
052 * Each list contains an averaged outlier, which is the same as a single
053 * outlier if there is only one outlier in the list, but the average of
054 * all the outliers in the list if there is more than one.
055 *
056 * NB This is simply my scheme for displaying outliers, and might not be
057 * acceptable by the wider community.
058 */
059public class OutlierList {
060
061    /** Storage for the outliers. */
062    private List outliers;
063
064    /** The averaged outlier. */
065    private Outlier averagedOutlier;
066
067    /**
068     * A flag that indicates whether or not there are multiple outliers in the
069     * list.
070     */
071    private boolean multiple = false;
072
073    /**
074     * Creates a new list containing a single outlier.
075     *
076     * @param outlier  the outlier.
077     */
078    public OutlierList(Outlier outlier) {
079        this.outliers = new ArrayList();
080        setAveragedOutlier(outlier);
081    }
082
083    /**
084     * Adds an outlier to the list.
085     *
086     * @param outlier  the outlier.
087     *
088     * @return A boolean.
089     */
090    public boolean add(Outlier outlier) {
091        return this.outliers.add(outlier);
092    }
093
094    /**
095     * Returns the number of outliers in the list.
096     *
097     * @return The item count.
098     */
099    public int getItemCount() {
100        return this.outliers.size();
101    }
102
103    /**
104     * Returns the averaged outlier.
105     *
106     * @return The averaged outlier.
107     */
108    public Outlier getAveragedOutlier() {
109        return this.averagedOutlier;
110    }
111
112    /**
113     * Sets the averaged outlier.
114     *
115     * @param averagedOutlier  the averaged outlier.
116     */
117    public void setAveragedOutlier(Outlier averagedOutlier) {
118        this.averagedOutlier = averagedOutlier;
119    }
120
121    /**
122     * Returns {@code true} if the list contains multiple outliers, and
123     * {@code false} otherwise.
124     *
125     * @return A boolean.
126     */
127    public boolean isMultiple() {
128        return this.multiple;
129    }
130
131    /**
132     * Sets the flag that indicates whether or not this list represents
133     * multiple outliers.
134     *
135     * @param multiple  the flag.
136     */
137    public void setMultiple(boolean multiple) {
138        this.multiple = multiple;
139    }
140
141    /**
142     * Returns {@code true} if the outlier overlaps, and
143     * {@code false} otherwise.
144     *
145     * @param other  the outlier.
146     *
147     * @return A boolean.
148     */
149    public boolean isOverlapped(Outlier other) {
150
151        if (other == null) {
152            return false;
153        }
154
155        boolean result = other.overlaps(getAveragedOutlier());
156        return result;
157
158    }
159
160    /**
161     * Updates the averaged outlier.
162     *
163     */
164    public void updateAveragedOutlier() {
165        double totalXCoords = 0.0;
166        double totalYCoords = 0.0;
167        int size = getItemCount();
168        for (Iterator iterator = this.outliers.iterator();
169                iterator.hasNext();) {
170            Outlier o = (Outlier) iterator.next();
171            totalXCoords += o.getX();
172            totalYCoords += o.getY();
173        }
174        getAveragedOutlier().getPoint().setLocation(
175                new Point2D.Double(totalXCoords / size, totalYCoords / size));
176    }
177
178}