1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.util;
16  
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.Iterator;
20  
21  /**
22   * <a href="UniqueList.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class UniqueList<E> extends ArrayList<E> {
27  
28      public UniqueList() {
29          super();
30      }
31  
32      public boolean add(E e) {
33          if (!contains(e)) {
34              return super.add(e);
35          }
36          else {
37              return false;
38          }
39      }
40  
41      public void add(int index, E e) {
42          if (!contains(e)) {
43              super.add(index, e);
44          }
45      }
46  
47      public boolean addAll(Collection<? extends E> c) {
48          c = new ArrayList<E>(c);
49  
50          Iterator<? extends E> itr = c.iterator();
51  
52          while (itr.hasNext()) {
53              E e = itr.next();
54  
55              if (contains(e)) {
56                  itr.remove();
57              }
58          }
59  
60          return super.addAll(c);
61      }
62  
63      public boolean addAll(int index, Collection<? extends E> c) {
64          c = new ArrayList<E>(c);
65  
66          Iterator<? extends E> itr = c.iterator();
67  
68          while (itr.hasNext()) {
69              E e = itr.next();
70  
71              if (contains(e)) {
72                  itr.remove();
73              }
74          }
75  
76          return super.addAll(index, c);
77      }
78  
79      public E set(int index, E e) {
80          if (!contains(e)) {
81              return super.set(index, e);
82          }
83          else {
84              return e;
85          }
86      }
87  
88  }