You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by lu...@free.fr on 2009/07/07 09:52:47 UTC
Re: svn commit: r791733 -
/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java
Here is what I have done on MATH-261. It seems to work as it does not triggers any warning anymore in SUN java compiler, eclipse java compiler, findbugs 1.2 and findbugs 1.3.9. I have tried not to change behaviour but am not sure I've done it properly.
Could someone please check this so we can close MATH-262 as resolved ?
thanks,
Luc
----- luc@apache.org a écrit :
> Author: luc
> Date: Tue Jul 7 07:44:52 2009
> New Revision: 791733
>
> URL: http://svn.apache.org/viewvc?rev=791733&view=rev
> Log:
> removed compilation/findbugs warnings about type safety
> the changes include API changes (replacing Object by Comparable<?>).
> The class itself is still not generic as this would prevent mixing
> types which is a desired feature here that is checked in the unit
> tests
> JIRA: MATH-261
>
> Modified:
>
> commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java
>
> Modified:
> commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java
> URL:
> http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java?rev=791733&r1=791732&r2=791733&view=diff
> ==============================================================================
> ---
> commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java
> (original)
> +++
> commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java
> Tue Jul 7 07:44:52 2009
> @@ -27,7 +27,7 @@
> /**
> * Maintains a frequency distribution.
> * <p>
> - * Accepts int, long, char or Object values. New values added must
> be
> + * Accepts int, long, char or Comparable values. New values added
> must be
> * comparable to those that have been added, otherwise the add method
> will
> * throw an IllegalArgumentException.</p>
> * <p>
> @@ -65,8 +65,9 @@
> *
> * @param comparator Comparator used to order values
> */
> - public Frequency(Comparator comparator) {
> - freqTable = new TreeMap<Comparable<?>, Long>(comparator);
> + @SuppressWarnings("unchecked")
> + public Frequency(Comparator<?> comparator) {
> + freqTable = new TreeMap<Comparable<?>, Long>((Comparator<?
> super Comparable<?>>) comparator);
> }
>
> /**
> @@ -80,9 +81,9 @@
> NumberFormat nf = NumberFormat.getPercentInstance();
> StringBuffer outBuffer = new StringBuffer();
> outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n");
> - Iterator<?> iter = freqTable.keySet().iterator();
> + Iterator<Comparable<?>> iter =
> freqTable.keySet().iterator();
> while (iter.hasNext()) {
> - Object value = iter.next();
> + Comparable<?> value = iter.next();
> outBuffer.append(value);
> outBuffer.append('\t');
> outBuffer.append(getCount(value));
> @@ -211,9 +212,9 @@
> */
> public long getSumFreq() {
> long result = 0;
> - Iterator<?> iterator = freqTable.values().iterator();
> + Iterator<Long> iterator = freqTable.values().iterator();
> while (iterator.hasNext()) {
> - result += ((Long) iterator.next()).longValue();
> + result += iterator.next().longValue();
> }
> return result;
> }
> @@ -224,8 +225,21 @@
> *
> * @param v the value to lookup.
> * @return the frequency of v.
> + * @deprecated replaced by {@link #getCount(Comparable)} as of
> 2.0
> */
> + @Deprecated
> public long getCount(Object v) {
> + return getCount((Comparable<?>) v);
> + }
> +
> + /**
> + * 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.
> + */
> + public long getCount(Comparable<?> v) {
> if (v instanceof Integer) {
> return getCount(((Integer) v).longValue());
> }
> @@ -281,8 +295,23 @@
> *
> * @param v the value to lookup
> * @return the proportion of values equal to v
> + * @deprecated replaced by {@link #getPct(Comparable)} as of 2.0
> */
> + @Deprecated
> public double getPct(Object v) {
> + return getCumPct((Comparable<?>) v);
> + }
> +
> + /**
> + * Returns the percentage of values that are equal to v
> + * (as a proportion between 0 and 1).
> + * <p>
> + * Returns <code>Double.NaN</code> if no values have been
> added.</p>
> + *
> + * @param v the value to lookup
> + * @return the proportion of values equal to v
> + */
> + public double getPct(Comparable<?> v) {
> final long sumFreq = getSumFreq();
> if (sumFreq == 0) {
> return Double.NaN;
> @@ -332,15 +361,30 @@
> *
> * @param v the value to lookup.
> * @return the proportion of values equal to v
> + * @deprecated replaced by {@link #getCumFreq(Comparable)} as of
> 2.0
> */
> + @Deprecated
> public long getCumFreq(Object v) {
> + return getCumFreq((Comparable<?>) v);
> + }
> +
> + /**
> + * Returns the cumulative frequency of values less than or equal
> to v.
> + * <p>
> + * Returns 0 if v is not comparable to the values set.</p>
> + *
> + * @param v the value to lookup.
> + * @return the proportion of values equal to v
> + */
> + @SuppressWarnings("unchecked")
> + public long getCumFreq(Comparable<?> v) {
> if (getSumFreq() == 0) {
> return 0;
> }
> if (v instanceof Integer) {
> return getCumFreq(((Integer) v).longValue());
> }
> - Comparator c = freqTable.comparator();
> + Comparator<Comparable<?>> c = (Comparator<Comparable<?>>)
> freqTable.comparator();
> if (c == null) {
> c = new NaturalComparator();
> }
> @@ -354,7 +398,7 @@
> } catch (ClassCastException ex) {
> return result; // v is not comparable
> }
> -
> +
> if (c.compare(v, freqTable.firstKey()) < 0) {
> return 0; // v is comparable, but less than first value
> }
> @@ -363,9 +407,9 @@
> return getSumFreq(); // v is comparable, but greater
> than the last value
> }
>
> - Iterator<?> values = valuesIterator();
> + Iterator<Comparable<?>> values = valuesIterator();
> while (values.hasNext()) {
> - Object nextValue = values.next();
> + Comparable<?> nextValue = values.next();
> if (c.compare(v, nextValue) > 0) {
> result += getCount(nextValue);
> } else {
> @@ -423,8 +467,26 @@
> *
> * @param v the value to lookup
> * @return the proportion of values less than or equal to v
> + * @deprecated replaced by {@link #getCumPct(Comparable)} as of
> 2.0
> */
> + @Deprecated
> public double getCumPct(Object v) {
> + return getCumPct((Comparable<?>) v);
> +
> + }
> +
> + /**
> + * Returns the cumulative percentage of values less than or equal
> to v
> + * (as a proportion between 0 and 1).
> + * <p>
> + * Returns <code>Double.NaN</code> if no values have been added.
> + * Returns 0 if at least one value has been added, but v is not
> comparable
> + * to the values set.</p>
> + *
> + * @param v the value to lookup
> + * @return the proportion of values less than or equal to v
> + */
> + public double getCumPct(Comparable<?> v) {
> final long sumFreq = getSumFreq();
> if (sumFreq == 0) {
> return Double.NaN;
> @@ -475,7 +537,7 @@
> * A Comparator that compares comparable objects using the
> * natural order. Copied from Commons Collections
> ComparableComparator.
> */
> - private static class NaturalComparator implements Comparator,
> Serializable {
> + private static class NaturalComparator<T extends Comparable<T>>
> implements Comparator<Comparable<T>>, Serializable {
>
> /** Serializable version identifier */
> private static final long serialVersionUID =
> -3852193713161395148L;
> @@ -493,9 +555,9 @@
> * @throws ClassCastException when <i>o1</i> is not a {@link
> Comparable Comparable},
> * or when
> <code>((Comparable)o1).compareTo(o2)</code> does
> */
> - @SuppressWarnings("unchecked") // See Javadoc, ClassCast is
> expected
> - public int compare(Object o1, Object o2) {
> - return ((Comparable<Object>)o1).compareTo(o2);
> + @SuppressWarnings("unchecked")
> + public int compare(Comparable<T> o1, Comparable<T> o2) {
> + return (o1.compareTo((T) o2));
> }
> }
>
> @@ -526,4 +588,5 @@
> return false;
> return true;
> }
> +
> }
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org