You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Shing Hing Man <ma...@yahoo.com> on 2004/08/11 14:19:45 UTC

[Math] Frequency : Is int comparable to Integer through default comparator

Deal all,
   It would be appreciated if someone could clarify
the following 
 on 
org.apache.commons.math.stat.univariate.Frequency.

My version of  org.apache.commons.math is dated 10
August, 2004.
The following snippet piece of code is from 

http://jakarta.apache.org/commons/math/userguide/stat.html#1.3%20Frequency%20distributions


  Frequency f = new Frequency();
        f.addValue(1);
        f.addValue(new Integer(1));   // (*)
        f.addValue(new Long(1));
        f.addValue(2);
        f.addValue(new Integer(-1));
        System.out.println(f.getCount(1));   //
displays 3
        System.out.println(f.getCumPct(0));  //
displays 0.2
        System.out.println(f.getPct(new Integer(1))); 
// displays 0.6
        System.out.println(f.getCumPct(-2));   //
displays 0
        System.out.println(f.getCumPct(10));  //
displays 1


When I run it, I got the following exception at line
(*).


Exception in thread "main"
java.lang.IllegalArgumentException: Value not
comparable to
 existing values.

It looks as though the default comparator can not
compare primitive type int with
Integer object. I thought this piece of code is
supposed to demonstrate the otherwise.

Thanks in advice for your help !

Shing


=====
Home page :
  http://uk.geocities.com/matmsh/index.html


	
	
		
___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun!  http://uk.messenger.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Math] Frequency : Is int comparable to Integer through default comparator

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Shing Hing Man wrote:

>Mark,
>  Thank you for explaining the cause of the
>IllegalArgumentException !
>
>I think there is no need to implement  a 'default
>comparator' 
>to make objects of type like Integer and Long
>comparable.
>  
>
I t would be a useful addition for users who want that functionality 
with java.lang.Numbers and Strings etc, I agree it probably doesn't need 
to be a default. It would make a nice utility for the utils section.

>The current version of the class Frequency would meet
>the needs 
>of most user.  
>
>  
>
Well, the issue goes deeper than comparision of Integer and Long, the 
thrown exception is "obtuse", its unclear from the 
IllegalArgumentException itself why the ClassCastException occurs until 
you look at both the implementation of Frequency and its underlying 
TreeMap. As such I think it is not user friendly. We can do better than 
this.  I agree we can keep the existing expected behavior, but I think 
the Exception handling should be improved or at least made more 
descriptive. The issue has to do with the fact that the methods never 
define the IllegalArgumentException as being "thrown" so user will not 
get a compilation error if they do not handle it appropriately. In the 
end the result would be unexpected behavior (no matter how much is 
documented in the javadoc). The users should be forced to handle the use 
case by catching the Exception in thier code. This could be either by 
throwing the IllegalArgumentException (probably a bad choice) or by 
creating an Exception specifically for this issue and throwing it from 
our methods.

>In most situations, your data could  be casted to a
>common type.
>  
>
The risk with Frequency is that the interface "promotes" heterogeneous  
and incomparable object usage in the methods that are implemented. For 
instance:

    public void addValue(int v) {
        addValue(new Long(v));
    }
    public void addValue(long v) {
        addValue(new Long(v));
    }
    public void addValue(char v) {
        addValue(new Character(v));
    }

The fact that the user can call addValue(long) or addValue(int) and then 
call addValue(char) creates a situation where the 
interface/implementation itself actually promotes the creation of 
incomparable objects in the tree. If this is going to be maintained then 
I would argue there should at least be a comparator that handles these 
cases.

>If a user wants to  add data of different types, or
>even their 
>own custom type, he/she could implement their own
>comparator to
>avoid the ClassCastException, hence the
>IllegalArgumentException,
>when retrieving elements from TreeMap.
>  
>
I think we can easily improve upon the implementation to manage the contents of the Tree better. A good Comparator can provide us better functionality, better Exception handling is important as well.

