leap.lang.Enumerables Maven / Gradle / Ivy
/*
* 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.Enumeration;
import java.util.List;
import leap.lang.collection.ArrayIterable;
import leap.lang.collection.ArrayObjectIterable;
import leap.lang.collection.CollectionEnumerable;
import leap.lang.collection.EmptyIterable;
import leap.lang.collection.IterableEnumerable;
import leap.lang.collection.ListEnumerable;
/**
* null safe utils for {@link Enumerable} objects.
*/
public class Enumerables {
@SuppressWarnings("rawtypes")
public static final Enumerable EMPTY = new EmptyIterable();
@SuppressWarnings("unchecked")
public static final Enumerable empty(){
return (Enumerable)EMPTY;
}
@SuppressWarnings("unchecked")
public static final Enumerable of(Object object) throws IllegalArgumentException {
Enumerable e = tryOf(object);
if(null == e) {
throw new IllegalArgumentException("not a supported enumerable type '" + object.getClass().getName() + "'");
}
return e;
}
@SuppressWarnings("unchecked")
public static final Enumerable tryOf(Object object) throws IllegalArgumentException {
if(null == object){
return empty();
}
if(object instanceof Enumerable){
return (Enumerable)object;
}
if(object instanceof List){
return new ListEnumerable((List)object);
}
if(object instanceof Collection){
return new CollectionEnumerable<>((Collection)object);
}
Class> clazz = object.getClass();
if(clazz.isArray()){
if(clazz.getComponentType().isPrimitive()){
return new ArrayObjectIterable(object);
}else{
return ofArray((E[])object);
}
}
if(object instanceof Iterable){
return new IterableEnumerable((Iterable)object);
}
return null;
}
@SuppressWarnings("unchecked")
public static final Enumerable ofArray(E... array){
return null == array || array.length == 0 ? (Enumerable)EMPTY : new ArrayIterable(array);
}
protected Enumerables(){
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy