All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.xphsc.collect.Iterables Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package com.github.xphsc.collect;

import com.github.xphsc.bean.Closure;
import java.util.Collection;
import java.util.Iterator;

/**
 * Created by ${huipei.x} on 2017-8-7.
 */
public class Iterables {

    public static boolean isEmpty(Iterable iterable) {
        return iterable instanceof Collection ?((Collection)iterable).isEmpty(): Iterators.isEmpty(emptyIteratorIfNull(iterable));
    }

    public static  void forEach(Iterable iterable, Closure closure) {
        Iterables.forEach((Iterable) emptyIteratorIfNull(iterable), closure);
    }

    private static  Iterator emptyIteratorIfNull(Iterable iterable) {
        return (Iterator)(iterable != null?iterable.iterator(): Iterators.emptyIterator());
    }

    public static  E forEachButLast(Iterable iterable, Closure closure) {
        return Iterators.forEachButLast(emptyIteratorIfNull(iterable), closure);
    }

    public static int size(Iterable iterable) {
        return iterable instanceof Collection?((Collection)iterable).size():Iterators.size(emptyIteratorIfNull(iterable));
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy