001package org.kuali.common.util;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.List;
006
007import org.apache.commons.lang3.StringUtils;
008
009import com.google.common.base.Optional;
010import com.google.common.collect.ImmutableList;
011
012public class ListUtils {
013
014        public static List<String> prefix(String prefix, List<String> list) {
015                return prefix(prefix, Optional.<String> absent(), list);
016        }
017
018        public static List<String> prefix(String prefix, String separator, List<String> list) {
019                return prefix(prefix, Optional.of(separator), list);
020        }
021
022        private static List<String> prefix(String prefix, Optional<String> separator, List<String> list) {
023                Assert.noBlanks(prefix);
024                Assert.noNulls(list, separator);
025                Assert.noBlanks(separator);
026                List<String> newList = newArrayList();
027                String separatorValue = separator.isPresent() ? separator.get() : "";
028                for (String element : list) {
029                        String value = prefix + separatorValue + element;
030                        newList.add(value);
031                }
032                return ImmutableList.copyOf(newList);
033        }
034
035        public static <T> List<T> newArrayList() {
036                return newArrayList(new ArrayList<T>());
037        }
038
039        public static <T> List<T> newArrayList(T element) {
040                Assert.noNulls(element);
041                List<T> list = newArrayList();
042                list.add(element);
043                return list;
044        }
045
046        public static <T> List<T> newArrayList(List<T> list) {
047                return newArrayList(list, false);
048        }
049
050        public static <T> List<T> newImmutableArrayList(List<T> list) {
051                return newArrayList(list, true);
052        }
053
054        public static <T> List<T> newArrayList(List<T> list, boolean immutable) {
055                Assert.noNulls(list);
056                if (immutable) {
057                        return Collections.unmodifiableList(new ArrayList<T>(list));
058                } else {
059                        return new ArrayList<T>(list);
060                }
061        }
062
063        /**
064         * This method guarantees 4 things:<br>
065         * 
066         * 1 - The <code>list</code> is not null.<br>
067         * 2 - The <code>list</code> is not empty. (size() > 0)<br>
068         * 3 - The <code>list</code> does not contain <code>null</code>.<br>
069         * 4 - Every element in the <code>list</code> is the exact same runtime type.<br>
070         */
071        public static void assertUniformRuntimeType(List<?> list) {
072                Assert.noNulls(list);
073                Assert.isTrue(list.size() > 0, "list is empty");
074                Assert.isFalse(list.contains(null), "list contains null");
075                Class<?> previous = list.get(0).getClass();
076                for (int i = 1; i < list.size(); i++) {
077                        Class<?> current = list.get(i).getClass();
078                        Assert.isTrue(current == previous, "non-uniform runtime types at index " + i);
079                        previous = current;
080                }
081        }
082
083        public static boolean equals(List<String> one, List<String> two) {
084                return equals(one, two, false);
085        }
086
087        public static boolean equalsIgnoreCase(List<String> one, List<String> two) {
088                return equals(one, two, true);
089        }
090
091        protected static boolean equals(List<String> one, List<String> two, boolean ignoreCase) {
092
093                // Nulls not allowed
094                Assert.noNulls(one, two);
095
096                // If the sizes are different they are not equal
097                if (one.size() != two.size()) {
098                        return false;
099                }
100
101                // The sizes are the same, just pick one
102                int size = one.size();
103
104                // Iterate over both lists comparing each value for equality
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                // All values in both lists match
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}