001 /*
002 * Created on Apr 29, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2007-2011 the original author or authors.
014 */
015 package org.fest.util;
016
017 import static java.util.Collections.*;
018 import static org.fest.util.ToString.toStringOf;
019
020 import java.util.*;
021
022 /**
023 * Utility methods related to collections.
024 *
025 * @author Yvonne Wang
026 * @author Alex Ruiz
027 * @author Joel Costigliola
028 */
029 public final class Collections {
030
031 /**
032 * Creates a list containing the given elements.
033 * @param <T> the type of elements of the list to create.
034 * @param elements the elements to store in the list.
035 * @return the created list.
036 */
037 public static <T> List<T> list(T... elements) {
038 if (elements == null) return null;
039 List<T> list = new ArrayList<T>();
040 for (T e : elements) list.add(e);
041 return list;
042 }
043
044 /**
045 * Creates a set containing the given elements.
046 * @param <T> the type of elements of the set to create.
047 * @param elements the elements to store in the set.
048 * @return the created set.
049 * @since 1.1.5
050 */
051 public static <T> Set<T> set(T... elements) {
052 if (elements == null) return null;
053 Set<T> set = new LinkedHashSet<T>();
054 for (T e : elements) set.add(e);
055 return set;
056 }
057
058 /**
059 * Returns any duplicate elements from the given collection.
060 * @param <T> the generic type of the given collection.
061 * @param c the given collection that might have duplicate elements.
062 * @return a collection containing the duplicate elements of the given one. If no duplicates are found, an empty
063 * collection is returned.
064 */
065 public static <T> Collection<T> duplicatesFrom(Collection<T> c) {
066 Set<T> duplicates = new HashSet<T>();
067 if (isEmpty(c)) return duplicates;
068 Set<T> onlyOne = new HashSet<T>();
069 for (T e : c) {
070 if (onlyOne.contains(e)) {
071 duplicates.add(e);
072 continue;
073 }
074 onlyOne.add(e);
075 }
076 return duplicates;
077 }
078
079 /**
080 * Returns {@code true} if the given collection is {@code null} or empty.
081 * @param c the collection to check.
082 * @return {@code true} if the given collection is {@code null} or empty, otherwise {@code false}.
083 */
084 public static boolean isEmpty(Collection<?> c) {
085 return c == null || c.isEmpty();
086 }
087
088 public static <T> List<T> filter(Collection<?> target, CollectionFilter<T> filter) {
089 return filter.filter(target);
090 }
091
092 /**
093 * Returns the {@code String} representation of the given collection, or {@code null} if the given collection is
094 * {@code null}.
095 * @param c the collection to format.
096 * @return the {@code String} representation of the given collection.
097 */
098 public static String format(Collection<?> c) {
099 if (c == null) return null;
100 Iterator<?> i = c.iterator();
101 if (!i.hasNext()) return "[]";
102 StringBuilder b = new StringBuilder();
103 b.append('[');
104 for (;;) {
105 Object e = i.next();
106 b.append(e == c ? "(this Collection)" : toStringOf(e));
107 if (!i.hasNext()) return b.append(']').toString();
108 b.append(", ");
109 }
110 }
111
112 /**
113 * Returns a new unmodifiable collection containing the non-null elements of the given collection. This method returns
114 * an empty unmodifiable collection if the given collection has only {@code null} elements or if it is empty. This
115 * method returns {@code null} if the given collection is {@code null}.
116 * @param <T> the type of elements of the collection.
117 * @param c the collection we want to extract non null elements from.
118 * @return a new unmodifiable collection containing the non-null elements of the given collection, or {@code null} if
119 * the given collection is {@code null}.
120 * @since 1.1.3
121 */
122 public static <T> Collection<T> nonNullElements(Collection<T> c) {
123 if (c == null) return null;
124 Collection<T> nonNullElements = new ArrayList<T>();
125 for (T o : c)
126 if (o != null) nonNullElements.add(o);
127 return unmodifiableCollection(nonNullElements);
128 }
129
130 /**
131 * Returns a new unmodifiable list containing the non-null elements of the given list. This method returns an empty
132 * unmodifiable list if the given list has only {@code null} elements or if it is empty. This method returns
133 * {@code null} if the given list is {@code null}.
134 * @param <T> the type of elements of the list.
135 * @param l the list we want to extract non null elements from.
136 * @return a new unmodifiable list containing the non-null elements of the given list, or {@code null} if the given
137 * list is {@code null}.
138 * @since 1.1.3
139 */
140 public static <T> List<T> nonNullElements(List<T> l) {
141 Collection<T> nonNullElements = nonNullElements((Collection<T>) l);
142 if (nonNullElements == null) return null;
143 return unmodifiableList(new ArrayList<T>(nonNullElements));
144 }
145
146 /**
147 * Returns {@code true} if the given collection has only {@code null} elements, {@code false} otherwise. If given
148 * collection is empty, this method returns {@code true}.
149 * @param c the given collection. <b>It must not be null</b>.
150 * @return {@code true} if the given collection has only {@code null} elements or is empty, {@code false} otherwise.
151 * @throws NullPointerException if the given collection is {@code null}.
152 * @since 1.1.3
153 */
154 public static boolean hasOnlyNullElements(Collection<?> c) {
155 if (c == null) throw new NullPointerException("The collection to check should not be null");
156 if (c.isEmpty()) return false;
157 for (Object element : c)
158 if (element != null) return false;
159 return true;
160 }
161
162 private Collections() {}
163 }