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.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  }