conform equality constraints - java

I wrote some generic method. It works fine with first-level descendants. But it fails with objects of the second-level descendant.
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static <T extends Comparable<T>> int f(List<T> list, T item) {
return 0;
}
static class A implements Comparable<A> {
#Override
public int compareTo(A o) {
return 0;
}
}
static class B extends A {
}
public static void main(String[] args) {
List<A> as = new ArrayList<A>();
List<B> bs = new ArrayList<B>();
A a = new A();
B b = new B();
f(as, a); // works
f(bs, b); // <-------- error
}
}
Compilation fails with:
Error:(29, 9) java: method f in class com.company.Main cannot be applied to
given types;
required: java.util.List<T>,T
found: java.util.List<com.company.Main.B>,com.company.Main.B
reason: inferred type does not conform to equality constraint(s)
inferred: com.company.Main.A
equality constraints(s): com.company.Main.A,com.company.Main.B
Why Java can not guess the equality variable types?

The type parameter T of your function f, has to implement Comparable<T> as per your definition <T extends Comparable<T>>. B does not implement Comparable<B>, but rather Comparable<A>. When you want to be able to call the function with B, the class should implement Comparable<B>, e.g. like so
static class B extends A implements Comparable<B> {
#Override
public int compareTo(B o) {
return super.compareTo(o);
}
}
Otherwise, you could change the type parameter of f to be able to have Ts which are subclasses of a Comparable class.
public static <T extends Comparable<? super T>> int f(List<T> list, T item) {
return 0;
}

Related

seek for help on java generic method

I have following class structure,
abstract class AbstractA {...}
class A1 extends AbstractA {...}
class A2 extends AbstractA {...}
abstract class AbstractB<T extends AbstractA> {
public void handle(T a) { ... }
}
class B1 extends AbstractB<A1> {
public void handle(A1 a) {
super.handle(a);
...
}
}
class B2 extends AbstractB<A2> {
public void handle(A2 a) {
super.handle(a);
...
}
}
Now I want to implement a generic method that would take a list of AbstractB and related AbstractA as parameters. e.g.
Handler.<B1, A1>handle(listOfB1, A1);
Handler.<B2, A2>handle(listOfB2, A2);
and
Handler.<B1, A2>handle(listOfB1, A2);
Handler.<B2, A1>handle(listOfB2, A1);
is not allowed.
I tried
class Handler {
// public static <T extends AbstractB<K extends AbstractA>, K extends AbstractA> handle(List<T> list, K a) {
public static <T extends AbstractB<? extends AbstractA>, K extends AbstractA> handle(List<T> list, K a) {
for (T tmp : list) {
tmp.handle(a);
}
}
}
but both does not compile. Can anyone help and give me any clue? Thanks!
Change it to :
public static <T extends AbstractB<K>, K extends AbstractA> void handle(List<T> list, K a) {
for (T tmp : list) {
tmp.handle(a);
}
}
Note that your method was missing a return type (I'm assuming your intended a void return type).
However, the main issue was that the type bound of T should be extends AbstractB<K> and not extends AbstractB<? extends AbstractA>.
Consider what happens in your current definition of the static handle method.
The current signature of the static method allows this call :
List<B1> listOfB1;
Handler.<B1, A2>handle(listOfB1, A2);
But in the body of the static method, you can't pass an A2 instance to the handle method of a B1 instance, which is why your code doesn't pass compilation. Therefore the type bound of T must depend on K.

Collections.sort() error

I am trying to sort a list of type A named BinOrder in class B according to Class A's int r.
However i am receiving this error for the line Collections.sort(BinOrder);
The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<A>)
Class A:
public class A{
int s;
int r;
public A(int si, int ri) {
s=si;
r= ri;
}
}
Class B:
import java.util.ArrayList;
import java.util.Collections;
public class B implements Comparable<A> {
public Iterator<A> randomMethodName(int a) {
ArrayList<A> BinOrder = new ArrayList<A>();
A a = new A(1,3)
A a2 = new A(1,4)
BinOrder.add(a);
BinOrder.add(a2);
}
// sort array in increasing order of r
Collections.sort(BinOrder);
return BinOrder;
}
#Override
public int compareTo(A list) {
return null;
}
}
To be able to use the single-argument version of Collection.sort() on an ArrayList of A, A should implement the Comparable interface:
public class A implements Comparable<A> {
...
#Override
int compareTo(A rhs) {
...
}
}
Here's the signature of Collections.sort :
public static <T extends Comparable<? super T>> void sort(List<T> list)
A must implement Comparable for this method.
You try to pass BinOrder to this method, when BinOrder is of type ArrayList<A>, but since A does not implement Comparable<A>, it doesn't fit the signature of the method.
Either change A to implement Comparable, or use the sort method that accepts a Comparator :
public static <T> void sort(List<T> list, Comparator<? super T> c)

Bounded types: Multiple bounds

