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

com.github.xphsc.collect.Iterators 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 com.github.xphsc.bean.comparator.Transformer;
import com.github.xphsc.collect.iterator.EmptyIterator;
import com.github.xphsc.collect.iterator.ResettableIterator;
import java.util.Iterator;



public class Iterators {

    public static boolean isEmpty(Iterator iterator) {
        return iterator == null || !iterator.hasNext();
    }

    public static  void forEach(Iterator iterator, Closure closure) {
        if(closure == null) {
            throw new NullPointerException("Closure must not be null");
        } else {
            if(iterator != null) {
                while(iterator.hasNext()) {
                    Object element = iterator.next();
                    closure.execute(element);
                }
            }

        }
    }
    public static  T forEachButLast(Iterator iterator, Closure closure) {
        if(closure == null) {
            throw new NullPointerException("Closure must not be null.");
        } else {
            if(iterator != null) {
                while(iterator.hasNext()) {
                    Object element = iterator.next();
                    if(!iterator.hasNext()) {
                        return (T) element;
                    }

                    closure.execute((T)element);
                }
            }

            return null;
        }
    }
    public static  ResettableIterator emptyIterator() {
        return EmptyIterator.resettableEmptyIterator();
    }


    public static int size(Iterator iterator) {
        int size = 0;
        if(iterator != null) {
            while(iterator.hasNext()) {
                iterator.next();
                ++size;
            }
        }

        return size;
    }


    public static  String toString(Iterator iterator, Transformer transformer) {
        return toString(iterator, transformer, ", ", "[", "]");
    }
    public static  String toString(Iterator iterator, Transformer transformer, String delimiter, String prefix, String suffix) {
        if(transformer == null) {
            throw new NullPointerException("transformer may not be null");
        } else if(delimiter == null) {
            throw new NullPointerException("delimiter may not be null");
        } else if(prefix == null) {
            throw new NullPointerException("prefix may not be null");
        } else if(suffix == null) {
            throw new NullPointerException("suffix may not be null");
        } else {
            StringBuilder stringBuilder = new StringBuilder(prefix);
            if(iterator != null) {
                while(iterator.hasNext()) {
                    Object element = iterator.next();
                    stringBuilder.append(transformer.transform(element));
                    stringBuilder.append(delimiter);
                }

                if(stringBuilder.length() > prefix.length()) {
                    stringBuilder.setLength(stringBuilder.length() - delimiter.length());
                }
            }

            stringBuilder.append(suffix);
            return stringBuilder.toString();
        }
    }


}