1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.StringReader;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.springframework.util.Assert;
32
33 public class CollectionUtils {
34
35
36
37
38
39 public static final List<String> getSequencedStrings(List<String> strings, int initialSequenceNumber) {
40 List<String> sequencedStrings = new ArrayList<String>();
41 int size = strings.size();
42 int length = new Integer(size).toString().length();
43 String prefix = StringUtils.repeat("0", length);
44 for (String string : strings) {
45 String sequence = StringUtils.right(prefix + (initialSequenceNumber++), length);
46 String sequencedString = sequence + "-" + string;
47 sequencedStrings.add(sequencedString);
48 }
49 return sequencedStrings;
50 }
51
52
53
54
55
56 public static final List<String> getSequencedStrings(List<String> strings) {
57 return getSequencedStrings(strings, 0);
58 }
59
60
61
62
63 public static final List<String> getUniqueStrings(List<String> strings) {
64 List<String> unique = new ArrayList<String>();
65 for (String string : strings) {
66 if (!unique.contains(string)) {
67 unique.add(string);
68 }
69 }
70 return unique;
71 }
72
73 public static final List<File> getUniqueFiles(List<File> files) {
74 List<String> strings = new ArrayList<String>();
75 for (File file : files) {
76 strings.add(LocationUtils.getCanonicalPath(file));
77 }
78 List<String> uniqueStrings = getUniqueStrings(strings);
79 List<File> uniqueFiles = new ArrayList<File>();
80 for (String uniqueString : uniqueStrings) {
81 uniqueFiles.add(new File(uniqueString));
82 }
83 return uniqueFiles;
84 }
85
86 public static final List<String> getLines(String s) {
87 if (s == null) {
88 return Collections.<String> emptyList();
89 }
90 try {
91 return IOUtils.readLines(new StringReader(s));
92 } catch (IOException e) {
93 throw new IllegalStateException(e);
94 }
95 }
96
97
98
99
100 public static final List<String> combineStringsUniquely(List<String> list1, List<String> list2) {
101 List<String> newList = getUniqueStrings(list1);
102 for (String element : list2) {
103 if (!newList.contains(element)) {
104 newList.add(element);
105 }
106 }
107 return newList;
108 }
109
110 protected static final <T> T getNewInstance(Class<T> c) {
111 try {
112 return c.newInstance();
113 } catch (IllegalAccessException e) {
114 throw new IllegalArgumentException(e);
115 } catch (InstantiationException e) {
116 throw new IllegalArgumentException(e);
117 }
118 }
119
120
121
122
123 public static final <T> List<T> getNewList(Class<T> c, int size) {
124 List<T> list = new ArrayList<T>();
125 for (int i = 0; i < size; i++) {
126 T element = getNewInstance(c);
127 list.add(element);
128 }
129 return list;
130 }
131
132
133
134
135
136 public static final <T> List<T> getList(List<Boolean> includes, List<T> list) {
137 Assert.isTrue(includes.size() == list.size());
138 List<T> included = new ArrayList<T>();
139 for (int i = 0; i < includes.size(); i++) {
140 if (includes.get(i)) {
141 included.add(list.get(i));
142 }
143 }
144 return included;
145 }
146
147
148
149
150 public static final <T> List<T> combine(T required, List<T> optional) {
151 Assert.notNull(required);
152 if (optional == null) {
153 return Collections.singletonList(required);
154 } else {
155 List<T> combined = new ArrayList<T>();
156
157 combined.add(required);
158
159 for (T element : optional) {
160 combined.add(element);
161 }
162 return combined;
163 }
164 }
165
166
167
168
169 public static final <T> List<T> toEmptyList(T o) {
170 if (o == null) {
171 return Collections.<T> emptyList();
172 } else {
173 return Collections.singletonList(o);
174 }
175 }
176
177
178
179
180 public static final <T> List<T> toEmptyList(List<T> list) {
181 if (list == null) {
182 return Collections.<T> emptyList();
183 } else {
184 return list;
185 }
186 }
187
188 public static final <T> List<T> toNullIfEmpty(List<T> list) {
189 if (isEmpty(list)) {
190 return null;
191 } else {
192 return list;
193 }
194 }
195
196 public static final <T> Collection<T> toNullIfEmpty(Collection<T> c) {
197 if (isEmpty(c)) {
198 return null;
199 } else {
200 return c;
201 }
202 }
203
204 public static final <T> List<T> getPreFilledList(int size, T value) {
205 if (value == null || size < 1) {
206 return Collections.<T> emptyList();
207 } else {
208 List<T> list = new ArrayList<T>(size);
209 for (int i = 0; i < size; i++) {
210 list.add(value);
211 }
212 return list;
213 }
214 }
215
216 public static final String getSpaceSeparatedString(List<?> list) {
217 list = toEmptyList(list);
218 StringBuilder sb = new StringBuilder();
219 for (int i = 0; i < list.size(); i++) {
220 if (i != 0) {
221 sb.append(" ");
222 }
223 sb.append(list.get(i).toString());
224 }
225 return sb.toString();
226 }
227
228 public static final Object[] toObjectArray(List<Object> objects) {
229 return objects.toArray(new Object[objects.size()]);
230 }
231
232 public static final String[] toStringArray(List<String> strings) {
233 return strings.toArray(new String[strings.size()]);
234 }
235
236 public static final boolean isEmpty(Collection<?> c) {
237 return c == null || c.size() == 0;
238 }
239
240 public static final List<String> sortedMerge(List<String> list, String csv) {
241 Set<String> set = new TreeSet<String>();
242 for (String string : toEmptyList(list)) {
243 set.add(string);
244 }
245 for (String string : getTrimmedListFromCSV(csv)) {
246 set.add(string);
247 }
248 return new ArrayList<String>(set);
249 }
250
251 public static final List<String> getTrimmedListFromCSV(String csv) {
252 if (StringUtils.isBlank(csv)) {
253 return Collections.<String> emptyList();
254 }
255 List<String> list = new ArrayList<String>();
256 String[] tokens = Str.splitAndTrimCSV(csv);
257 list.addAll(Arrays.asList(tokens));
258 return list;
259 }
260
261 public static final List<String> combineStrings(List<String> list1, List<String> list2, List<String> list3) {
262 List<String> combined = new ArrayList<String>();
263 nullSafeAdd(combined, list1);
264 nullSafeAdd(combined, list2);
265 nullSafeAdd(combined, list3);
266 return combined;
267 }
268
269
270
271
272 public static final List<String> combineStrings(List<String> list1, String string, List<String> list2) {
273 return combineStrings(list1, toEmptyList(string), list2);
274 }
275
276
277
278
279 public static final List<String> combineStrings(List<String> list1, List<String> list2) {
280 return combineStrings(list1, (String) null, list2);
281 }
282
283 public static final <T> void nullSafeAdd(List<T> list1, List<T> list2) {
284 if (list2 != null) {
285 list1.addAll(list2);
286 }
287 }
288
289
290
291
292 public static final boolean containsAny(String s, List<String> strings) {
293 for (String string : strings) {
294 if (StringUtils.contains(s, string)) {
295 return true;
296 }
297 }
298 return false;
299 }
300 }