17

I am new to generics. You can see I am repeating some code after knowing the exact type of val, filterSmall, filterGreat. I want to write generic code for comparing val against filter values. I could write something like this

  private  <T> boolean  compareAgainstFilters(T val, T filterSmall, T filterGreat) {
    if (!(filterSmall != null && filterSmall <= val)) {
        return true;
    } 

    if (!(filterGreat != null && val <= filterGreat)) {
        return true;
    }
    return true;
}

but at compile time, java wouldn't know if the <= operator is valid for type T.

I don't want to repeat the code, so how can I achieve that?

if (value != null) {
        switch (value.getClass().getName()) {
        case "java.lang.Long":
            Long filterSmall = (Long) filterSmaller;
            Long filterGreat = (Long) filterGreater;
            Long val = (Long) value;

            if (!(filterSmall != null && filterSmall <= val)) {
                return true;
            } 

            if (!(filterGreat != null && val <= filterGreat)) {
                return true;
            }
            break;

        case "java.lang.Float":
            Float filterSmallFloat = (Float) filterSmaller;
            Float filterGreatFloat = (Float) filterGreater;
            Float valFloat = (Float) value;

            if (!(filterSmallFloat != null && filterSmallFloat <= valFloat)) {
                return true;
            } 

            if (!(filterGreatFloat != null && valFloat <= filterGreatFloat)) {
                return true;
            }
        }
    }
5
  • 1
    I think you have a small bug in your logic: you return true if val is greater than filterSmall, even if it is greater than filterGreat. Furthermore, you return true if val is smaller than filterGreat, even if it is smaller than filterSmall. Finally, I think you want to return false; if neither if-clauses were true. Commented Jul 26, 2015 at 15:56
  • How is your question specific to java 8? Commented Jul 26, 2015 at 16:08
  • I wasn't sure if there could be a better way to solve this in Java 8. Guess not. Commented Jul 26, 2015 at 16:11
  • A side note: This switch over the class name looks odd for me. Such type checks should be avoided in general, but I see that this can be hard with Number objects, so if you really really have to do such tests, you should use if (value instanceof Float)... Commented Jul 27, 2015 at 13:47
  • @Marco13 yeah I will take that into consideration! Thanks. Commented Jul 27, 2015 at 14:00

2 Answers 2

24

You can use the Comparable interface for comparing numbers, since all the wrapper classes of numeric primitives implement it :

  private  <T extends Comparable<T>> boolean  compareAgainstFilters(T val, T filterSmall, T filterGreat) {
    if (!(filterSmall != null && filterSmall.compareTo(val)<=0)) {
        return true;
    } 

    if (!(filterGreat != null && val.compareTo(filterGreat)<=0)) {
        return true;
    }
    return true;
}

<T extends Comparable<T>> restricts the types that can be used as type arguments instead of T. In this case they are required to implement Comparable<T>.

Sign up to request clarification or add additional context in comments.

Comments

4

You cannot use the < or > operators in the generic method. Normally when the variable is of type Float or Long, the compiler would accept the operator after doing automatic unboxing conversion (to get the primitive type) in the compiled code. In other words,

filterSmall <= val

is compiled to:

filterSmall.longValue() <= val.longValue()

In the generic method, there is no way for the compiler to apply unboxing conversion. The only way to perform comparison is to have the type parameter extend the Comparable interface, and use the compareTo method. Note that the types Float, Long and so on already implement this interface.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.