001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.ArrayList;
018    import java.util.Collection;
019    import java.util.List;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public abstract class TranslatedList<E, F> extends ListWrapper<E> {
025    
026            public TranslatedList(List<E> newList, List<F> oldList) {
027                    super(newList);
028    
029                    _oldList = oldList;
030            }
031    
032            public boolean add(E o) {
033                    _oldList.add(toOldObject(o));
034    
035                    return super.add(o);
036            }
037    
038            public void add(int index, E element) {
039                    _oldList.add(index, toOldObject(element));
040    
041                    super.add(index, element);
042            }
043    
044            public boolean addAll(Collection<? extends E> c) {
045                    for (E o : c) {
046                            _oldList.add(toOldObject(o));
047                    }
048    
049                    return super.addAll(c);
050            }
051    
052            public boolean addAll(int index, Collection<? extends E> c) {
053                    for (E o : c) {
054                            _oldList.add(index++, toOldObject(o));
055                    }
056    
057                    return super.addAll(c);
058            }
059    
060            public boolean remove(Object o) {
061                    _oldList.remove(toOldObject((E)o));
062    
063                    return super.remove(o);
064            }
065    
066            public E remove(int index) {
067                    _oldList.remove(index);
068    
069                    return super.remove(index);
070            }
071    
072            public boolean removeAll(Collection<?> c) {
073                    List<F> tempList = new ArrayList<F>();
074    
075                    for (Object o : c) {
076                            tempList.add(toOldObject((E)o));
077                    }
078    
079                    _oldList.removeAll(tempList);
080    
081                    return super.removeAll(c);
082            }
083    
084            public boolean retainAll(Collection<?> c) {
085                    List<F> tempList = new ArrayList<F>();
086    
087                    for (Object o : c) {
088                            tempList.add(toOldObject((E)o));
089                    }
090    
091                    _oldList.retainAll(tempList);
092    
093                    return super.retainAll(c);
094            }
095    
096            public E set(int index, E element) {
097                    _oldList.set(index, toOldObject(element));
098    
099                    return super.set(index, element);
100            }
101    
102            public List<E> subList(int fromIndex, int toIndex) {
103                    List<E> newList = super.subList(fromIndex, toIndex);
104                    List<F> oldList = _oldList.subList(fromIndex, toIndex);
105    
106                    return newInstance(newList, oldList);
107            }
108    
109            protected abstract TranslatedList<E, F> newInstance(
110                    List<E> newList, List<F> oldList);
111    
112            protected abstract F toOldObject(E o);
113    
114            private List<F> _oldList;
115    
116    }