eeijlar
eeijlar

Reputation: 1340

How can I make this Java method completely generic

I have a method which calculates the 95th percentile of an ArrayList of type Float containing statistics data:

    public String calculatePercentile(ArrayList<Float> arrayOfStats) {
    this.statistics = (ArrayList<Float>) arrayOfStats.clone();
    int N = statistics.size();
    int integerPart =0;
    int fractionalPart =0;
    int fraction =0;
    float rank =0;
    float floatPoint =0;
    float interpolate=0;
    float interpolateFirstPart =0;
    float interpolateSecondPart =0;

      if (N == 0) {
        return Float.toString(0);
       } else {   
         Collections.sort(statistics);
         rank = (float) ((P / 100.0) * (N - 1));
            if (rank == Math.round(rank)) {
              return Float.toString(statistics.get((int) rank));               
            } else {
                String split = Float.toString(rank);
                Pattern pattern = Pattern.compile(floatRegExPattern);
                Matcher matcher = pattern.matcher(split); 
                while(matcher.find()) {
                integerPart = Integer.parseInt(matcher.group(1));
                fractionalPart = Integer.parseInt(matcher.group(3));
                }

                if (fractionalPart < 10) {
                    floatPoint = (float) (fractionalPart / 10);
                } else {
                    floatPoint = (float) fractionalPart / 100;
                }        
         fraction = integerPart + 1;
         interpolateFirstPart = statistics.get(fraction);
         interpolateSecondPart = statistics.get(integerPart);
         interpolate = interpolateFirstPart - interpolateSecondPart;
         return roundToTwoDecimalPlaces((floatPoint * interpolate) + interpolateFirstPart);
       }
      }
}

My question is how can I make this method generic so that it can not just accept and calculate ArrayLists of type Float, but it can also do Integers etc. I have tried to use templates such as

ArrayList<? as Number> 

but when I get to Collections.sort, it complains and I couldn't figure out what was wrong. The return value needs to be a string.

Upvotes: 0

Views: 88

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234847

You can use

public <T extends Number> String calculatePercentile(ArrayList<T> arrayOfStats) {

and then use Number.floatValue to retrieve each stat as a float. You'll probably have to use this to write your own comparator to do the sorting. A comparator might look like this:

Comparator<Number> c = new Comparator<Number>() {
    public int compare(Number a, Number b) {
        return Float.compare(a.floatValue(), b.floatValue());
    }
}

Incidentally, a better way to assign to statistics would be:

this.statistics = new ArrayList<Number>(arrayOfStats);

You don't need to clone and cast.

Upvotes: 3

Related Questions