-Mark 

-- 
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Math] Frequency : Is int comparable to Integer through default comparator

Posted by Shing Hing Man <ma...@yahoo.com>.
Mark,
  Thank you for explaining the cause of the
IllegalArgumentException !

I think there is no need to implement  a 'default
comparator' 
to make objects of type like Integer and Long
comparable.
The current version of the class Frequency would meet
the needs 
of most user.  

In most situations, your data could  be casted to a
common type.
If a user wants to  add data of different types, or
even their 
own custom type, he/she could implement their own
comparator to
avoid the ClassCastException, hence the
IllegalArgumentException,
when retrieving elements from TreeMap.



regards,
Shing


 --- "Mark R. Diggory" <md...@latte.harvard.edu>
wrote: 
> Actually,
> 
> getting the old behavior back should probably look
> more like this. I 
> don't recommend we maintain this as a long term
> solution, we could do 
> some type detection and recognize that a char and a
> String can be equal 
> in some cases, as well, we could modify the class to
> support bins that 
> are "ranges", then frequencies could be done with
> floating point values.
> 
> Right now, if you add floating point values, but
> they get truncated to 
> longs in the comparision and will overwrite the the
> entry, which the 
> user may not see as expected behavior.
> 
> >     private static class NaturalComparator
> implements Comparator {
> >
> >         public int compare(Object o1, Object o2) {
> >             
> >             if(o1 instanceof Number && o2
> instanceof Number){
> >                 long thisVal =
> ((Number)o1).longValue();
> >                 long anotherVal =
> ((Number)o2).longValue();
> >                 return (thisVal<anotherVal ? -1 :
> (thisVal==anotherVal 
> > ? 0 : 1));
> >             }else{
> >                 return
> ((Comparable)o1).compareTo(o2);
> >             }
> >             
> >         }
> >     }
> 
> 
> 
> Mark R. Diggory wrote:
> 
> > It looks as though we will have write our own
> object comparator that 
> > will work with the major objects we want to add to
> the frequecy table. 
> > We need to consider that if we want to be able to
> add any object to 
> > the frequency, that not only does it need to
> extend Comparable, but it 
> > also needs to be castable to any other type stored
> in the Tree.
> >
> > Phil, this looks like an issue that arose when you
> modified Frequency 
> > to use the TreeMap instead of Commons Collections.
> I think we could 
> > still use it if we establish some override on the
> sorting/comparision 
> > mechanism. Like:
> >
> >> /**
> >>      * A Comparator that compares comparable
> objects using the
> >>      * natural order.  Copied from Commons
> Collections 
> >> ComparableComparator.
> >>      */
> >>     private static class NaturalComparator
> implements Comparator {
> >>         /**
> >>          * Compare the two {@link Comparable
> Comparable} arguments.
> >>          * This method is equivalent to:
> >>          * <pre>(({@link Comparable
> Comparable})o1).{@link 
> >> Comparable#compareTo compareTo}(o2)</pre>
> >>          *
> >>          * @param  o1 the first object
> >>          * @param  o2 the second object
> >>          * @return  result of comparison
> >>          * @throws NullPointerException when
> <i>o1</i> is 
> >> <code>null</code>,
> >>          *         or when 
> >> <code>((Comparable)o1).compareTo(o2)</code> does
> >>          * @throws ClassCastException when
> <i>o1</i> is not a {@link 
> >> Comparable Comparable},
> >>          *         or when 
> >> <code>((Comparable)o1).compareTo(o2)</code> does
> >>          */
> >>         public int compare(Object o1, Object o2)
> {
> >>             try{
> >>                 return
> ((Comparable)o1).compareTo(o2);
> >>             }catch(Exception e){
> >>                 return -1;
> >>             }
> >>         }
> >>     }
> >
> >
> >
> > -Mark
> >
> > Mark R. Diggory wrote:
> >
> >> Actually, it looks like the source of this error
> is in a 
> >> Classcastexception when attempting to get the
> object from the 
> >> TreeMap. This is caused because the object (a
> Long) is not an 
> >> "Integer", which the TreeMap is causing in its
> comparison of an 
> >> Integer to a Long object, the comparison of an
> Integer to Long throws 
> >> the class cast exception in the Integer Class.
> >>
> >> It appears we are assuming that Integers and
> Longs can be compared to 
> >> one another just because they Implement
> Comparable is a bad 
> >> assumption, This probably isn't what was
> expected. These objects 
> >> implement Comparable, but they throw exceptions
> when specific 
> >> requirements are not met. I can't say that its a
> very elegant 
> >> solution on Sun's part.
> >>
> >> Ouch <snip from Integer>:
> >>
> >>>    /**
> >>>      * Compares this <code>Integer</code> object
> to another object.
> >>>      * If the object is an <code>Integer</code>,
> this function behaves
> >>>      * like <code>compareTo(Integer)</code>. 
> Otherwise, it throws a
> >>>      * <code>ClassCastException</code> (as
> <code>Integer</code>
> >>>      * objects are only comparable to other
> <code>Integer</code>
> >>>      * objects).
> >>>      *
> >>>      * @param   o the <code>Object</code> to be
> compared.
> >>>      * @return  the value <code>0</code> if the
> argument is a
> >>>      *        <code>Integer</code> numerically
> equal to this
> >>>      *        <code>Integer</code>; a value less
> than <code>0</code>
> >>>      *        if the argument is a
> <code>Integer</code> numerically
> >>>      *        greater than this
> <code>Integer</code>; and a value
> >>>      *        greater than <code>0</code> if the
> argument is a
> >>>      *        <code>Integer</code> numerically
> less than this
> >>>      *        <code>Integer</code>.
> >>>      * @exception
> <code>ClassCastException</code> if the argument is 
> >>> not an
> >>>      *          <code>Integer</code>.
> >>>      * @see     java.lang.Comparable
> >>>      * @since   1.2
> >>>      */
> >>>     public int compareTo(Object o) {
> >>>     return compareTo((Integer)o);
> >>>     }
> >>
> >>
> >>
> >>
> >> Mark R. Diggory wrote:
> >>
> >>> I looked over the code and the default
> comparator uses the default 
> >>> "compare" method of the object. All ints and
> longs are wrapped in a 
> >>> "Long" before being added to the Frequency
> table. I suspect that the 
> >>> Long objects being added cannot be compared to
> the Character objects 
> >>> without throwing an error?
> >>>
> >>> The other thought is that the NaturalComparator
> is not added to 
> >>> default TreeMap when its created.
> >>>
> >>> Frequency.NaturalComparator
> >>>
>
http://jakarta.apache.org/commons/math/xref/org/apache/commons/math/stat/Frequency.html#401
> 
> 
=== message truncated === 

