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