/
SelectItemsUtils.java
147 lines (124 loc) · 5.5 KB
/
SelectItemsUtils.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
142
143
144
145
146
147
/*
* 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.util.selectitems;
import static org.omnifaces.util.Utils.isEmpty;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.Converter;
import jakarta.faces.model.SelectItem;
import jakarta.faces.model.SelectItemGroup;
/**
* Collection of utility methods for working with {@link SelectItem} or data represented by them.
*
* @author Arjan Tijms
*
*/
public final class SelectItemsUtils {
private SelectItemsUtils() {}
/**
* Finds an object value in the {@link SelectItem} instances associated with the given component by means of matching its converted value with
* the given string value.
*
* @param context The involved faces context.
* @param component the component with which {@link SelectItem}s should be associated that are used to search in.
* @param value a string that should match the string representation of one of the values held by the {@link SelectItem}s.
* @param converter the faces {@link Converter} used to generate String representations for the values held by the {@link SelectItem}s.
*
* @return the Object representation of the value where its string representation matches the input value.
*/
public static Object findValueByStringConversion(FacesContext context, UIComponent component, String value, Converter<Object> converter) {
return findValueByStringConversion(context, component, SelectItemsCollector.collectFromParent(context, component).iterator(), value, converter);
}
private static Object findValueByStringConversion(FacesContext context, UIComponent component, Iterator<SelectItem> items, String value, Converter<Object> converter) {
while (items.hasNext()) {
SelectItem item = items.next();
if (item instanceof SelectItemGroup) {
SelectItem[] groupItems = ((SelectItemGroup) item).getSelectItems();
Object object = findValueByStringConversion(context, component, groupItems, value, converter);
if (object != null) {
return object;
}
}
else if (!item.isNoSelectionOption()) {
Object itemValue = item.getValue();
String convertedItemValue = converter.getAsString(context, component, itemValue);
if (Objects.equals(value, convertedItemValue)) {
return itemValue;
}
}
}
return null;
}
private static Object findValueByStringConversion(FacesContext context, UIComponent component, SelectItem[] items, String value, Converter<Object> converter) {
return isEmpty(items) ? null : findValueByStringConversion(context, component, new ArrayIterator(items), value, converter);
}
/**
* Collects all values associated with all {@link SelectItem} instances associated with the given component.
* <p>
* Note that values from recursively scanned {@link SelectItemGroup} instances are included.
*
* @param context The involved faces context.
* @param component the component with which {@link SelectItem} instances should be associated
* @return List of all values hold by {@link SelectItem} instances
*/
public static List<Object> collectAllValuesFromSelectItems(FacesContext context, UIComponent component) {
List<Object> values = new ArrayList<>();
collect(SelectItemsCollector.collectFromParent(context, component).iterator(), values);
return values;
}
private static void collect(Iterator<SelectItem> items, List<Object> values) {
while (items.hasNext()) {
SelectItem item = items.next();
if (item instanceof SelectItemGroup) {
SelectItem[] subitems = ((SelectItemGroup) item).getSelectItems();
if (!isEmpty(subitems)) {
collect(new ArrayIterator(subitems), values);
}
} else if (!item.isNoSelectionOption()) {
values.add(item.getValue());
}
}
}
/**
* Exposes an Array via an <code>Iterator</code>
*/
static class ArrayIterator implements Iterator<SelectItem> {
private SelectItem[] items;
private int index = 0;
public ArrayIterator(SelectItem[] items) {
this.items = items;
}
@Override
public boolean hasNext() {
return (index < items.length);
}
@Override
public SelectItem next() {
if (hasNext()) {
return items[index++];
}
else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}