1 package fr.ifremer.quadrige2.core.dao.technical;
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 import com.google.common.base.Function;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.Lists;
30 import com.google.common.collect.Maps;
31 import com.google.common.collect.Sets;
32 import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
33 import org.apache.commons.beanutils.PropertyUtils;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.apache.commons.collections4.MapUtils;
36 import org.apache.commons.lang3.StringUtils;
37
38 import java.lang.reflect.InvocationTargetException;
39 import java.util.*;
40
41
42
43
44
45 public class Beans {
46
47
48
49
50
51
52
53
54 public static <E> List<E> getList(Collection<E> list) {
55 if (CollectionUtils.isEmpty(list)) {
56 return Lists.newArrayList();
57 } else {
58 return Lists.newArrayList(list);
59 }
60 }
61
62
63
64
65
66
67
68
69 public static <E> List<E> getListWithoutNull(Collection<E> list) {
70 List<E> result = getList(list);
71 result.removeAll(Collections.singleton((E) null));
72 return result;
73 }
74
75
76
77
78
79
80
81
82 public static <E> Set<E> getSet(Collection<E> list) {
83 if (CollectionUtils.isEmpty(list)) {
84 return Sets.newHashSet();
85 } else {
86 return Sets.newHashSet(list);
87 }
88 }
89
90
91
92
93
94
95
96
97 public static <E> Set<E> getSetWithoutNull(Collection<E> list) {
98 Set<E> result = getSet(list);
99 result.removeAll(Collections.singleton((E) null));
100 return result;
101 }
102
103
104
105
106
107
108
109
110
111 public static <K, V> Map<K, V> getMap(Map<K, V> map) {
112 if (MapUtils.isEmpty(map)) {
113 return Maps.newHashMap();
114 } else {
115 return Maps.newHashMap(map);
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128 public static <K, V> Map<K, V> splitByProperty(Iterable<V> list, String propertyName) {
129 Preconditions.checkArgument(StringUtils.isNotBlank(propertyName));
130 Function<V, K> function = newPropertyFunction(propertyName);
131 return getMap(Maps.uniqueIndex(list, function));
132 }
133
134
135
136
137
138
139
140
141
142
143 public static <K, V> List<K> collectProperties(Collection<V> collection, String propertyName) {
144 List<V> list;
145 if (collection != null && collection instanceof List) {
146 list = (List<V>) collection;
147 } else {
148 list = getList(collection);
149 }
150 Preconditions.checkArgument(StringUtils.isNotBlank(propertyName));
151 Function<V, K> function = newPropertyFunction(propertyName);
152 return Lists.transform(list, function);
153 }
154
155 private static <K, V> Function<V, K> newPropertyFunction(final String propertyName) {
156 return input -> getProperty(input, propertyName);
157 }
158
159
160
161
162
163
164
165
166
167
168 @SuppressWarnings("unchecked")
169 public static <K, V> V getProperty(K object, String propertyName) {
170 try {
171 return (V) PropertyUtils.getProperty(object, propertyName);
172 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
173 throw new Quadrige2TechnicalException(String.format("Could not get property %1s on object of type %2s", propertyName, object.getClass().getName()), e);
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186 public static <K, V> void setProperty(K object, String propertyName, V value) {
187 try {
188 PropertyUtils.setProperty(object, propertyName, value);
189 } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
190 throw new Quadrige2TechnicalException(String.format("Could not set property %1s not found on object of type %2s", propertyName, object.getClass().getName()), e);
191 }
192 }
193
194 public static Integer[] asIntegerArray(Collection<Integer> values) {
195 if (CollectionUtils.isEmpty(values)) {
196 return null;
197 }
198 return values.toArray(new Integer[values.size()]);
199 }
200
201 public static String[] asStringArray(Collection<String> values) {
202 if (CollectionUtils.isEmpty(values)) {
203 return null;
204 }
205 return values.toArray(new String[values.size()]);
206 }
207
208 public static String[] asStringArray(String value, String delimiter) {
209 if (StringUtils.isBlank(value)) return new String[0];
210 StringTokenizer tokenizer = new StringTokenizer(value, delimiter);
211 String[] values = new String[tokenizer.countTokens()];
212 int i=0;
213 while (tokenizer.hasMoreTokens()) {
214 values[i] = tokenizer.nextToken();
215 i++;
216 }
217 return values;
218 }
219 }