I have a class MyGen which takes two generic arguments and has a method getValue()
public interface MyGen<E,T> {
T getValue();
}
Usually the second generic type would be either Long or Integer.
Then, I wrote a method to combine the objects when the second generic type is Long as below:
public static <E extends MyGen<?, Long>> long combineValue(Set<E> set) {
return set.stream()
.map(MyGen::getValue)
.reduce(0L, (a,b) -> a | b);
}
Now I want to have a similar method when the second type is an Integer. So I tried updating the above same method to :
public static <E extends MyGen<?, ? extends Number>> long combineValue(Set<E> set) {
return (long) set.stream()
.map(MyGen::getValue)
.reduce((a,b) -> a | b) // error1
.orElse(0); // error2
}
But the following errors are displayed:
- error1
The operator | is undefined for the argument type(s) capture#4-of ? extends java.lang.Number, capture#4-of ? extends java.lang.Number
- error2
The method orElse(capture#4-of ? extends Number) in the type Optional<capture#4-of ? extends Number> is not applicable for the arguments (int)
Is there any way to handle both Long and Integer in the same method or could it be done only using two separate methods?