View Javadoc

1   /*
2    * $Id: CollectionIndexesModel.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    */
4   package org.rcfaces.core.model;
5   
6   import java.io.Serializable;
7   import java.util.ArrayList;
8   import java.util.Arrays;
9   import java.util.Collection;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  /**
14   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
15   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
16   */
17  public class CollectionIndexesModel extends AbstractIndexesModel implements
18          Serializable, ICommitableObject {
19      private static final String REVISION = "$Revision: 1.18 $";
20  
21      private static final long serialVersionUID = -3821092264981658279L;
22  
23      protected static final int[] EMPTY_SELECTION = new int[0];
24  
25      protected static final int UNKNOWN_INDEX = -1;
26  
27      protected final Collection collection;
28  
29      protected boolean commited;
30  
31      public CollectionIndexesModel(Collection collection) {
32          this.collection = collection;
33      }
34  
35      public int getFirstIndex() {
36          if (collection.isEmpty()) {
37              return -1;
38          }
39  
40          if (collection instanceof List) {
41              return getIndex(((List) collection).get(0));
42          }
43  
44          return getIndex(collection.iterator().next());
45      }
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see org.rcfaces.core.model.IIndexesModel#listIndexes()
51       */
52      public final int[] listSortedIndexes() {
53          if (collection.isEmpty()) {
54              return EMPTY_SELECTION;
55          }
56  
57          int n[] = new int[collection.size()];
58          int pos = 0;
59          int unknownIndex = getUnknownIndex();
60          for (Iterator it = collection.iterator(); it.hasNext();) {
61              int idx = getIndex(it.next());
62  
63              if (idx == unknownIndex) {
64                  continue;
65              }
66  
67              n[pos++] = idx;
68          }
69  
70          if (pos == n.length) {
71              if (n.length > 1) {
72                  Arrays.sort(n);
73              }
74              return n;
75          }
76  
77          int n2[] = new int[pos];
78  
79          System.arraycopy(n, 0, n2, 0, pos);
80  
81          if (n2.length > 1) {
82              Arrays.sort(n2);
83          }
84  
85          return n2;
86      }
87  
88      /*
89       * (non-Javadoc)
90       * 
91       * @see org.rcfaces.core.model.IIndexesModel#clearIndexes()
92       */
93      public final void clearIndexes() {
94          if (commited) {
95              throw new IllegalStateException("Already commited indexes model.");
96          }
97  
98          collection.clear();
99      }
100 
101     /*
102      * (non-Javadoc)
103      * 
104      * @see org.rcfaces.core.model.IIndexesModel#containsIndex(int)
105      */
106     public final boolean containsIndex(int index) {
107         return collection.contains(getKey(index));
108     }
109 
110     protected int getIndex(Object object) {
111         if (object instanceof Number) {
112             return ((Number) object).intValue();
113         }
114 
115         return getUnknownIndex();
116     }
117 
118     protected Object getKey(int index) {
119         return new Integer(index);
120     }
121 
122     protected int getUnknownIndex() {
123         return UNKNOWN_INDEX;
124     }
125 
126     /*
127      * (non-Javadoc)
128      * 
129      * @see org.rcfaces.core.model.IIndexesModel#addIndex(int)
130      */
131     public boolean addIndex(int index) {
132         if (commited) {
133             throw new IllegalStateException("Already commited indexes model.");
134         }
135 
136         return collection.add(getKey(index));
137     }
138 
139     /*
140      * (non-Javadoc)
141      * 
142      * @see org.rcfaces.core.model.IIndexesModel#removeIndex(int)
143      */
144     public final boolean removeIndex(int index) {
145         if (commited) {
146             throw new IllegalStateException("Already commited indexes model.");
147         }
148 
149         return collection.remove(getKey(index));
150     }
151 
152     /*
153      * (non-Javadoc)
154      * 
155      * @see org.rcfaces.core.model.IIndexesModel#setIndexes(int[])
156      */
157     public void setIndexes(int[] indexes) {
158         if (commited) {
159             throw new IllegalStateException("Already commited indexes model.");
160         }
161 
162         clearIndexes();
163 
164         if (indexes == null || indexes.length < 1) {
165             return;
166         }
167 
168         for (int i = 0; i < indexes.length; i++) {
169             int val = indexes[i];
170             if (val < 0) {
171                 continue;
172             }
173 
174             addIndex(val);
175         }
176     }
177 
178     /*
179      * (non-Javadoc)
180      * 
181      * @see org.rcfaces.core.model.IIndexesModel#countIndexes()
182      */
183     public int countIndexes() {
184         if (collection.isEmpty()) {
185             return 0;
186         }
187 
188         int count = 0;
189         int unknownIndex = getUnknownIndex();
190         for (Iterator it = collection.iterator(); it.hasNext();) {
191             int idx = getIndex(it.next());
192 
193             if (idx == unknownIndex) {
194                 continue;
195             }
196 
197             count++;
198         }
199 
200         return count;
201     }
202 
203     public void commit() {
204         this.commited = true;
205 
206         if (collection instanceof ArrayList) {
207             ((ArrayList) collection).trimToSize();
208         }
209     }
210 
211     public boolean isCommited() {
212         return commited;
213     }
214 
215     public IIndexesModel copy() {
216         return new CollectionIndexesModel(collection);
217     }
218 }