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

org.osgl.util.IteratorSeq 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.EnumSet;
import java.util.Enumeration;
import java.util.Iterator;

/**
 * Adapt an {@link java.util.Iterator} to a {@link C.Sequence}
 */
class IteratorSeq extends SequenceBase {

    private final Iterator itr_;

    IteratorSeq(final Iterator itr) {
        E.NPE(itr);
        itr_ = itr;
    }

    @Override
    protected EnumSet initFeatures() {
        return EnumSet.of(C.Feature.READONLY);
    }

    @Override
    public Iterator iterator() {
        return new DelegatingIterator(itr_, true);
    }

    @Override
    public C.Sequence head(int n) {
        if (n < 0) {
            throw new UnsupportedOperationException();
        }
        if (n == 0) {
            return Nil.seq();
        }
        return of(Iterators.filterIndex(itr_, N.F.lt(n)));
    }

    @Override
    public C.Sequence tail() throws UnsupportedOperationException {
        if (isEmpty()) {
            throw new UnsupportedOperationException();
        }
        return of(Iterators.filterIndex(itr_, N.F.gt(0)));
    }

    @Override
    public C.Sequence takeWhile($.Function predicate) {
        return of(Iterators.filterWhile(itr_, predicate));
    }

    @Override
    public C.Sequence drop(int n) {
        return of(Iterators.filterIndex(itr_, N.F.gte(n)));
    }

    @Override
    public C.Sequence dropWhile($.Function predicate) {
        return of(Iterators.filterUntil(itr_, $.F.negate(predicate)));
    }

    @Override
    public C.Sequence append(C.Sequence seq) {
        return of(Iterators.composite(itr_, seq.iterator()));
    }

    @Override
    public C.Sequence append(Iterator iterator) {
        return of(Iterators.composite(itr_, iterator));
    }

    @Override
    public C.Sequence append(Enumeration enumeration) {
        return of(Iterators.composite(itr_, new EnumerationIterator(enumeration)));
    }

    @Override
    public C.Sequence append(T t) {
        return of(Iterators.composite(itr_, Iterators.of(t)));
    }

    @Override
    public C.Sequence prepend(C.Sequence seq) {
        return of(Iterators.composite(seq.iterator(), itr_));
    }

    @Override
    public C.Sequence prepend(T t) {
        return of(Iterators.composite(Iterators.of(t), itr_));
    }

    @Override
    public C.Sequence filter($.Function predicate) {
        return of(Iterators.filter(itr_, predicate));
    }

    @Override
    public int size() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    @Override
    public  C.Sequence map($.Function mapper) {
        return new IteratorSeq(Iterators.map(itr_, mapper));
    }

    @Override
    public  C.Sequence flatMap($.Function> mapper
    ) {
        return new IteratorSeq(Iterators.flatMap(itr_, mapper));
    }

    static  IteratorSeq of(Iterator itr) {
        return new IteratorSeq(itr);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy