public class Foo<T> {
private T value;
public Foo(T value){
this.value = value;
}
public Foo<T> Is(){
return this;
}
// At this point, I want a T that implements (extends) Comparable
// So i can compare the value.
// How to do it in Java.
public Foo<T extends Comparable> GreaterThanBar(T anotherValue)
{
return this;
}
}
This is like a builder pattern I want to do, like fluent style. Chain the methods. etc.
I have the above class, I m trying to convert from C# to Java. In C# I can add the method that where T: IComprable, it works.
How do i do this with Java? I m trying to do this on GreaterThanBar method, so that I can comparare the values.
public <T extends Comparable> Foo<T> GreaterThan(T another)
{
if(another.compareTo(value) > 0){
throw new IllegalArgumentException();
}
return (Foo<T>) this;
}
is this the correct way of doing it? Eclipse told me to cast it but i d like to know why i need to cast it to that?
and this worked.
is there a better way to do this?
As a C# developer, I really liked how this is easy to do with C#.
this(i.e. your class) extendValidation<T>? Otherwise your cast is wrong, right?