From 04d470232ee8de204fad5d0339c32d646be3a5c5 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Fri, 17 Apr 2009 13:07:26 +0000 Subject: [PATCH] Make HashMap final as it is only set in the ctors Document behaviour of getCount(Object v) if v is not comparable Save value of getSumFreq() from DIV/0 check so we don't calculate it twice git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@765978 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/math/stat/Frequency.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/commons/math/stat/Frequency.java b/src/java/org/apache/commons/math/stat/Frequency.java index ac909c1fe..a5b8d7b60 100644 --- a/src/java/org/apache/commons/math/stat/Frequency.java +++ b/src/java/org/apache/commons/math/stat/Frequency.java @@ -49,7 +49,7 @@ public class Frequency implements Serializable { private static final long serialVersionUID = -3845586908418844111L; /** underlying collection */ - private TreeMap freqTable = null; + private final TreeMap freqTable; /** * Default constructor. @@ -193,6 +193,7 @@ public class Frequency implements Serializable { /** * Returns the number of values = v. + * Returns 0 if the value is not comparable. * * @param v the value to lookup. * @return the frequency of v. @@ -255,10 +256,11 @@ public class Frequency implements Serializable { * @return the proportion of values equal to v */ public double getPct(Object v) { - if (getSumFreq() == 0) { + final long sumFreq = getSumFreq(); + if (sumFreq == 0) { return Double.NaN; } - return (double) getCount(v) / (double) getSumFreq(); + return (double) getCount(v) / (double) sumFreq; } /** @@ -396,10 +398,11 @@ public class Frequency implements Serializable { * @return the proportion of values less than or equal to v */ public double getCumPct(Object v) { - if (getSumFreq() == 0) { + final long sumFreq = getSumFreq(); + if (sumFreq == 0) { return Double.NaN; } - return (double) getCumFreq(v) / (double) getSumFreq(); + return (double) getCumFreq(v) / (double) sumFreq; } /**