001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import static org.kuali.common.util.base.Precondition.checkNotNull;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import org.apache.commons.lang3.StringUtils;
025
026import com.google.common.base.Optional;
027import com.google.common.collect.ImmutableList;
028
029public class ListUtils {
030
031        public static <T> boolean equalElements(List<T> list1, List<T> list2) {
032                checkNotNull(list1, "list1");
033                checkNotNull(list2, "list2");
034                if (list1.size() != list2.size()) {
035                        return false;
036                }
037                for (int i = 0; i < list1.size(); i++) {
038                        if (!list1.get(i).equals(list2.get(i))) {
039                                return false;
040                        }
041                }
042                return true;
043        }
044
045        public static List<String> prefix(String prefix, List<String> list) {
046                return prefix(prefix, Optional.<String> absent(), list);
047        }
048
049        public static List<String> prefix(String prefix, String separator, List<String> list) {
050                return prefix(prefix, Optional.of(separator), list);
051        }
052
053        private static List<String> prefix(String prefix, Optional<String> separator, List<String> list) {
054                Assert.noBlanks(prefix);
055                Assert.noNulls(list, separator);
056                Assert.noBlanks(separator);
057                List<String> newList = newArrayList();
058                String separatorValue = separator.isPresent() ? separator.get() : "";
059                for (String element : list) {
060                        String value = prefix + separatorValue + element;
061                        newList.add(value);
062                }
063                return ImmutableList.copyOf(newList);
064        }
065
066        public static <T> List<T> newArrayList() {
067                return newArrayList(new ArrayList<T>());
068        }
069
070        public static <T> List<T> newArrayList(T element) {
071                Assert.noNulls(element);
072                List<T> list = newArrayList();
073                list.add(element);
074                return list;
075        }
076
077        public static <T> List<T> newArrayList(List<T> list) {
078                return newArrayList(list, false);
079        }
080
081        public static <T> List<T> newImmutableArrayList(List<T> list) {
082                return newArrayList(list, true);
083        }
084
085        public static <T> List<T> newArrayList(List<T> list, boolean immutable) {
086                Assert.noNulls(list);
087                if (immutable) {
088                        return Collections.unmodifiableList(new ArrayList<T>(list));
089                } else {
090                        return new ArrayList<T>(list);
091                }
092        }
093
094        /**
095         * This method guarantees 4 things:<br>
096         * 
097         * 1 - The <code>list</code> is not null.<br>
098         * 2 - The <code>list</code> is not empty. (size() > 0)<br>
099         * 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}