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 * MeanAndStandardDeviation.java
029 * -----------------------------
030 * (C) Copyright 2003-present, 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    public double getMeanValue() {
098        double result = Double.NaN;
099        if (this.mean != null) {
100            result = this.mean.doubleValue();
101        }
102        return result;
103    }
104
105    /**
106     * Returns the standard deviation.
107     *
108     * @return The standard deviation.
109     */
110    public Number getStandardDeviation() {
111        return this.standardDeviation;
112    }
113
114    /**
115     * Returns the standard deviation as a double primitive.  If the underlying
116     * standard deviation is {@code null}, this method will return
117     * {@code Double.NaN}.
118     *
119     * @return The standard deviation.
120     */
121    public double getStandardDeviationValue() {
122        double result = Double.NaN;
123        if (this.standardDeviation != null) {
124            result = this.standardDeviation.doubleValue();
125        }
126        return result;
127    }
128
129    /**
130     * Tests this instance for equality with an arbitrary object.
131     *
132     * @param obj  the object ({@code null} permitted).
133     *
134     * @return A boolean.
135     */
136    @Override
137    public boolean equals(Object obj) {
138        if (obj == this) {
139            return true;
140        }
141        if (!(obj instanceof MeanAndStandardDeviation)) {
142            return false;
143        }
144        MeanAndStandardDeviation that = (MeanAndStandardDeviation) obj;
145        if (!Objects.equals(this.mean, that.mean)) {
146            return false;
147        }
148        if (!Objects.equals(this.standardDeviation, that.standardDeviation)) {
149            return false;
150        }
151        return true;
152    }
153
154    /**
155     * Returns a string representing this instance.
156     *
157     * @return A string.
158     */
159    @Override
160    public String toString() {
161        return "[" + this.mean + ", " + this.standardDeviation + "]";
162    }
163
164}