=====
Home page :
  http://uk.geocities.com/matmsh/index.html


	
	
		
___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun!  http://uk.messenger.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Math] Frequency : Is int comparable to Integer through default comparator

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Actually,

getting the old behavior back should probably look more like this. I 
don't recommend we maintain this as a long term solution, we could do 
some type detection and recognize that a char and a String can be equal 
in some cases, as well, we could modify the class to support bins that 
are "ranges", then frequencies could be done with floating point values.

Right now, if you add floating point values, but they get truncated to 
longs in the comparision and will overwrite the the entry, which the 
user may not see as expected behavior.

>     private static class NaturalComparator implements Comparator {
>
>         public int compare(Object o1, Object o2) {
>             
>             if(o1 instanceof Number && o2 instanceof Number){
>                 long thisVal = ((Number)o1).longValue();
>                 long anotherVal = ((Number)o2).longValue();
>                 return (thisVal<anotherVal ? -1 : (thisVal==anotherVal 
> ? 0 : 1));
>             }else{
>                 return ((Comparable)o1).compareTo(o2);
>             }
>             
>         }
>     }



Mark R. Diggory wrote:

> It looks as though we will have write our own object comparator that 
> will work with the major objects we want to add to the frequecy table. 
> We need to consider that if we want to be able to add any object to 
> the frequency, that not only does it need to extend Comparable, but it 
> also needs to be castable to any other type stored in the Tree.
>
> Phil, this looks like an issue that arose when you modified Frequency 
> to use the TreeMap instead of Commons Collections. I think we could 
> still use it if we establish some override on the sorting/comparision 
> mechanism. Like:
>
>> /**
>>      * A Comparator that compares comparable objects using the
>>      * natural order.  Copied from Commons Collections 
>> ComparableComparator.
>>      */
>>     private static class NaturalComparator implements Comparator {
>>         /**
>>          * Compare the two {@link Comparable Comparable} arguments.
>>          * This method is equivalent to:
>>          * <pre>(({@link Comparable Comparable})o1).{@link 
>> Comparable#compareTo compareTo}(o2)</pre>
>>          *
>>          * @param  o1 the first object
>>          * @param  o2 the second object
>>          * @return  result of comparison
>>          * @throws NullPointerException when <i>o1</i> is 
>> <code>null</code>,
>>          *         or when 
>> <code>((Comparable)o1).compareTo(o2)</code> does
>>          * @throws ClassCastException when <i>o1</i> is not a {@link 
>> Comparable Comparable},
>>          *         or when 
>> <code>((Comparable)o1).compareTo(o2)</code> does
>>          */
>>         public int compare(Object o1, Object o2) {
>>             try{
>>                 return ((Comparable)o1).compareTo(o2);
>>             }catch(Exception e){
>>                 return -1;
>>             }
>>         }
>>     }
>
>
>
> -Mark
>
> Mark R. Diggory wrote:
>
>> Actually, it looks like the source of this error is in a 
>> Classcastexception when attempting to get the object from the 
>> TreeMap. This is caused because the object (a Long) is not an 
>> "Integer", which the TreeMap is causing in its comparison of an 
>> Integer to a Long object, the comparison of an Integer to Long throws 
>> the class cast exception in the Integer Class.
>>
>> It appears we are assuming that Integers and Longs can be compared to 
>> one another just because they Implement Comparable is a bad 
>> assumption, This probably isn't what was expected. These objects 
>> implement Comparable, but they throw exceptions when specific 
>> requirements are not met. I can't say that its a very elegant 
>> solution on Sun's part.
>>
>> Ouch <snip from Integer>:
>>
>>>    /**
>>>      * Compares this <code>Integer</code> object to another object.
>>>      * If the object is an <code>Integer</code>, this function behaves
>>>      * like <code>compareTo(Integer)</code>.  Otherwise, it throws a
>>>      * <code>ClassCastException</code> (as <code>Integer</code>
>>>      * objects are only comparable to other <code>Integer</code>
>>>      * objects).
>>>      *
>>>      * @param   o the <code>Object</code> to be compared.
>>>      * @return  the value <code>0</code> if the argument is a
>>>      *        <code>Integer</code> numerically equal to this
>>>      *        <code>Integer</code>; a value less than <code>0</code>
>>>      *        if the argument is a <code>Integer</code> numerically
>>>      *        greater than this <code>Integer</code>; and a value
>>>      *        greater than <code>0</code> if the argument is a
>>>      *        <code>Integer</code> numerically less than this
>>>      *        <code>Integer</code>.
>>>      * @exception <code>ClassCastException</code> if the argument is 
>>> not an
>>>      *          <code>Integer</code>.
>>>      * @see     java.lang.Comparable
>>>      * @since   1.2
>>>      */
>>>     public int compareTo(Object o) {
>>>     return compareTo((Integer)o);
>>>     }
>>
>>
>>
>>
>> Mark R. Diggory wrote:
>>
>>> I looked over the code and the default comparator uses the default 
>>> "compare" method of the object. All ints and longs are wrapped in a 
>>> "Long" before being added to the Frequency table. I suspect that the 
>>> Long objects being added cannot be compared to the Character objects 
>>> without throwing an error?
>>>
>>> The other thought is that the NaturalComparator is not added to 
>>> default TreeMap when its created.
>>>
>>> Frequency.NaturalComparator
>>> http://jakarta.apache.org/commons/math/xref/org/apache/commons/math/stat/Frequency.html#401 
>>>
>>>
>>>>  public int compare(Object o1, Object o2) {
>>>>             return ((Comparable)o1).compareTo(o2);
>>>>          }
>>>
>>>
>>>
>>>
>>>
>>>
>>> Shing Hing Man wrote:
>>>
>>>> Deal all,
>>>>   It would be appreciated if someone could clarify
>>>> the following on org.apache.commons.math.stat.univariate.Frequency.
>>>>
>>>> My version of  org.apache.commons.math is dated 10
>>>> August, 2004.
>>>> The following snippet piece of code is from
>>>> http://jakarta.apache.org/commons/math/userguide/stat.html#1.3%20Frequency%20distributions 
>>>>
>>>>
>>>>
>>>>  Frequency f = new Frequency();
>>>>        f.addValue(1);
>>>>        f.addValue(new Integer(1));   // (*)
>>>>        f.addValue(new Long(1));
>>>>        f.addValue(2);
>>>>        f.addValue(new Integer(-1));
>>>>        System.out.println(f.getCount(1));   //
>>>> displays 3
>>>>        System.out.println(f.getCumPct(0));  //
>>>> displays 0.2
>>>>        System.out.println(f.getPct(new Integer(1))); // displays 0.6
>>>>        System.out.println(f.getCumPct(-2));   //
>>>> displays 0
>>>>        System.out.println(f.getCumPct(10));  //
>>>> displays 1
>>>>
>>>>
>>>> When I run it, I got the following exception at line
>>>> (*).
>>>>
>>>>
>>>> Exception in thread "main"
>>>> java.lang.IllegalArgumentException: Value not
>>>> comparable to
>>>> existing values.
>>>>
>>>> It looks as though the default comparator can not
>>>> compare primitive type int with
>>>> Integer object. I thought this piece of code is
>>>> supposed to demonstrate the otherwise.
>>>>
>>>> Thanks in advice for your help !
>>>>
>>>> Shing
>>>>
>>>>
>>>> =====
>>>> Home page :
>>>>  http://uk.geocities.com/matmsh/index.html
>>>>
>>>>
>>>>                
>>>> ___________________________________________________________ALL-NEW 
>>>> Yahoo! Messenger - all new features - even more fun!  
>>>> http://uk.messenger.yahoo.com
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>>
>>>>  
>>>>
>>>
>>>
>>
>>
>
>


