Why can't I set a Transformer to a LazyList in Apache Commons?
In apache.commons.collections there is a class called MapUtils which have these two methods to define a Map which can create on demand objects for the map:
- lazyMap(Map,Factory)
- lazyMap(Map,Transformer)
So I can use a factory to instantiate the object
Factory factory = new Factory() {
public Object create() {
return new Object();
}
}
or a transformer to instantiate the new object depending on the key of the map
Transformer factory = new Transformer() {
public Object transform(Object mapKey) {
return new Object(mapKey);
}
}
There's a similar class for Lists: ListUtils, but this class only has a method with a Factory:
- lazyList(List list, Factory factory)
I'd 开发者_JAVA技巧like to transform the object like in the map situation but using the index of the object in the list instead of the key in the map.
Transformer factory = new Transformer() {
public Object transform(int index) {
return new Object(index);
}
}
My question is why there is not a lazyList(List list, Transformer transformer)? Does apache provide any other List to do this or do I have to build my custom implementation?
Thanks.
First of all, in my opinion you should use Guava for this sort of thing... it makes full use of generics and provides a much more well thought-out, compact and sensible API. It also provides a Lists.transform method which transforms an underlying List
based on the elements at each position in the list.
That said, I don't think a transform method for transforming a List
by index makes much sense. The actual underlying List
would be completely meaningless given that the transformation would ignore the elements it contains... only its size would matter. Could you explain why you would want to do something like that?
精彩评论