commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bruno P. Kinoshita (JIRA)" <>
Subject [jira] [Resolved] (MATH-1413) Add generics to Frequency
Date Wed, 10 May 2017 08:09:04 GMT


Bruno P. Kinoshita resolved MATH-1413.
    Resolution: Fixed

> Add generics to Frequency
> -------------------------
>                 Key: MATH-1413
>                 URL:
>             Project: Commons Math
>          Issue Type: Bug
>            Reporter: Bruno P. Kinoshita
>            Assignee: Bruno P. Kinoshita
>            Priority: Minor
>              Labels: generics
>             Fix For: 4.0
> The Frequency class has no generic types, but creates an internal TreeMap, with keys
with the type `Comparator<?>`. Then whenever you add or get values from the TreeMap,
it will throw CCE if you pass a key value with a different type from the others stored in
the map.
> {code}
> Frequency f = new Frequency();
> // Okay
> f.addValue("Ola");
> // Key 12, type Integer, does not match String type. CCE, wrapped in a Math exception
> f.addValue(12);
> {code}
> And even a simpler example without Math classes to help outlining the issue.
> {code}
> TreeMap<Integer, String> map = new TreeMap<>();
> int key = 1;
> map.put(key, "One");
> // Okay
> map.get(100);
> // TreeMap will try to call 1.1#compareTo against each Key it contains, which can result
> // in a CCE here.
> map.get(1.1);
> {code}
> It is not clear how we could compare the key type to the given v value. Checking if the
map is not empty, and then checking the type of the first key doesn't sound very elegant (and
not sure if it would be a valid solution).
> A better solution could be to add generics to the class, breaking the current contract
(i.e. not allowing three types, boxing to longs, etc), but on the bright side, it won't use
CCE as a normal workflow, nor need suppress warnings annotations any more.

This message was sent by Atlassian JIRA

View raw message