-- 
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Math] Frequency : Is int comparable to Integer through default comparator

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
It looks as though we will have write our own object comparator that 
will work with the major objects we want to add to the frequecy table. 
We need to consider that if we want to be able to add any object to the 
frequency, that not only does it need to extend Comparable, but it also 
needs to be castable to any other type stored in the Tree.

Phil, this looks like an issue that arose when you modified Frequency to 
use the TreeMap instead of Commons Collections. I think we could still 
use it if we establish some override on the sorting/comparision 
mechanism. Like:

> /**
>      * A Comparator that compares comparable objects using the
>      * natural order.  Copied from Commons Collections 
> ComparableComparator.
>      */
>     private static class NaturalComparator implements Comparator {
>         /**
>          * Compare the two {@link Comparable Comparable} arguments.
>          * This method is equivalent to:
>          * <pre>(({@link Comparable Comparable})o1).{@link 
> Comparable#compareTo compareTo}(o2)</pre>
>          *
>          * @param  o1 the first object
>          * @param  o2 the second object
>          * @return  result of comparison
>          * @throws NullPointerException when <i>o1</i> is 
> <code>null</code>,
>          *         or when <code>((Comparable)o1).compareTo(o2)</code> 
> does
>          * @throws ClassCastException when <i>o1</i> is not a {@link 
> Comparable Comparable},
>          *         or when <code>((Comparable)o1).compareTo(o2)</code> 
> does
>          */
>         public int compare(Object o1, Object o2) {
>             try{
>                 return ((Comparable)o1).compareTo(o2);
>             }catch(Exception e){
>                 return -1;
>             }
>         }
>     }


