001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.Collection;
019: import java.util.List;
020: import java.util.ListIterator;
021:
022: import org.apache.commons.collections.Transformer;
023: import org.apache.commons.collections.collection.TransformedCollection;
024: import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
025:
026: /**
027: * Decorates another <code>List</code> to transform objects that are added.
028: * <p>
029: * The add and set methods are affected by this class.
030: * Thus objects must be removed or searched for using their transformed form.
031: * For example, if the transformation converts Strings to Integers, you must
032: * use the Integer form to remove objects.
033: * <p>
034: * This class is Serializable from Commons Collections 3.1.
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: */
041: public class TransformedList extends TransformedCollection implements
042: List {
043:
044: /** Serialization version */
045: private static final long serialVersionUID = 1077193035000013141L;
046:
047: /**
048: * Factory method to create a transforming list.
049: * <p>
050: * If there are any elements already in the list being decorated, they
051: * are NOT transformed.
052: *
053: * @param list the list to decorate, must not be null
054: * @param transformer the transformer to use for conversion, must not be null
055: * @throws IllegalArgumentException if list or transformer is null
056: */
057: public static List decorate(List list, Transformer transformer) {
058: return new TransformedList(list, transformer);
059: }
060:
061: //-----------------------------------------------------------------------
062: /**
063: * Constructor that wraps (not copies).
064: * <p>
065: * If there are any elements already in the list being decorated, they
066: * are NOT transformed.
067: *
068: * @param list the list to decorate, must not be null
069: * @param transformer the transformer to use for conversion, must not be null
070: * @throws IllegalArgumentException if list or transformer is null
071: */
072: protected TransformedList(List list, Transformer transformer) {
073: super (list, transformer);
074: }
075:
076: /**
077: * Gets the decorated list.
078: *
079: * @return the decorated list
080: */
081: protected List getList() {
082: return (List) collection;
083: }
084:
085: //-----------------------------------------------------------------------
086: public Object get(int index) {
087: return getList().get(index);
088: }
089:
090: public int indexOf(Object object) {
091: return getList().indexOf(object);
092: }
093:
094: public int lastIndexOf(Object object) {
095: return getList().lastIndexOf(object);
096: }
097:
098: public Object remove(int index) {
099: return getList().remove(index);
100: }
101:
102: //-----------------------------------------------------------------------
103: public void add(int index, Object object) {
104: object = transform(object);
105: getList().add(index, object);
106: }
107:
108: public boolean addAll(int index, Collection coll) {
109: coll = transform(coll);
110: return getList().addAll(index, coll);
111: }
112:
113: public ListIterator listIterator() {
114: return listIterator(0);
115: }
116:
117: public ListIterator listIterator(int i) {
118: return new TransformedListIterator(getList().listIterator(i));
119: }
120:
121: public Object set(int index, Object object) {
122: object = transform(object);
123: return getList().set(index, object);
124: }
125:
126: public List subList(int fromIndex, int toIndex) {
127: List sub = getList().subList(fromIndex, toIndex);
128: return new TransformedList(sub, transformer);
129: }
130:
131: /**
132: * Inner class Iterator for the TransformedList
133: */
134: protected class TransformedListIterator extends
135: AbstractListIteratorDecorator {
136:
137: protected TransformedListIterator(ListIterator iterator) {
138: super (iterator);
139: }
140:
141: public void add(Object object) {
142: object = transform(object);
143: iterator.add(object);
144: }
145:
146: public void set(Object object) {
147: object = transform(object);
148: iterator.set(object);
149: }
150: }
151:
152: }
|