Problem of defender methods with generic methods
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Fri Nov 5 21:55:28 PDT 2010
Hi Maurizio,
when compiling the following code I get two error.
public interface CustomizedList<T> extends List<T> {
extension CustomizedList<T> filter(Predicate<T> predicate) default
Trait.<T>filter;
<R> extension CustomizedList<R> map(Extrator<T,R> predicate)
default Trait.<T,R>map; <========== cannot find symbol R
<A extends Comparable<? super A>> extension A max() default
Trait.<A>max; <============= cannot find symbol A
public static class Trait {
public static <T> CustomizedList<T> filter(
final CustomizedList<T> self, final Predicate<T> predicate) {
CustomizedList<T> result=null;
try {
result = self.getClass().newInstance();
} catch (Throwable e) {
e.printStackTrace();
}
for(T elm:self){
if(predicate.op(elm))
result.add(elm);
}
return result;
}
public static <T,R> CustomizedList<R> map(
final CustomizedList<T> self, final Extrator<T,R> extrator) {
CustomizedList<R> result=null;
try {
result = self.getClass().newInstance();
} catch (Throwable e) {
e.printStackTrace();
}
for(T elm:self){
result.add(extrator.extract(elm));
}
return result;
}
public static <R extends Comparable<? super R>> R max(
final CustomizedList<? extends R> self) {
if(self.size()==0)
return null;
R result=self.get(0);
for(R elm:self){
if(elm.compareTo(result)>0)
result=elm;
}
return result;
}
}
}
Best Regards,
Ali Ebrahimi
More information about the lambda-dev
mailing list