-Mark

Mark R. Diggory wrote:

> Actually, it looks like the source of this error is in a 
> Classcastexception when attempting to get the object from the TreeMap. 
> This is caused because the object (a Long) is not an "Integer", which 
> the TreeMap is causing in its comparison of an Integer to a Long 
> object, the comparison of an Integer to Long throws the class cast 
> exception in the Integer Class.
>
> It appears we are assuming that Integers and Longs can be compared to 
> one another just because they Implement Comparable is a bad 
> assumption, This probably isn't what was expected. These objects 
> implement Comparable, but they throw exceptions when specific 
> requirements are not met. I can't say that its a very elegant solution 
> on Sun's part.
>
> Ouch <snip from Integer>:
>
>>    /**
>>      * Compares this <code>Integer</code> object to another object.
>>      * If the object is an <code>Integer</code>, this function behaves
>>      * like <code>compareTo(Integer)</code>.  Otherwise, it throws a
>>      * <code>ClassCastException</code> (as <code>Integer</code>
>>      * objects are only comparable to other <code>Integer</code>
>>      * objects).
>>      *
>>      * @param   o the <code>Object</code> to be compared.
>>      * @return  the value <code>0</code> if the argument is a
>>      *        <code>Integer</code> numerically equal to this
>>      *        <code>Integer</code>; a value less than <code>0</code>
>>      *        if the argument is a <code>Integer</code> numerically
>>      *        greater than this <code>Integer</code>; and a value
>>      *        greater than <code>0</code> if the argument is a
>>      *        <code>Integer</code> numerically less than this
>>      *        <code>Integer</code>.
>>      * @exception <code>ClassCastException</code> if the argument is 
>> not an
>>      *          <code>Integer</code>.
>>      * @see     java.lang.Comparable
>>      * @since   1.2
>>      */
>>     public int compareTo(Object o) {
>>     return compareTo((Integer)o);
>>     }
>
>
>
> Mark R. Diggory wrote:
>
>> I looked over the code and the default comparator uses the default 
>> "compare" method of the object. All ints and longs are wrapped in a 
>> "Long" before being added to the Frequency table. I suspect that the 
>> Long objects being added cannot be compared to the Character objects 
>> without throwing an error?
>>
>> The other thought is that the NaturalComparator is not added to 
>> default TreeMap when its created.
>>
>> Frequency.NaturalComparator
>> http://jakarta.apache.org/commons/math/xref/org/apache/commons/math/stat/Frequency.html#401 
>>
>>
>>>  public int compare(Object o1, Object o2) {
>>>             return ((Comparable)o1).compareTo(o2);
>>>          }
>>
>>
>>
>>
>>
>> Shing Hing Man wrote:
>>
>>> Deal all,
>>>   It would be appreciated if someone could clarify
>>> the following on org.apache.commons.math.stat.univariate.Frequency.
>>>
>>> My version of  org.apache.commons.math is dated 10
>>> August, 2004.
>>> The following snippet piece of code is from
>>> http://jakarta.apache.org/commons/math/userguide/stat.html#1.3%20Frequency%20distributions 
>>>
>>>
>>>
>>>  Frequency f = new Frequency();
>>>        f.addValue(1);
>>>        f.addValue(new Integer(1));   // (*)
>>>        f.addValue(new Long(1));
>>>        f.addValue(2);
>>>        f.addValue(new Integer(-1));
>>>        System.out.println(f.getCount(1));   //
>>> displays 3
>>>        System.out.println(f.getCumPct(0));  //
>>> displays 0.2
>>>        System.out.println(f.getPct(new Integer(1))); // displays 0.6
>>>        System.out.println(f.getCumPct(-2));   //
>>> displays 0
>>>        System.out.println(f.getCumPct(10));  //
>>> displays 1
>>>
>>>
>>> When I run it, I got the following exception at line
>>> (*).
>>>
>>>
>>> Exception in thread "main"
>>> java.lang.IllegalArgumentException: Value not
>>> comparable to
>>> existing values.
>>>
>>> It looks as though the default comparator can not
>>> compare primitive type int with
>>> Integer object. I thought this piece of code is
>>> supposed to demonstrate the otherwise.
>>>
>>> Thanks in advice for your help !
>>>
>>> Shing
>>>
>>>
>>> =====
>>> Home page :
>>>  http://uk.geocities.com/matmsh/index.html
>>>
>>>
>>>                
>>> ___________________________________________________________ALL-NEW 
>>> Yahoo! Messenger - all new features - even more fun!  
>>> http://uk.messenger.yahoo.com
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>
>>>  
>>>
>>
>>
>
>


