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

org.osgl.util.Iterators Maven / Gradle / Ivy

The newest version!
package org.osgl.util;

/*-
 * #%L
 * Java Tool
 * %%
 * Copyright (C) 2014 - 2017 OSGL (Open Source General Library)
 * %%
 * 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.
 * #L%
 */

import org.osgl.$;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Created with IntelliJ IDEA.
 * User: luog
 * Date: 23/10/13
 * Time: 6:13 PM
 * To change this template use File | Settings | File Templates.
 */
public enum Iterators {
    ;

    public static final Iterator NULL = new Iterator() {
        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Object next() {
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove");
        }
    };

    public static  Iterator nil() {
        return $.cast(NULL);
    }

    public static  Iterator filterIndex(Iterator itr, $.Function predicate) {
        return new IndexFilteredIterator<>(itr, predicate);
    }

    public static  Iterator filter(Iterator itr, $.Function predicate) {
        return new FilteredIterator<>(itr, predicate);
    }

    public static  Iterator filterWhile(Iterator itr, $.Function predicate) {
        return new FilteredIterator<>(itr, predicate, FilteredIterator.Type.WHILE);
    }

    public static  Iterator filterUntil(Iterator itr, $.Function predicate) {
        return new FilteredIterator<>(itr, predicate, FilteredIterator.Type.UNTIL);
    }

    public static  Iterator composite(Iterator i1, Iterator i2) {
        return new CompositeIterator<>(i1, i2);
    }

    public static Iterator of(Object t) {
        if (null == t) {
            return new SingletonIterator<>(t);
        }
        Class type = t.getClass();
        if (type.isArray()) {
            return new ArrayObjectIterator(t);
        }
        if (Iterable.class.isAssignableFrom(type)) {
            return ((Iterable) t).iterator();
        }
        return new SingletonIterator(t);
    }

    public static  Iterator singleton(T t) {
        return new SingletonIterator<>(t);
    }

    public static Iterator ofArray(Object array) {
        return new ArrayObjectIterator(array);
    }

    public static  Iterator map(Iterator itr, $.Function mapper) {
        return new MappedIterator<>(itr, mapper);
    }

    public static  Iterator flatMap(Iterator itr, $.Function> mapper) {
        return new FlatMappedIterator<>(itr, mapper);
    }

    public static  Iterator collect(Iterator itr, String path) {
        return new CollectorIterator<>(itr, path);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy