1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.model;
20
21 import java.beans.IntrospectionException;
22
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.NoSuchElementException;
27
28 import javax.faces.model.ArrayDataModel;
29 import javax.faces.model.DataModel;
30 import javax.faces.model.ListDataModel;
31 import javax.faces.model.ScalarDataModel;
32 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
33
34
35
36
37
38 public final class ModelUtils
39 {
40
41
42
43
44
45
46 public static Iterator<Object> getRowKeyIterator(final CollectionModel model)
47 {
48 Iterator<Object> iter = new Iterator<Object>()
49 {
50 public boolean hasNext()
51 {
52 return _next != null;
53 }
54
55 public Object next()
56 {
57 if (_next == null)
58 throw new NoSuchElementException();
59 Object value = _next;
60 _next = _next();
61 return value;
62 }
63
64 public void remove()
65 {
66 throw new UnsupportedOperationException();
67 }
68
69 private Object _next()
70 {
71 int oldIndex = model.getRowIndex();
72 try
73 {
74 model.setRowIndex(_rowIndex++);
75 if (model.isRowAvailable())
76 return model.getRowKey();
77 } finally
78 {
79 model.setRowIndex(oldIndex);
80 }
81 return null;
82 }
83
84 private Object _next = Boolean.TRUE;
85 private int _rowIndex = 0;
86 };
87 iter.next();
88 return iter;
89 }
90
91
92
93
94
95
96
97
98 public static int getRowCount(RowKeyIndex model)
99 {
100 int rowCount = model.getRowCount();
101
102 if (rowCount >= 0)
103 {
104 return rowCount;
105 }
106
107 int lowerBound = 0;
108 int upperBound = 100;
109 for(model.setRowIndex(upperBound); model.isRowAvailable();)
110 {
111 lowerBound = upperBound;
112 upperBound <<= 1;
113 model.setRowIndex(upperBound);
114 }
115 return findLastIndex(model, lowerBound, upperBound);
116 }
117
118
119
120
121
122
123
124
125
126
127 public static int findLastIndex(RowKeyIndex table,
128 int startIndex, int endIndex)
129 {
130 int rowCount = table.getRowCount();
131
132 if (rowCount >= 0)
133 {
134
135 if (rowCount < endIndex)
136 endIndex = rowCount;
137
138
139 }
140
141 if (table.isRowAvailable(endIndex - 1))
142 return endIndex;
143
144 final int old = table.getRowIndex();
145 try
146 {
147 while(startIndex < endIndex)
148 {
149
150 int middle = (startIndex + endIndex) / 2;
151
152
153 assert (middle != endIndex) :
154 "something is grossly wrong with integer division";
155
156 table.setRowIndex(middle);
157 if (table.isRowAvailable())
158 startIndex = middle + 1;
159 else
160 endIndex = middle;
161 }
162 return endIndex;
163 }
164 finally
165 {
166 table.setRowIndex(old);
167 }
168 }
169
170
171
172
173
174 public static TreeModel toTreeModel(Object value)
175 {
176 if (value instanceof TreeModel)
177 return (TreeModel) value;
178
179
180
181
182
183
184
185 return new ChildPropertyTreeModel(value, null);
186 }
187
188
189
190
191 public static MenuModel toMenuModel(Object value)
192 {
193 if (value instanceof MenuModel)
194 return (MenuModel) value;
195 else
196 {
197 try
198 {
199 return new ViewIdPropertyMenuModel(value, null);
200 }
201 catch (IntrospectionException e)
202 {
203 IllegalArgumentException re =
204 new IllegalArgumentException(_LOG.getMessage(
205 "CANNOT_CONVERT_INTO_MENUMODEL",value));
206 re.initCause(e);
207 throw re;
208 }
209 }
210 }
211
212
213
214
215
216
217
218 public static CollectionModel toCollectionModel(Object value)
219 {
220 if (value instanceof CollectionModel)
221 return (CollectionModel) value;
222 else
223 {
224 return new SortableModel(value);
225 }
226 }
227
228
229
230
231
232
233 public static DataModel toDataModel(Object value)
234 {
235 if (value instanceof DataModel)
236 {
237 return (DataModel) value;
238 }
239
240 if (value instanceof Object[])
241 {
242 return new ArrayDataModel((Object[]) value);
243 }
244
245 if (value == null)
246 {
247 return new ListDataModel(Collections.emptyList());
248 }
249 else if (value instanceof List)
250 return new ListDataModel((List) value);
251
252 return new ScalarDataModel(value);
253 }
254
255 private ModelUtils()
256 {
257 }
258 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
259 ModelUtils.class);
260 }