#How can I compare Type Parameter T2 with T?
47 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @quartz lotus! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
T1 and T2 are different types, so they should never be equals anyways. This breaks all laws.
I mean they CAN be the same type, but if you're comparing them then they SHOULD be the same type.
You can check if they are derrived from the same class and then cast them accordingly if you want
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
sure you don't want public Intervall<T> schnitt(Intervall<T> anderes) for the intersection?
that would enforce both being the same type
This is the method signature which my professor gave me: public <T2> Intervall<T> schnitt(Intervall<T2> anderes) and I should think of an bound for T2 with which I can solve the task: form an intersection of this and anderes. And I thought about comparing the this-intervall with anderes-Intervall to check if this is within anderes
I want later one to check if Java.sql.time is within the Java.util.date Intervall for example
then one would need to be comparable to the other
So do I really need to cast T2?
no
you need to provide some way of comparing them to each other
if you have a common superclass that is comparable, you could just use intervals of that
Ok I’m going to try that I thought I already tried using lower bounds
thats what I get from that, I tried that earlier. Im definitely understanding something wrong
from my understanding firstly T2 needs to extend comparable, so it is comparable at all. Also T2 has to be a superclass of T in order to make sure that T is comparable with T2
public class Intervall<T extends Comparable<T>> {
private T untereGrenze;
private T obereGrenze;
public Intervall(T untereGrenze, T obereGrenze) {
this.untereGrenze = untereGrenze;
this.obereGrenze = obereGrenze;
}
public boolean isLeer() {
return untereGrenze == null || obereGrenze == null
|| obereGrenze.compareTo(untereGrenze) < 0 || untereGrenze.compareTo(obereGrenze) > 0;
}
public <T1 extends Comparable<? super T>> boolean enthaelt(T1 wert) {
return (wert.compareTo(untereGrenze) > 0 && wert.compareTo(obereGrenze) < 0
|| wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) == 0
|| wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) < 0
|| wert.compareTo(obereGrenze) == 0 && wert.compareTo(untereGrenze) > 0);
}
public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<T2> anderes) {
return null;
}
}
public <T2 extends Intervall<? super T>> Intervall<T> schnitt(T2 anderes){ should work
the issue is that you can't have Intervall<T2> as that would require T2 to extend Comparable<T2>
my professor gave me this method signature: ```java
public <T2> Intervall<T> schnitt(Intervall<T2> anderes)
Think carefully about the bounds for T, T1, and T2 so that the following task is solvable! So i cant change the paramterlist nor the method signature only the bounds
public <T2 extends Comparable<T2>> Intervall<T> schnitt(Intervall<T2> anderes) { would be possible but that wouldn't allow comparing T1 and T2
public <T2 extends Comparable<? extends T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) { maybe?
my fault, should be public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) { I think
sadly this doesnt work either, maybe my approach with comparing the values is wrong? I really tried everything I could think of to make this work
What happens?
that compiles for me
public class Intervall<T extends Comparable<T>> {
private T untereGrenze;
private T obereGrenze;
public Intervall(T untereGrenze, T obereGrenze) {
this.untereGrenze = untereGrenze;
this.obereGrenze = obereGrenze;
}
public boolean isLeer() {
return untereGrenze == null || obereGrenze == null
|| obereGrenze.compareTo(untereGrenze) < 0 || untereGrenze.compareTo(obereGrenze) > 0;
}
public <T1 extends Comparable<? super T>> boolean enthaelt(T1 wert) {
return (wert.compareTo(untereGrenze) > 0 && wert.compareTo(obereGrenze) < 0
|| wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) == 0
|| wert.compareTo(untereGrenze) == 0 && wert.compareTo(obereGrenze) < 0
|| wert.compareTo(obereGrenze) == 0 && wert.compareTo(untereGrenze) > 0);
}
public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) {
anderes.untereGrenze.compareTo(untereGrenze);
return null;
}
}
it says "Provided:
capture of ? extends T2" "Required type:
T"
you mean where you call it?
omg wait I compared it the other way around like this: this.untereGrenze.compareTo(anderes.obereGrenze);
I think u got it right, it works now. Im writing the method now and test if it works, thank you vm for your help
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
and maybe you need to change the class declaration to public class Intervall<T extends Comparable<? super T>>
so for me to understand it: what we did is: T2 extends Comparable<? super T>> meaning, T2 has to be comparable and must be a Superclass of T. (Intervall <? extends T2> anderes) the paramter anderes must be from type Intervall and a Subclass of T2?
can i change my method signatures than to <T1 extends Comparable<T>> and <T2 extends Comparable<T>> ?
yes
What's T1?
public class Intervall<T extends Comparable<? super T>> {
private T untereGrenze;
private T obereGrenze;
public Intervall(T untereGrenze, T obereGrenze) {
this.untereGrenze = untereGrenze;
this.obereGrenze = obereGrenze;
}
public <T2 extends Comparable<? super T>> Intervall<T> schnitt(Intervall<? extends T2> anderes) {
anderes.untereGrenze.compareTo(untereGrenze);
return null;
}
}
import java.time.*;
import java.time.chrono.*;
Intervall<LocalDate> a = new Intervall<>(LocalDate.now(), LocalDate.now());
Intervall<ThaiBuddhistDate> b = new Intervall<>(ThaiBuddhistDate.now(), ThaiBuddhistDate.now());
a.schnitt(b);
seems to work for me
Yes it has to work, it makes all sense, thank you again! T
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
just another method, I will try and see 🙂
Stay healthy!