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 * MeanAndStandardDeviation.java
029 * -----------------------------
030 * (C) Copyright 2003-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.statistics;
038
039import java.io.Serializable;
040import java.util.Objects;
041
042/**
043 * A simple data structure that holds a mean value and a standard deviation
044 * value.  This is used in the
045 * {@link org.jfree.data.statistics.DefaultStatisticalCategoryDataset} class.
046 */
047public class MeanAndStandardDeviation implements Serializable {
048
049    /** For serialization. */
050    private static final long serialVersionUID = 7413468697315721515L;
051
052    /** The mean. */
053    private Number mean;
054
055    /** The standard deviation. */
056    private Number standardDeviation;
057
058    /**
059     * Creates a new mean and standard deviation record.
060     *
061     * @param mean  the mean.
062     * @param standardDeviation  the standard deviation.
063     */
064    public MeanAndStandardDeviation(double mean, double standardDeviation) {
065        this(Double.valueOf(mean), Double.valueOf(standardDeviation));
066    }
067
068    /**
069     * Creates a new mean and standard deviation record.
070     *
071     * @param mean  the mean ({@code null} permitted).
072     * @param standardDeviation  the standard deviation ({@code null}
073     *                           permitted.
074     */
075    public MeanAndStandardDeviation(Number mean, Number standardDeviation) {
076        this.mean = mean;
077        this.standardDeviation = standardDeviation;
078    }
079
080    /**
081     * Returns the mean.
082     *
083     * @return The mean.
084     */
085    public Number getMean() {
086        return this.mean;
087    }
088
089    /**
090     * Returns the mean as a double primitive.  If the underlying mean is
091     * {@code null}, this method will return {@code Double.NaN}.
092     *
093     * @return The mean.
094     *
095     * @see #getMean()
096     *
097     * @since 1.0.7
098     */
099    public double getMeanValue() {
100        double result = Double.NaN;
101        if (this.mean != null) {
102            result = this.mean.doubleValue();
103        }
104        return result;
105    }
106
107    /**
108     * Returns the standard deviation.
109     *
110     * @return The standard deviation.
111     */
112    public Number getStandardDeviation() {
113        return this.standardDeviation;
114    }
115
116    /**
117     * Returns the standard deviation as a double primitive.  If the underlying
118     * standard deviation is {@code null}, this method will return
119     * {@code Double.NaN}.
120     *
121     * @return The standard deviation.
122     *
123     * @since 1.0.7
124     */
125    public double getStandardDeviationValue() {
126        double result = Double.NaN;
127        if (this.standardDeviation != null) {
128            result = this.standardDeviation.doubleValue();
129        }
130        return result;
131    }
132
133    /**
134     * Tests this instance for equality with an arbitrary object.
135     *
136     * @param obj  the object ({@code null} permitted).
137     *
138     * @return A boolean.
139     */
140    @Override
141    public boolean equals(Object obj) {
142        if (obj == this) {
143            return true;
144        }
145        if (!(obj instanceof MeanAndStandardDeviation)) {
146            return false;
147        }
148        MeanAndStandardDeviation that = (MeanAndStandardDeviation) obj;
149        if (!Objects.equals(this.mean, that.mean)) {
150            return false;
151        }
152        if (!Objects.equals(this.standardDeviation, that.standardDeviation)
153        ) {
154            return false;
155        }
156        return true;
157    }
158
159    @Override
160    public int hashCode()
161    {
162        int hash = 3;
163        hash = 79 * hash + Objects.hashCode( this.mean );
164        hash = 79 * hash + Objects.hashCode( this.standardDeviation );
165        return hash;
166    }
167
168    /**
169     * Returns a string representing this instance.
170     *
171     * @return A string.
172     *
173     * @since 1.0.7
174     */
175    @Override
176    public String toString() {
177        return "[" + this.mean + ", " + this.standardDeviation + "]";
178    }
179
180}