View Javadoc

1   /*
2    * $Id: OrderedSet.java,v 1.2 2008/02/25 14:05:43 oeuillot Exp $
3    */
4   package org.rcfaces.core.lang;
5   
6   import java.io.Serializable;
7   import java.util.AbstractSet;
8   import java.util.ArrayList;
9   import java.util.Collection;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import org.rcfaces.core.model.ICommitableObject;
14  
15  /**
16   * 
17   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
18   * @version $Revision: 1.2 $ $Date: 2008/02/25 14:05:43 $
19   */
20  public class OrderedSet extends AbstractSet implements Cloneable, Serializable,
21          ICommitableObject {
22      private static final String REVISION = "$Revision: 1.2 $";
23  
24      private static final long serialVersionUID = -8481215239333898818L;
25  
26      private List order;
27  
28      private boolean commited;
29  
30      public OrderedSet() {
31          order = new ArrayList();
32      }
33  
34      public OrderedSet(int size) {
35          order = new ArrayList(size);
36      }
37  
38      public OrderedSet(Collection collection) {
39          this(collection.size());
40  
41          addAll(collection);
42      }
43  
44      public Iterator iterator() {
45          final Iterator it = order.iterator();
46  
47          return new Iterator() {
48              private static final String REVISION = "$Revision: 1.2 $";
49  
50              public boolean hasNext() {
51                  return it.hasNext();
52              }
53  
54              public Object next() {
55                  return it.next();
56              }
57  
58              public void remove() {
59                  if (commited) {
60                      throw new IllegalStateException("Already commited set.");
61                  }
62  
63                  it.remove();
64              }
65  
66          };
67      }
68  
69      public boolean contains(Object o) {
70          return order.contains(o);
71      }
72  
73      public boolean containsAll(Collection c) {
74          return order.containsAll(c);
75      }
76  
77      public boolean add(Object o) {
78          if (commited) {
79              throw new IllegalStateException("Already commited set.");
80          }
81  
82          if (order.contains(o)) {
83              return false;
84          }
85  
86          return order.add(o);
87      }
88  
89      public boolean remove(Object o) {
90          if (commited) {
91              throw new IllegalStateException("Already commited set.");
92          }
93  
94          return order.remove(o);
95      }
96  
97      public boolean removeAll(Collection c) {
98          if (commited) {
99              throw new IllegalStateException("Already commited set.");
100         }
101 
102         return order.removeAll(c);
103     }
104 
105     public int size() {
106         return order.size();
107     }
108 
109     public void clear() {
110         if (commited) {
111             throw new IllegalStateException("Already commited set.");
112         }
113 
114         order.clear();
115     }
116 
117     public Object[] toArray() {
118         return order.toArray();
119     }
120 
121     public Object[] toArray(Object[] a) {
122         return order.toArray(a);
123     }
124 
125     public int hashCode() {
126         return order.hashCode();
127     }
128 
129     public boolean equals(Object obj) {
130         if (this == obj) {
131             return true;
132         }
133 
134         if (getClass() != obj.getClass()) {
135             return false;
136         }
137 
138         final OrderedSet other = (OrderedSet) obj;
139         if (order == null) {
140             if (other.order != null) {
141                 return false;
142             }
143 
144         } else if (!order.equals(other.order)) {
145             return false;
146         }
147 
148         return true;
149     }
150 
151     public boolean retainAll(Collection c) {
152         if (commited) {
153             throw new IllegalStateException("Already commited set.");
154         }
155 
156         return order.retainAll(c);
157     }
158 
159     public String toString() {
160         return order.toString();
161     }
162 
163     public Object clone() {
164         try {
165             OrderedSet newSet = (OrderedSet) super.clone();
166             newSet.order = new ArrayList(order);
167 
168             return newSet;
169 
170         } catch (CloneNotSupportedException ex) {
171             throw new RuntimeException(ex);
172         }
173     }
174 
175     public void commit() {
176         if (order instanceof ArrayList) {
177             ((ArrayList) order).trimToSize();
178         }
179         this.commited = true;
180     }
181 
182     public boolean isCommited() {
183         return commited;
184     }
185 
186 }