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