I have read this article here and tried to figure out how to work with bound types. What I try to achieve is a parametrized method that handles four different cases:
T extends B only
T extends B and I (here D)
T extends I only
everything else
So here is the code:
public class Main {
public static void main(String... args) {
B b = new B();
D d = new D();
I i = new I() {
};
handle("aaasd");
handle(b);
handle(d); <---- Problem 1
handle(i);
}
public static class B {
}
public static interface I {
}
public static class D extends B implements I {
}
public static <T> void handle(T objT) {
System.out.println("T");
}
private static <T extends B> void handle(T obj) {
System.out.println("B");
}
public static <T extends B & I> void handle(T objT) { <--- Problem 2
System.out.println("B+I");
}
private static <T extends I> void handle(T obj) {
System.out.println("I");
}
}
The compiler complains and says two things:
Ambiguous call
The method handle(Main.D) is ambiguous for the type Main
I guess the problem is caused by the same cause as Problem number 2. The & I clearly bounds the type of T to a subtype of B AND I thus removing ambiguity in my opinion.
Same erasure handle
Method handle(T) has the same erasure handle(Main.B) as another method in type Main
My guess is that this is the real cause for all the problems. Java somehow removes bounding to I during runtime? But when I call the method with type B this doesn't call the annoted method.
Can someone explain how I fix the problem/distinguish between B, B&I and I?
Java somehow removes bounding to I during runtime?
No, Java removes every type information at runtime (except for reflection purposes) which is called type erasure.
Using bounds the compiler would be able to translate your code to handle(Object), handle(B) and handle(I) but in the T extends B & I case the compiler would get conflicts.
AFAIK, there's no way to fix this without having a common bound, e.g. T extends D instead of T extends B & I where D extends B implements I or to change the method name or add another parameter.
Another way might be to add the logic in the B+I case to either the B or I method and check for the second condition inside, e.g.
private static <T extends B> void handle(T obj) {
if( obj instanceof I) {
System.out.println("B+I");
}
else {
System.out.println("B");
}
}
There's a concept known as type erasure that applies to all generics in Java. With generic methods, after compilation, the methods in the byte code appear as their erasure, so
public static <T> void handle(T objT) {
System.out.println("T");
}
private static <T extends B> void handle(T obj) {
System.out.println("B");
}
public static <T extends B & I> void handle(T objT) { <--- Problem 2
System.out.println("B+I");
}
private static <T extends I> void handle(T obj) {
System.out.println("I");
}
actually become
public static void handle(Object objT) {
System.out.println("T");
}
private static void handle(B obj) {
System.out.println("B");
}
public static void handle(B objT) {
System.out.println("B+I");
}
private static void handle(I obj) {
System.out.println("I");
}
The left-most bound of a type variable is what a parameter of that type gets replaced with. As you can see, both your 2nd and 3rd method have the same name and same parameter types, ie. the same signature. This cannot be allowed by the compiler.
However, the syntax of bounds forces you to provide the class type before any interface types so
<T extends I & B>
wouldn't work. It also wouldn't work because your 4th method would again have the same erasure.
Additionally, invoking
handle(d);
is a problem since both the 2nd and 4th method could handle it, none is more specific. This is known as overloading ambiguity.

Java Generics - inserting inner type parameter

