1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  /**
27   * <a href="TranslatedList.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   *
31   */
32  public abstract class TranslatedList<E, F> extends ListWrapper<E> {
33  
34      public TranslatedList(List<E> newList, List<F> oldList) {
35          super(newList);
36  
37          _oldList = oldList;
38      }
39  
40      public boolean add(E o) {
41          _oldList.add(toOldObject(o));
42  
43          return super.add(o);
44      }
45  
46      public void add(int index, E element) {
47          _oldList.add(index, toOldObject(element));
48  
49          super.add(index, element);
50      }
51  
52      public boolean addAll(Collection<? extends E> c) {
53          for (E o : c) {
54              _oldList.add(toOldObject(o));
55          }
56  
57          return super.addAll(c);
58      }
59  
60      public boolean addAll(int index, Collection<? extends E> c) {
61          for (E o : c) {
62              _oldList.add(index++, toOldObject(o));
63          }
64  
65          return super.addAll(c);
66      }
67  
68      public boolean remove(Object o) {
69          _oldList.remove(toOldObject((E)o));
70  
71          return super.remove(o);
72      }
73  
74      public E remove(int index) {
75          _oldList.remove(index);
76  
77          return super.remove(index);
78      }
79  
80      public boolean removeAll(Collection<?> c) {
81          List<F> tempList = new ArrayList<F>();
82  
83          for (Object o : c) {
84              tempList.add(toOldObject((E)o));
85          }
86  
87          _oldList.removeAll(tempList);
88  
89          return super.removeAll(c);
90      }
91  
92      public boolean retainAll(Collection<?> c) {
93          List<F> tempList = new ArrayList<F>();
94  
95          for (Object o : c) {
96              tempList.add(toOldObject((E)o));
97          }
98  
99          _oldList.retainAll(tempList);
100 
101         return super.retainAll(c);
102     }
103 
104     public E set(int index, E element) {
105         _oldList.set(index, toOldObject(element));
106 
107         return super.set(index, element);
108     }
109 
110     public List<E> subList(int fromIndex, int toIndex) {
111         List<E> newList = super.subList(fromIndex, toIndex);
112         List<F> oldList = _oldList.subList(fromIndex, toIndex);
113 
114         return newInstance(newList, oldList);
115     }
116 
117     protected abstract TranslatedList<E, F> newInstance(
118         List<E> newList, List<F> oldList);
119 
120     protected abstract F toOldObject(E o);
121 
122     private List<F> _oldList;
123 
124 }