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