Suggestion for Improving Number
James Genac Sep 26, 2012 3:54 PMHello Oracle Java community,
I've recently encountered some difficulties using the abstract class java.lang.Number, and have a suggestion for improvement.
I'm writing a class that computes statistical information on a list of numbers - it would be nice to not couple this class to Integer, Double, BigDecimal, or any other wrapper by using generics. I saw that there is a nice superclass that all Number objects inherit from.
I came up with:
--------
public class Statistics<T extends Number> {
private List<T> data;
// statistical data that i wish to find and store, such as median, mean, standard dev, etc
public synchronized void setData(List<T> data) {
this.data = data;
if (this.data != null && !this.data.isEmpty()) calculateStatistics();
}
private void calculateStatistics() {
// Welcome to instanceof and casting hell...
}
//...
}
------
h4. It would be nice to have richer functionality from the Number class, say to do mathematical operations with them or compare them.
h4. After all, in the real world it is possible to do so.
h4. Real numbers are much like BigDecimal. Why not take the idea of BigDecimal, and make that the parent of Integer, BigInteger, Double, Short, Byte, Float (I'm probably forgetting a few)? All of those are limited forms of real numbers. It would make comparison between Number datatypes easy, would probably remove all of that duplicated arithmetic code between all of the children of Number, and also allow Numbers to be used in powerful generic ways. The parent/replacement of BigDecimal could even be named RealNumber, which stays true to its math domain.
As a side note, I'm solving this problem by taking an initial step to convert the List<whatever type of Number that the user enters> into a List<BigDecimal> by getting the toString() value of each element when cast as a Number.
-------
private List<BigDecimal> convertData(List<T> data) {
ArrayList<BigDecimal> converted = new ArrayList<BigDecimal>();
for (T element : data) {
converted.add(new BigDecimal(((Number) element).toString()));
}
return converted;
}
-------
Criticism is always welcome.
Thanks for your time and thoughts.
-James Genac
I've recently encountered some difficulties using the abstract class java.lang.Number, and have a suggestion for improvement.
I'm writing a class that computes statistical information on a list of numbers - it would be nice to not couple this class to Integer, Double, BigDecimal, or any other wrapper by using generics. I saw that there is a nice superclass that all Number objects inherit from.
I came up with:
--------
public class Statistics<T extends Number> {
private List<T> data;
// statistical data that i wish to find and store, such as median, mean, standard dev, etc
public synchronized void setData(List<T> data) {
this.data = data;
if (this.data != null && !this.data.isEmpty()) calculateStatistics();
}
private void calculateStatistics() {
// Welcome to instanceof and casting hell...
}
//...
}
------
h4. It would be nice to have richer functionality from the Number class, say to do mathematical operations with them or compare them.
h4. After all, in the real world it is possible to do so.
h4. Real numbers are much like BigDecimal. Why not take the idea of BigDecimal, and make that the parent of Integer, BigInteger, Double, Short, Byte, Float (I'm probably forgetting a few)? All of those are limited forms of real numbers. It would make comparison between Number datatypes easy, would probably remove all of that duplicated arithmetic code between all of the children of Number, and also allow Numbers to be used in powerful generic ways. The parent/replacement of BigDecimal could even be named RealNumber, which stays true to its math domain.
As a side note, I'm solving this problem by taking an initial step to convert the List<whatever type of Number that the user enters> into a List<BigDecimal> by getting the toString() value of each element when cast as a Number.
-------
private List<BigDecimal> convertData(List<T> data) {
ArrayList<BigDecimal> converted = new ArrayList<BigDecimal>();
for (T element : data) {
converted.add(new BigDecimal(((Number) element).toString()));
}
return converted;
}
-------
Criticism is always welcome.
Thanks for your time and thoughts.
-James Genac