-- 
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Math] Frequency : Is int comparable to Integer through default comparator

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Actually, it looks like the source of this error is in a 
Classcastexception when attempting to get the object from the TreeMap. 
This is caused because the object (a Long) is not an "Integer", which 
the TreeMap is causing in its comparison of an Integer to a Long object, 
the comparison of an Integer to Long throws the class cast exception in 
the Integer Class.

It appears we are assuming that Integers and Longs can be compared to 
one another just because they Implement Comparable is a bad assumption, 
This probably isn't what was expected. These objects implement 
Comparable, but they throw exceptions when specific requirements are not 
met. I can't say that its a very elegant solution on Sun's part.

Ouch <snip from Integer>:

>    /**
>      * Compares this <code>Integer</code> object to another object.
>      * If the object is an <code>Integer</code>, this function behaves
>      * like <code>compareTo(Integer)</code>.  Otherwise, it throws a
>      * <code>ClassCastException</code> (as <code>Integer</code>
>      * objects are only comparable to other <code>Integer</code>
>      * objects).
>      *
>      * @param   o the <code>Object</code> to be compared.
>      * @return  the value <code>0</code> if the argument is a
>      *        <code>Integer</code> numerically equal to this
>      *        <code>Integer</code>; a value less than <code>0</code>
>      *        if the argument is a <code>Integer</code> numerically
>      *        greater than this <code>Integer</code>; and a value
>      *        greater than <code>0</code> if the argument is a
>      *        <code>Integer</code> numerically less than this
>      *        <code>Integer</code>.
>      * @exception <code>ClassCastException</code> if the argument is 
> not an
>      *          <code>Integer</code>.
>      * @see     java.lang.Comparable
>      * @since   1.2
>      */
>     public int compareTo(Object o) {
>     return compareTo((Integer)o);
>     }


