/
IterableDataModel.java
141 lines (116 loc) · 3.35 KB
/
IterableDataModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.model;
import static org.omnifaces.util.Utils.iterableToList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import jakarta.faces.model.CollectionDataModel;
import jakarta.faces.model.DataModel;
import jakarta.faces.model.DataModelEvent;
import jakarta.faces.model.DataModelListener;
import jakarta.faces.model.ListDataModel;
/**
* <strong>IterableDataModel</strong> is an implementation of {@link DataModel} that wraps an <code>Iterable</code>.
* <p>
* This can be used to encapsulate nearly every collection type, including {@link Collection} derived types such as
* {@link List} and {@link Set}. As such this specific DataModel can be used instead of more specific DataModels like
* {@link ListDataModel} and {@link CollectionDataModel}.
*
* @since 1.5
* @author Arjan.Tijms
*
*/
public class IterableDataModel<E> extends DataModel<E> {
private int index = -1;
private Iterable<E> iterable;
private List<E> list;
/**
* Construct the iterable data model based on the given iterable instance.
* @param iterable The iterable instance to construct the iterable data model for.
*/
public IterableDataModel(Iterable<E> iterable) {
setWrappedData(iterable);
}
@Override
public boolean isRowAvailable() {
return list != null && index >= 0 && index < list.size();
}
@Override
public int getRowCount() {
if (list == null) {
return -1;
}
return list.size();
}
@Override
public E getRowData() {
if (list == null) {
return null;
}
if (!isRowAvailable()) {
throw new IllegalStateException();
}
return list.get(index);
}
@Override
public int getRowIndex() {
return index;
}
@Override
public void setRowIndex(int rowIndex) {
if (rowIndex < -1) {
throw new IllegalArgumentException();
}
int oldRowIndex = index;
index = rowIndex;
if (list == null) {
return;
}
notifyListeners(oldRowIndex, rowIndex);
}
@Override
public Object getWrappedData() {
return iterable;
}
@SuppressWarnings("unchecked")
@Override
public void setWrappedData(Object data) {
if (data == null) {
iterable = null;
list = null;
setRowIndex(-1);
} else {
iterable = (Iterable<E>) data;
list = iterableToList(iterable);
setRowIndex(0);
}
}
private E getRowDataOrNull() {
if (isRowAvailable()) {
return getRowData();
}
return null;
}
private void notifyListeners(int oldRowIndex, int rowIndex) {
DataModelListener[] dataModelListeners = getDataModelListeners();
if (oldRowIndex != rowIndex && dataModelListeners != null) {
DataModelEvent dataModelEvent = new DataModelEvent(this, rowIndex, getRowDataOrNull());
for (DataModelListener dataModelListener : dataModelListeners) {
if (dataModelListener != null) {
dataModelListener.rowSelected(dataModelEvent);
}
}
}
}
}