001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
033            public boolean add(E o) {
034                    _oldList.add(toOldObject(o));
035    
036                    return super.add(o);
037            }
038    
039            @Override
040            public void add(int index, E element) {
041                    _oldList.add(index, toOldObject(element));
042    
043                    super.add(index, element);
044            }
045    
046            @Override
047            public boolean addAll(Collection<? extends E> c) {
048                    for (E o : c) {
049                            _oldList.add(toOldObject(o));
050                    }
051    
052                    return super.addAll(c);
053            }
054    
055            @Override
056            public boolean addAll(int index, Collection<? extends E> c) {
057                    for (E o : c) {
058                            _oldList.add(index++, toOldObject(o));
059                    }
060    
061                    return super.addAll(c);
062            }
063    
064            @Override
065            public E remove(int index) {
066                    _oldList.remove(index);
067    
068                    return super.remove(index);
069            }
070    
071            @Override
072            public boolean remove(Object o) {
073                    _oldList.remove(toOldObject((E)o));
074    
075                    return super.remove(o);
076            }
077    
078            @Override
079            public boolean removeAll(Collection<?> c) {
080                    List<F> tempList = new ArrayList<F>();
081    
082                    for (Object o : c) {
083                            tempList.add(toOldObject((E)o));
084                    }
085    
086                    _oldList.removeAll(tempList);
087    
088                    return super.removeAll(c);
089            }
090    
091            @Override
092            public boolean retainAll(Collection<?> c) {
093                    List<F> tempList = new ArrayList<F>();
094    
095                    for (Object o : c) {
096                            tempList.add(toOldObject((E)o));
097                    }
098    
099                    _oldList.retainAll(tempList);
100    
101                    return super.retainAll(c);
102            }
103    
104            @Override
105            public E set(int index, E element) {
106                    _oldList.set(index, toOldObject(element));
107    
108                    return super.set(index, element);
109            }
110    
111            @Override
112            public List<E> subList(int fromIndex, int toIndex) {
113                    List<E> newList = super.subList(fromIndex, toIndex);
114                    List<F> oldList = _oldList.subList(fromIndex, toIndex);
115    
116                    return newInstance(newList, oldList);
117            }
118    
119            protected abstract TranslatedList<E, F> newInstance(
120                    List<E> newList, List<F> oldList);
121    
122            protected abstract F toOldObject(E o);
123    
124            private List<F> _oldList;
125    
126    }