
repacked.com.google.common.collect.Iterables Maven / Gradle / Ivy
/**
* Copyright © 2013 Sven Ruppert ([email protected])
*
* 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 repacked.com.google.common.collect;
import repacked.com.google.common.base.Function;
import repacked.com.google.common.base.Predicate;
import java.util.Collection;
import java.util.Iterator;
/**
* Copyright (C) 2010 RapidPM
* 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.
*
* Created by RapidPM - Team on 19.09.16.
*/
public class Iterables {
private Iterables() {
}
public static Iterable filter(
final Iterable unfiltered ,
final Predicate super T> retainIfTrue) {
if (unfiltered == null || retainIfTrue == null) throw new NullPointerException();
return new FluentIterable() {
@Override
public Iterator iterator() {
return Iterators.filter(unfiltered.iterator() , retainIfTrue);
}
};
}
public static Iterable concat(Iterable extends T> a , Iterable extends T> b) {
return FluentIterable.concat(a , b);
}
public static Iterable concat(Iterable> inputs) {
return FluentIterable.concat(inputs);
}
public static boolean isEmpty(final Iterable iterable) {
if (iterable instanceof Collection) {
return ((Collection>) iterable).isEmpty();
}
return ! iterable.iterator().hasNext();
}
public static T getOnlyElement(final Iterable iterable) {
final Iterator iterator = iterable.iterator();
return Iterators.getOnlyElement(iterator);
}
public static boolean any(Iterable iterable , Predicate super T> predicate) {
return Iterators.any(iterable.iterator() , predicate);
}
public static boolean contains(final Iterable> iterable , final Object element) {
if (iterable instanceof Collection) {
Collection> collection = (Collection>) iterable;
return Collections2.safeContains(collection , element);
}
return Iterators.contains(iterable.iterator() , element);
}
public static Iterable transform(
final Iterable fromIterable , final Function super F, ? extends T> function) {
return new FluentIterable() {
@Override
public Iterator iterator() {
return Iterators.transform(fromIterable.iterator() , function);
}
};
}
public static Iterable limit(final Iterable iterable , final int limitSize) {
return new FluentIterable() {
@Override
public Iterator iterator() {
return Iterators.limit(iterable.iterator() , limitSize);
}
};
}
}