View Javadoc
1   /*
2    * #%L
3    * Nuiton Utils
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  /* *
24   * HashList.java
25   *
26   * Created: 2 nov. 2004
27   *
28   * @author Benjamin Poussin - poussin@codelutin.com
29   *
30   *
31   * Mise a jour: $Date$
32   * par : */
33  
34  package org.nuiton.util;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashSet;
39  
40  /**
41   * Cette objet permet de gerer l'unicité des objets ajouté.
42   * Lorsque l'on appelle la methode add sur cette objet, il verifie
43   * en premier que l'objet n'est pas deja dans la liste. S'il y est
44   * alors il n'est pas ajouter, sinon il est ajouter.
45   * L'utilisation de la methode set n'est pas permise
46   *
47   * FIXME: la serialisation n'est pas modifier, c-a-d que seul le tableau est
48   * conserve et pas la hashSet, donc apres recuperation, l'uticite n'est plus
49   * garantie. Il faut donc surcharger readObject et writeObject pour conserver
50   * le HashSet
51   */
52  public class HashList<E> extends ArrayList<E> { // HashList
53  
54      /**  */
55      private static final long serialVersionUID = -334941610313293930L;
56  
57      protected HashSet<E> set = new HashSet<E>();
58  
59      public HashList() {
60      }
61  
62      public HashList(Collection<? extends E> c) {
63          addAll(c);
64      }
65  
66      public HashList(int initialCapacity) {
67          super(initialCapacity);
68      }
69  
70      @Override
71      public E set(int index, E element) {
72          throw new UnsupportedOperationException("You can't use set method in HashList");
73      }
74  
75      @Override
76      public boolean add(E o) {
77          boolean result = !contains(o);
78          add(size(), o);
79          return result;
80      }
81  
82      @Override
83      public void add(int index, E element) {
84          if (set.add(element)) {
85              super.add(index, element);
86          }
87      }
88  
89      /**
90       * supprime l'element demandé. Si l'elment n'existe pas alors, null
91       * est retrouné.
92       */
93      @Override
94      public E remove(int index) {
95          if (set.remove(get(index))) {
96              return super.remove(index);
97          }
98          return null;
99      }
100 
101     @Override
102     public void clear() {
103         set.clear();
104         super.clear();
105     }
106 
107     @Override
108     public boolean addAll(Collection<? extends E> c) {
109         boolean modified = false;
110         for (E aC : c) {
111             if (add(aC))
112                 modified = true;
113         }
114         return modified;
115 
116     }
117 
118     @Override
119     public boolean addAll(int index, Collection<? extends E> c) {
120         boolean modified = false;
121         for (E aC : c) {
122             add(index++, aC);
123             modified = true;
124         }
125         return modified;
126 
127     }
128 
129     @Override
130     protected void removeRange(int fromIndex, int toIndex) {
131         for (int i = toIndex - 1; i >= fromIndex; i--) {
132             remove(i);
133         }
134     }
135 
136     @Override
137     public boolean contains(Object elem) {
138         return set.contains(elem);
139     }
140 
141     @Override
142     public Object clone() {
143         HashList<E> result = new HashList<E>(this);
144         return result;
145     }
146 
147 } // HashList
148