I am new to java. I am just trying to pass Comparable<String> into a method parameter of generic type <E extends Comparable<E>> . I believe the meaning of <E extends Comparable<E>> is any object that extends Comparable. Please let me know how to pass Comparable<String> or any object that extends Comparable<String> and has an other object in it.
Compiler is giving me error The inferred type Compare<String> is not a valid substitute for the bounded parameter <E extends Comparable<E>>
Code:
public class Compare<T> implements Comparable<T>{
public int compareTo(T o) {
return 0; // Not worried about logic
}
}
class CompareTest{
public <E extends Comparable<E>>void testGeneric(E e){
System.out.println("Executed");
}
public static void main(String args[]){
Compare<String> compare = new Compare<String>();
CompareTest test = new CompareTest();
test.testGeneric(compare);
//The inferred type Compare<String> is not a valid substitute for the bounded
//parameter <E extends Comparable<E>>
}
}
E extends Comparable<E> means: a type E that is able to compare to other objects of the same type E.
But your Compare type doesn't qualify. It can't compare with another Compare. A Compare<T> can only compare itself to a T, and not to a Compare<T>, since it's declared as
public class Compare<T> implements Comparable<T>
It's quite hard to understand what you want to achieve with this Compare type.
Your method
public <E extends Comparable<E>> void testGeneric(E e){
expects a type E that is a sub type of Comparable<E>. But you are passing it a Compare<String> which is not a sub type of <Comparable<Compare<String>>, but a sub type of Comparable<String>.
You'll have to clarify what you are trying to do if you need more help.
The error in my IDE says:
Inferred type 'Compare<java.lang.String>' for type parameter 'E' is not within its bound;
should implement 'java.lang.Comparable<Compare<java.lang.String>>'
It seems that E is inferred as Compare<String> instead of String. To get E to be String, try this:
public <E extends Comparable<E>> void testGeneric(Comparable<E> e){
To define a class as being comparable, the generic parameter to Comparable must be the class itself:
public class MyComparable implements Comparable<MyComparable> {
public int compareTo(MyComparable o) {
return 0;
}
}
Applying that to your class, you get the following code (which compiles):
public static class Compare<T> implements Comparable<Compare<T>> {
public int compareTo(Compare<T> o) {
return 0; // Not worried about logic
}
}
class CompareTest {
public <E extends Comparable<E>> void testGeneric(E e) {
System.out.println("Executed");
}
}
public static void main(String[] args) {
Compare<String> compare = new Compare<String>();
CompareTest test = new CompareTest();
test.testGeneric(compare);
}

Implementing a method with generics relating the return and parameter types specified in an interface that does not do this

I am with a sort of trouble when using java generics in the visitor pattern.
My code is something like that:
public interface MyInterfaceVisitor<A, B> {
public A visitMyConcreteObject(MyConcreteObject object, B parameter);
}
public interface MyObject {
public <A, B> A accept(MyInterfaceVisitor<A, B> visitor, B parameter);
}
public class MyConcreteObject implements MyObject {
#Override
public <A, B> A accept(MyInterfaceVisitor<A, B> visitor, B parameter) {
return visitor.visitMyConcreteObject(this, parameter);
}
}
public class MyConcreteVisitor implements MyInterfaceVisitor<????> {
#Override
public <X extends C> X visitMyConcreteObject(MyConcreteObject object, Class<X> parameter) {
// Do a lot of things.
// Return an instance of the given class.
}
// This method is the entry point of the MyConcreteVisitor.
public <X extends C> void someOtherMethod(Class<X> parameter) {
MyObject m = ...;
X x = m.accept(this, parameter);
...;
}
}
public class C {}
public class Dog extends C {}
public class Cat extends C {}
public class Client {
public static void main(String... args) {
MyConcreteVisitor v = new MyConcreteVisitor();
v.someOtherMethod(Cat.class);
v.someOtherMethod(Dog.class);
}
}
// We have other implementations of the visitor that does not matters, like this one.
public class SomeOtherConcreteVisitor implements MyInterfaceVisitor<String, Integer> {
#Override
public String visitMyConcreteObject(MyConcreteObject object, Integer parameter) {
return "foo";
}
}
I need to find what is the generic signature in the ???? that makes the code compilable allowing the overriden method in MyConcreteVisitor class to match the signature in MyInterfaceVisitor interface.
I can't change the signature of the visitMyObject in the MyInterfaceVisitor interface, nor its generics. This happens because others implementations of MyInterfaceVisitor exists and their generics have nothing to with the ones from MyConcreteVisitor.
The MyConcreteVisitor class should not have a generic per-se, so the compiler must allow a MyConcreteVisitor v = new MyConcreteVisitor(); without generating the unchecked or rawtypes warning.
If I change the concrete visitMyObject to public C visitMyObject(MyObject object, Class<? extends C> parameter) and declare the ???? as <C, Class<? extends C>>, I would need to add a cast in the someOtherMethod.
How to define the generic type making it compilable without getting the unchecked or rawtypes warning, changing the interface or adding a cast? Is this even possible in java or I am abusing the generics too much?
The issue is that your implementation is trying to introduce another type parameter X extends C to the method visitMyConcreteObject and resolve the B parameter with it. You can't make visitMyConcreteObject generic with X but try to resolve B with a type parameterized by X, e.g. Class<X>, because B is resolved at the class declaration but X is only declared by a method of the class.
From what I can see, you have two options. Either make MyConcreteVisitor generic on X:
public class MyConcreteVisitor<X extends C> implements MyInterfaceVisitor<X, Class<X>> {
#Override
public X visitMyConcreteObject(MyConcreteObject object, Class<X> parameter) {
// Do a lot of things.
// Return an instance of the given class.
}
}
Or get rid of X and lose type safety (beyond the concrete type C):
public class MyConcreteVisitor implements MyInterfaceVisitor<C, Class<? extends C>> {
#Override
public C visitMyConcreteObject(MyConcreteObject object, Class<? extends C> parameter) {
// Do a lot of things.
// Return an instance of the given class.
}
}
i think this is what you are looking for:
public class MyConcreteVisitor implements MyInterfaceVisitor<Object,Class<?>> {
#Override
public Object visitMyConcreteObject(MyConcreteObject object, Class<?> parameter) {
// Do a lot of things.
// Return an instance of the given class.
}
// This method is the entry point of the MyConcreteVisitor.
public <X> void someOtherMethod(Class<X> parameter) {
MyObject m = ...;
X x = parameter.cast(m.accept(this, parameter));
...;
}
}

Categories