Mark R. Diggory wrote:

> I looked over the code and the default comparator uses the default 
> "compare" method of the object. All ints and longs are wrapped in a 
> "Long" before being added to the Frequency table. I suspect that the 
> Long objects being added cannot be compared to the Character objects 
> without throwing an error?
>
> The other thought is that the NaturalComparator is not added to 
> default TreeMap when its created.
>
> Frequency.NaturalComparator
> http://jakarta.apache.org/commons/math/xref/org/apache/commons/math/stat/Frequency.html#401 
>
>
>>  public int compare(Object o1, Object o2) {
>>             return ((Comparable)o1).compareTo(o2);
>>          }
>
>
>
>
> Shing Hing Man wrote:
>
>> Deal all,
>>   It would be appreciated if someone could clarify
>> the following on org.apache.commons.math.stat.univariate.Frequency.
>>
>> My version of  org.apache.commons.math is dated 10
>> August, 2004.
>> The following snippet piece of code is from
>> http://jakarta.apache.org/commons/math/userguide/stat.html#1.3%20Frequency%20distributions 
>>
>>
>>
>>  Frequency f = new Frequency();
>>        f.addValue(1);
>>        f.addValue(new Integer(1));   // (*)
>>        f.addValue(new Long(1));
>>        f.addValue(2);
>>        f.addValue(new Integer(-1));
>>        System.out.println(f.getCount(1));   //
>> displays 3
>>        System.out.println(f.getCumPct(0));  //
>> displays 0.2
>>        System.out.println(f.getPct(new Integer(1))); // displays 0.6
>>        System.out.println(f.getCumPct(-2));   //
>> displays 0
>>        System.out.println(f.getCumPct(10));  //
>> displays 1
>>
>>
>> When I run it, I got the following exception at line
>> (*).
>>
>>
>> Exception in thread "main"
>> java.lang.IllegalArgumentException: Value not
>> comparable to
>> existing values.
>>
>> It looks as though the default comparator can not
>> compare primitive type int with
>> Integer object. I thought this piece of code is
>> supposed to demonstrate the otherwise.
>>
>> Thanks in advice for your help !
>>
>> Shing
>>
>>
>> =====
>> Home page :
>>  http://uk.geocities.com/matmsh/index.html
>>
>>
>>     
>>     
>>        
>> ___________________________________________________________ALL-NEW 
>> Yahoo! Messenger - all new features - even more fun!  
>> http://uk.messenger.yahoo.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>  
>>
>
>


