KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TransformedList


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.Collection JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ListIterator JavaDoc;
21
22 import org.apache.commons.collections.Transformer;
23 import org.apache.commons.collections.collection.TransformedCollection;
24 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
25
26 /**
27  * Decorates another <code>List</code> to transform objects that are added.
28  * <p>
29  * The add and set methods are affected by this class.
30  * Thus objects must be removed or searched for using their transformed form.
31  * For example, if the transformation converts Strings to Integers, you must
32  * use the Integer form to remove objects.
33  * <p>
34  * This class is Serializable from Commons Collections 3.1.
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
38  *
39  * @author Stephen Colebourne
40  */

41 public class TransformedList extends TransformedCollection implements List JavaDoc {
42
43     /** Serialization version */
44     private static final long serialVersionUID = 1077193035000013141L;
45
46     /**
47      * Factory method to create a transforming list.
48      * <p>
49      * If there are any elements already in the list being decorated, they
50      * are NOT transformed.
51      *
52      * @param list the list to decorate, must not be null
53      * @param transformer the transformer to use for conversion, must not be null
54      * @throws IllegalArgumentException if list or transformer is null
55      */

56     public static List JavaDoc decorate(List JavaDoc list, Transformer transformer) {
57         return new TransformedList(list, transformer);
58     }
59     
60     //-----------------------------------------------------------------------
61
/**
62      * Constructor that wraps (not copies).
63      * <p>
64      * If there are any elements already in the list being decorated, they
65      * are NOT transformed.
66      *
67      * @param list the list to decorate, must not be null
68      * @param transformer the transformer to use for conversion, must not be null
69      * @throws IllegalArgumentException if list or transformer is null
70      */

71     protected TransformedList(List JavaDoc list, Transformer transformer) {
72         super(list, transformer);
73     }
74
75     /**
76      * Gets the decorated list.
77      *
78      * @return the decorated list
79      */

80     protected List JavaDoc getList() {
81         return (List JavaDoc) collection;
82     }
83
84     //-----------------------------------------------------------------------
85
public Object JavaDoc get(int index) {
86         return getList().get(index);
87     }
88
89     public int indexOf(Object JavaDoc object) {
90         return getList().indexOf(object);
91     }
92
93     public int lastIndexOf(Object JavaDoc object) {
94         return getList().lastIndexOf(object);
95     }
96
97     public Object JavaDoc remove(int index) {
98         return getList().remove(index);
99     }
100
101     //-----------------------------------------------------------------------
102
public void add(int index, Object JavaDoc object) {
103         object = transform(object);
104         getList().add(index, object);
105     }
106
107     public boolean addAll(int index, Collection JavaDoc coll) {
108         coll = transform(coll);
109         return getList().addAll(index, coll);
110     }
111
112     public ListIterator JavaDoc listIterator() {
113         return listIterator(0);
114     }
115
116     public ListIterator JavaDoc listIterator(int i) {
117         return new TransformedListIterator(getList().listIterator(i));
118     }
119
120     public Object JavaDoc set(int index, Object JavaDoc object) {
121         object = transform(object);
122         return getList().set(index, object);
123     }
124
125     public List JavaDoc subList(int fromIndex, int toIndex) {
126         List JavaDoc sub = getList().subList(fromIndex, toIndex);
127         return new TransformedList(sub, transformer);
128     }
129
130     /**
131      * Inner class Iterator for the TransformedList
132      */

133     protected class TransformedListIterator extends AbstractListIteratorDecorator {
134         
135         protected TransformedListIterator(ListIterator JavaDoc iterator) {
136             super(iterator);
137         }
138         
139         public void add(Object JavaDoc object) {
140             object = transform(object);
141             iterator.add(object);
142         }
143         
144         public void set(Object JavaDoc object) {
145             object = transform(object);
146             iterator.set(object);
147         }
148     }
149
150 }
151
Popular Tags