1 package org.kuali.common.util;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.apache.commons.lang3.StringUtils;
8
9 import com.google.common.base.Optional;
10 import com.google.common.collect.ImmutableList;
11
12 public class ListUtils {
13
14 public static List<String> prefix(String prefix, List<String> list) {
15 return prefix(prefix, Optional.<String> absent(), list);
16 }
17
18 public static List<String> prefix(String prefix, String separator, List<String> list) {
19 return prefix(prefix, Optional.of(separator), list);
20 }
21
22 private static List<String> prefix(String prefix, Optional<String> separator, List<String> list) {
23 Assert.noBlanks(prefix);
24 Assert.noNulls(list, separator);
25 Assert.noBlanks(separator);
26 List<String> newList = newArrayList();
27 String separatorValue = separator.isPresent() ? separator.get() : "";
28 for (String element : list) {
29 String value = prefix + separatorValue + element;
30 newList.add(value);
31 }
32 return ImmutableList.copyOf(newList);
33 }
34
35 public static <T> List<T> newArrayList() {
36 return newArrayList(new ArrayList<T>());
37 }
38
39 public static <T> List<T> newArrayList(T element) {
40 Assert.noNulls(element);
41 List<T> list = newArrayList();
42 list.add(element);
43 return list;
44 }
45
46 public static <T> List<T> newArrayList(List<T> list) {
47 return newArrayList(list, false);
48 }
49
50 public static <T> List<T> newImmutableArrayList(List<T> list) {
51 return newArrayList(list, true);
52 }
53
54 public static <T> List<T> newArrayList(List<T> list, boolean immutable) {
55 Assert.noNulls(list);
56 if (immutable) {
57 return Collections.unmodifiableList(new ArrayList<T>(list));
58 } else {
59 return new ArrayList<T>(list);
60 }
61 }
62
63
64
65
66
67
68
69
70
71 public static void assertUniformRuntimeType(List<?> list) {
72 Assert.noNulls(list);
73 Assert.isTrue(list.size() > 0, "list is empty");
74 Assert.isFalse(list.contains(null), "list contains null");
75 Class<?> previous = list.get(0).getClass();
76 for (int i = 1; i < list.size(); i++) {
77 Class<?> current = list.get(i).getClass();
78 Assert.isTrue(current == previous, "non-uniform runtime types at index " + i);
79 previous = current;
80 }
81 }
82
83 public static boolean equals(List<String> one, List<String> two) {
84 return equals(one, two, false);
85 }
86
87 public static boolean equalsIgnoreCase(List<String> one, List<String> two) {
88 return equals(one, two, true);
89 }
90
91 protected static boolean equals(List<String> one, List<String> two, boolean ignoreCase) {
92
93
94 Assert.noNulls(one, two);
95
96
97 if (one.size() != two.size()) {
98 return false;
99 }
100
101
102 int size = one.size();
103
104
105 for (int i = 0; i < size; i++) {
106 if (!equal(one.get(i), two.get(i), ignoreCase)) {
107 return false;
108 }
109 }
110
111
112 return true;
113 }
114
115 protected static boolean equal(String one, String two, boolean ignoreCase) {
116 if (ignoreCase) {
117 return StringUtils.equalsIgnoreCase(one, two);
118 } else {
119 return StringUtils.equals(one, two);
120 }
121 }
122
123 }