001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.stat.descriptive;
018
019import org.apache.commons.math4.core.jdkmath.JdkMath;
020import org.apache.commons.numbers.core.Precision;
021
022/**
023 *  Value object representing the results of a univariate statistical summary.
024 *
025 */
026public class StatisticalSummaryValues
027    implements StatisticalSummary {
028    /** The sample mean. */
029    private final double mean;
030
031    /** The sample variance. */
032    private final double variance;
033
034    /** The number of observations in the sample. */
035    private final long n;
036
037    /** The maximum value. */
038    private final double max;
039
040    /** The minimum value. */
041    private final double min;
042
043    /** The sum of the sample values. */
044    private final double sum;
045
046    /**
047      * Constructor.
048      *
049      * @param mean  the sample mean
050      * @param variance  the sample variance
051      * @param n  the number of observations in the sample
052      * @param max  the maximum value
053      * @param min  the minimum value
054      * @param sum  the sum of the values
055     */
056    public StatisticalSummaryValues(double mean, double variance, long n,
057        double max, double min, double sum) {
058        super();
059        this.mean = mean;
060        this.variance = variance;
061        this.n = n;
062        this.max = max;
063        this.min = min;
064        this.sum = sum;
065    }
066
067    /**
068     * @return Returns the max.
069     */
070    @Override
071    public double getMax() {
072        return max;
073    }
074
075    /**
076     * @return Returns the mean.
077     */
078    @Override
079    public double getMean() {
080        return mean;
081    }
082
083    /**
084     * @return Returns the min.
085     */
086    @Override
087    public double getMin() {
088        return min;
089    }
090
091    /**
092     * @return Returns the number of values.
093     */
094    @Override
095    public long getN() {
096        return n;
097    }
098
099    /**
100     * @return Returns the sum.
101     */
102    @Override
103    public double getSum() {
104        return sum;
105    }
106
107    /**
108     * @return Returns the standard deviation
109     */
110    @Override
111    public double getStandardDeviation() {
112        return JdkMath.sqrt(variance);
113    }
114
115    /**
116     * @return Returns the variance.
117     */
118    @Override
119    public double getVariance() {
120        return variance;
121    }
122
123    /**
124     * Returns true iff <code>object</code> is a
125     * <code>StatisticalSummaryValues</code> instance and all statistics have
126     *  the same values as this.
127     *
128     * @param object the object to test equality against.
129     * @return true if object equals this
130     */
131    @Override
132    public boolean equals(Object object) {
133        if (object == this ) {
134            return true;
135        }
136        if (!(object instanceof StatisticalSummaryValues)) {
137            return false;
138        }
139        StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
140        return Precision.equalsIncludingNaN(stat.getMax(),      getMax())  &&
141               Precision.equalsIncludingNaN(stat.getMean(),     getMean()) &&
142               Precision.equalsIncludingNaN(stat.getMin(),      getMin())  &&
143               Precision.equalsIncludingNaN(stat.getN(),        getN())    &&
144               Precision.equalsIncludingNaN(stat.getSum(),      getSum())  &&
145               Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
146    }
147
148    /**
149     * Returns hash code based on values of statistics.
150     *
151     * @return hash code
152     */
153    @Override
154    public int hashCode() {
155        int result = 31 + Double.hashCode(getMax());
156        result = result * 31 + Double.hashCode(getMean());
157        result = result * 31 + Double.hashCode(getMin());
158        result = result * 31 + Double.hashCode(getN());
159        result = result * 31 + Double.hashCode(getSum());
160        result = result * 31 + Double.hashCode(getVariance());
161        return result;
162    }
163
164    /**
165     * Generates a text report displaying values of statistics.
166     * Each statistic is displayed on a separate line.
167     *
168     * @return String with line feeds displaying statistics
169     */
170    @Override
171    public String toString() {
172        StringBuffer outBuffer = new StringBuffer();
173        String endl = "\n";
174        outBuffer.append("StatisticalSummaryValues:").append(endl);
175        outBuffer.append("n: ").append(getN()).append(endl);
176        outBuffer.append("min: ").append(getMin()).append(endl);
177        outBuffer.append("max: ").append(getMax()).append(endl);
178        outBuffer.append("mean: ").append(getMean()).append(endl);
179        outBuffer.append("std dev: ").append(getStandardDeviation())
180            .append(endl);
181        outBuffer.append("variance: ").append(getVariance()).append(endl);
182        outBuffer.append("sum: ").append(getSum()).append(endl);
183        return outBuffer.toString();
184    }
185}