View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	 * This method guarantees 4 things:<br>
96  	 * 
97  	 * 1 - The <code>list</code> is not null.<br>
98  	 * 2 - The <code>list</code> is not empty. (size() > 0)<br>
99  	 * 3 - The <code>list</code> does not contain <code>null</code>.<br>
100 	 * 4 - Every element in the <code>list</code> is the exact same runtime type.<br>
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 		// Nulls not allowed
125 		Assert.noNulls(one, two);
126 
127 		// If the sizes are different they are not equal
128 		if (one.size() != two.size()) {
129 			return false;
130 		}
131 
132 		// The sizes are the same, just pick one
133 		int size = one.size();
134 
135 		// Iterate over both lists comparing each value for equality
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 		// All values in both lists match
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 }