Menu

Generic types

Robin Hillyard

Generic types

As mentioned in the [Details] document, the bound on F, at least for the basic Fuzzy<f> type, is: F extends Comparable<f></f>. This obviously ensures that it is possible to tell which of two F objects is the greater (or if they are the same).</f>

In the unit tests, I have tried to give an idea of how uncertainty of text strings might be handled using regular expressions. So, there, F is String.

But for the more typical case where the F quantity is numeric, we clearly want X to implement Number. Here, we hit the first snag: Number is an abstract type (which does nothing other than define methods like intValue() and doubleValue()) so if we bind X to Number instead of Comparable, we'd get what we need, right? Wrong. Number doesn't implement Comparable<number></number>. Fortunately, the designers of the generics concept in Java recognized this possibility and provided syntax to have multiple bounds. Thus, the definition of FuzzyNumber:

public interface FuzzyNumber<X extends Number & Comparable<X>> extends Fuzzy<X> {
    ...
}

Note that for the sake of trying to distinguish the different generic types according to their bounds, we generally use X for numeric fuzzy types.

Acceptable types

The concrete sub-classes of Number do in fact all implement Comparable. For example, Integer is defined thus:

public final class Integer extends Number implements Comparable<Integer> {
    ...
}

It's a pity about that final modifier. That's another thing that makes dealing with numbers in Java so maddeningly awkward. Yes, I understand about polymorphism and the performance penalty but I wish there was another way.

So, in place of X, you can have Integer, Double, BigInteger, BigDecimal, Long, Short, etc. That looks like a really useful wide menu of types.

But, remember, if you provide an expression parser that instantiates and operates on Fuzzy<double> for instance you cannot at the same time create an object of type Fuzzy<integer> and expect it to play well with its friend. Well, you could try having a generator of Fuzzy objects, say an expression parser, which would use the most appropriate number type to store the value in. You could then write something like Fuzzy<?> x = getFuzzy(expression). The problem with that approach is that it doesn't work. You cannot declare a concrete expression parser based on a generic type "?". And various other workarounds don't appear to work either. If you can make it work, please let me know.</integer></double>

So, you really need a numeric type that is both Comparable and a Number, and also can take on any form of number. That's where ComparableNumber and the other special numeric types come in.

Special Numeric Types

First, just note that you don't need to use any of the following types. You can use, say, Double or BigDecimal as your generic type. But there are several advantages to using the following types:

  • Arithmetic
  • ComparableNumber
  • Rational

Arithmetic is an interface which defines many of the common arithmetic operations, as well as the methods of Number (although it cannot extend Number obviously). However, one very useful method signature is asNumber() which returns the object as a Number.

There is are two concrete extenders of Arithmetic: ComparableNumber which is a wrapper around a Number and additionally implements Comparable<comparablenumber></comparablenumber>; and Rational is simply a Number whose value is the ratio of two long numbers.


Related

Wiki: Details
Wiki: Installation and Building

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.