From 43c35b992762a2c4b57b5fb7eabcedecfee43722 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Thu, 17 Jun 2004 23:14:55 +0000 Subject: [PATCH] Implemented equals and hashcode, making summaries with identical statistics equal. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141305 13f79535-47bb-0310-9956-ffa450edef68 --- .../stat/univariate/SummaryStatistics.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/math/stat/univariate/SummaryStatistics.java b/src/java/org/apache/commons/math/stat/univariate/SummaryStatistics.java index 429e424cd..896e37e39 100644 --- a/src/java/org/apache/commons/math/stat/univariate/SummaryStatistics.java +++ b/src/java/org/apache/commons/math/stat/univariate/SummaryStatistics.java @@ -18,11 +18,12 @@ package org.apache.commons.math.stat.univariate; import java.io.Serializable; import org.apache.commons.discovery.tools.DiscoverClass; +import org.apache.commons.math.util.MathUtils; /** * Abstract factory class for univariate statistical summaries. * - * @version $Revision: 1.7 $ $Date: 2004/06/14 23:26:53 $ + * @version $Revision: 1.8 $ $Date: 2004/06/17 23:14:55 $ */ public abstract class SummaryStatistics implements StatisticalSummary, Serializable { @@ -157,5 +158,48 @@ public abstract class SummaryStatistics implements StatisticalSummary, Serializa * Resets all statistics */ public abstract void clear(); + + /** + * Returns true iff object is a SummaryStatistics + * instance and all statistics have the same values as this. + * + * @return true if object equals this + */ + public boolean equals(Object object) { + if (object == this ) { + return true; + } + if (object instanceof SummaryStatistics == false) { + return false; + } + SummaryStatistics stat = (SummaryStatistics) object; + return (MathUtils.equals(stat.getGeometricMean(), + this.getGeometricMean()) && + MathUtils.equals(stat.getMax(), this.getMax()) && + MathUtils.equals(stat.getMean(),this.getMean()) && + MathUtils.equals(stat.getMin(),this.getMin()) && + MathUtils.equals(stat.getN(), this.getN()) && + MathUtils.equals(stat.getSum(), this.getSum()) && + MathUtils.equals(stat.getSumsq(),this.getSumsq()) && + MathUtils.equals(stat.getVariance(),this.getVariance())); + } + + /** + * Returns hash code based on values of statistics + * + * @return hash code + */ + public int hashCode() { + int result = 31 + MathUtils.hash(getGeometricMean()); + result = result * 31 + MathUtils.hash(getGeometricMean()); + result = result * 31 + MathUtils.hash(getMax()); + result = result * 31 + MathUtils.hash(getMean()); + result = result * 31 + MathUtils.hash(getMin()); + result = result * 31 + MathUtils.hash(getN()); + result = result * 31 + MathUtils.hash(getSum()); + result = result * 31 + MathUtils.hash(getSumsq()); + result = result * 31 + MathUtils.hash(getVariance()); + return result; + } }