I have the following interface:
public interface Caster{
public boolean tryCast(Object value);
}
and its implementations:
public class IntegerCaster{
public boolean tryCast(Object value){
try{
Integer.class.cast(value);
return true;
} catch (ClassCastException e){
return false;
}
}
}
public class DateCaster{
public boolean tryCast(Object value){
try{
Date.class.cast(value);
return true;
} catch (ClassCastException e){
return false;
}
}
}
Is it possible to make such implementation generic? We can't quite take and declare Caster with type parameter, because we won't be able implement it as follows:
public interface Caster<T>{
public boolean tryCast(Object value);
}
public class CasterImpl<T> implements Caster<T>{
public boolean tryCast(Object value){
try{
T.class.cast(value); //fail
return true;
} catch (ClassCastException e){
return false;
}
}
}
Integer.class.isInstance,Date.class.isInstance, etc instead of this interface?