1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.List;
20  
21  /**
22   * <a href="TranslatedList.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public abstract class TranslatedList<E, F> extends ListWrapper<E> {
27  
28      public TranslatedList(List<E> newList, List<F> oldList) {
29          super(newList);
30  
31          _oldList = oldList;
32      }
33  
34      public boolean add(E o) {
35          _oldList.add(toOldObject(o));
36  
37          return super.add(o);
38      }
39  
40      public void add(int index, E element) {
41          _oldList.add(index, toOldObject(element));
42  
43          super.add(index, element);
44      }
45  
46      public boolean addAll(Collection<? extends E> c) {
47          for (E o : c) {
48              _oldList.add(toOldObject(o));
49          }
50  
51          return super.addAll(c);
52      }
53  
54      public boolean addAll(int index, Collection<? extends E> c) {
55          for (E o : c) {
56              _oldList.add(index++, toOldObject(o));
57          }
58  
59          return super.addAll(c);
60      }
61  
62      public boolean remove(Object o) {
63          _oldList.remove(toOldObject((E)o));
64  
65          return super.remove(o);
66      }
67  
68      public E remove(int index) {
69          _oldList.remove(index);
70  
71          return super.remove(index);
72      }
73  
74      public boolean removeAll(Collection<?> c) {
75          List<F> tempList = new ArrayList<F>();
76  
77          for (Object o : c) {
78              tempList.add(toOldObject((E)o));
79          }
80  
81          _oldList.removeAll(tempList);
82  
83          return super.removeAll(c);
84      }
85  
86      public boolean retainAll(Collection<?> c) {
87          List<F> tempList = new ArrayList<F>();
88  
89          for (Object o : c) {
90              tempList.add(toOldObject((E)o));
91          }
92  
93          _oldList.retainAll(tempList);
94  
95          return super.retainAll(c);
96      }
97  
98      public E set(int index, E element) {
99          _oldList.set(index, toOldObject(element));
100 
101         return super.set(index, element);
102     }
103 
104     public List<E> subList(int fromIndex, int toIndex) {
105         List<E> newList = super.subList(fromIndex, toIndex);
106         List<F> oldList = _oldList.subList(fromIndex, toIndex);
107 
108         return newInstance(newList, oldList);
109     }
110 
111     protected abstract TranslatedList<E, F> newInstance(
112         List<E> newList, List<F> oldList);
113 
114     protected abstract F toOldObject(E o);
115 
116     private List<F> _oldList;
117 
118 }