KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > CachingTransformingList


1 /*
2  * CachingTransformingList.java
3  */

4 package polyglot.util;
5
6 import java.util.*;
7
8 /**
9  * This subclass of TransformingList applies the transformation to each
10  * element of the underlying list at most once.
11  */

12 public class CachingTransformingList extends TransformingList {
13     ArrayList cache;
14
15     public CachingTransformingList(Collection underlying,
16                    Transformation trans)
17     {
18         this(new ArrayList(underlying), trans);
19     }
20
21     public CachingTransformingList(List underlying, Transformation trans) {
22     super(underlying, trans);
23     cache = new ArrayList(Collections.nCopies(underlying.size(), null));
24     }
25
26     public Object JavaDoc get(int index) {
27     Object JavaDoc o = cache.get(index);
28     if (o == null) {
29         o = trans.transform(underlying.get(index));
30         cache.set(index, o);
31     }
32     return o;
33     }
34 }
35
Popular Tags