-- 
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Math] Frequency : Is int comparable to Integer through default comparator

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
I looked over the code and the default comparator uses the default 
"compare" method of the object. All ints and longs are wrapped in a 
"Long" before being added to the Frequency table. I suspect that the 
Long objects being added cannot be compared to the Character objects 
without throwing an error?

The other thought is that the NaturalComparator is not added to default 
TreeMap when its created.

Frequency.NaturalComparator
http://jakarta.apache.org/commons/math/xref/org/apache/commons/math/stat/Frequency.html#401

>  public int compare(Object o1, Object o2) {
>             return ((Comparable)o1).compareTo(o2);
>          }



 Shing Hing Man wrote:

>Deal all,
>   It would be appreciated if someone could clarify
>the following 
> on 
>org.apache.commons.math.stat.univariate.Frequency.
>
>My version of  org.apache.commons.math is dated 10
>August, 2004.
>The following snippet piece of code is from 
>
>http://jakarta.apache.org/commons/math/userguide/stat.html#1.3%20Frequency%20distributions
>
>
>  Frequency f = new Frequency();
>        f.addValue(1);
>        f.addValue(new Integer(1));   // (*)
>        f.addValue(new Long(1));
>        f.addValue(2);
>        f.addValue(new Integer(-1));
>        System.out.println(f.getCount(1));   //
>displays 3
>        System.out.println(f.getCumPct(0));  //
>displays 0.2
>        System.out.println(f.getPct(new Integer(1))); 
>// displays 0.6
>        System.out.println(f.getCumPct(-2));   //
>displays 0
>        System.out.println(f.getCumPct(10));  //
>displays 1
>
>
>When I run it, I got the following exception at line
>(*).
>
>
>Exception in thread "main"
>java.lang.IllegalArgumentException: Value not
>comparable to
> existing values.
>
>It looks as though the default comparator can not
>compare primitive type int with
>Integer object. I thought this piece of code is
>supposed to demonstrate the otherwise.
>
>Thanks in advice for your help !
>
>Shing
>
>
>=====
>Home page :
>  http://uk.geocities.com/matmsh/index.html
>
>
>	
>	
>		
>___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun!  http://uk.messenger.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>  
>


-- 
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org