
leap.lang.Collections2 Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package leap.lang;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* null safe utils for {@link Collection}
*/
public class Collections2 {
public static boolean isEmpty(Collection> c){
return null == c || c.isEmpty();
}
public static boolean isNotEmpty(Collection> c){
return null != c && !c.isEmpty();
}
public static boolean contains(Collection> collection, Object object) {
if(null == collection){
return false;
}
try {
return collection.contains(object);
} catch (ClassCastException e) {
return false;
} catch (NullPointerException e) {
return false;
}
}
public static void addAll(Collection super E> collection,E[] elements){
if(null == collection || null == elements){
return;
}
for(int i=0;i void addAll(Collection super E> collection,Iterable elements){
if(null == collection || null == elements){
return;
}
for(E element : elements){
collection.add(element);
}
}
public static boolean addIfNotNull(Collection super E> collection,E element){
if(null == collection || null == element){
return false;
}
collection.add(element);
return true;
}
public static boolean addIfNotEmpty(Collection stringCollection,String stringElement, boolean trim){
if(null == stringCollection || null == stringElement){
return false;
}
if(trim){
stringElement = stringElement.trim();
}
if(Strings.EMPTY.equals(stringElement)){
return false;
}
stringCollection.add(stringElement);
return true;
}
public static String[] toStringArray(Collection c){
if(null == c || c.isEmpty()){
return Arrays2.EMPTY_STRING_ARRAY;
}
return c.toArray(new String[c.size()]);
}
public static T remove(Collection c, Predicate predicate) {
T found = null;
for(T item : c) {
if(predicate.test(item)) {
found = item;
break;
}
}
if(null != found) {
c.remove(found);
}
return found;
}
public static Map toMap(Collection c, Function key) {
if(null == c) {
return New.linkedHashMap();
}
Map map = new LinkedHashMap<>(c.size());
for(T item : c) {
map.put(key.apply(item), item);
}
return map;
}
protected Collections2(){
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy