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

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

There is a newer version: 1.30.0
Show 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.NoSuchElementException;

abstract class ReversibleSeqBase extends SequenceBase implements C.ReversibleSequence {
    @Override
    public C.ReversibleSequence lazy() {
        super.lazy();
        return this;
    }

    @Override
    public C.ReversibleSequence eager() {
        super.eager();
        return this;
    }

    @Override
    public C.ReversibleSequence parallel() {
        super.parallel();
        return this;
    }

    @Override
    public C.ReversibleSequence sequential() {
        super.sequential();
        return this;
    }

    @Override
    public ReversibleSeqBase accept($.Visitor visitor) {
        super.accept(visitor);
        return this;
    }

    @Override
    public ReversibleSeqBase each($.Visitor visitor) {
        return accept(visitor);
    }

    @Override
    public ReversibleSeqBase forEach($.Visitor visitor) {
        return accept(visitor);
    }

    @Override
    public C.ReversibleSequence acceptLeft($.Visitor visitor) {
        super.acceptLeft(visitor);
        return this;
    }

    @Override
    public C.ReversibleSequence head(int n) {
        if (n == 0) {
            return Nil.rseq();
        } else if (n < 0) {
            if (isLimited()) {
                return drop(size() + n);
            } else {
                throw new UnsupportedOperationException();
            }
        } else {
            if (isLimited() && n >= size()) {
                return this;
            }
            return IndexFilteredRSeq.of(this, $.F.lessThan(n));
        }
    }

    @Override
    public C.ReversibleSequence tail() throws UnsupportedOperationException {
        return IndexFilteredRSeq.of(this, $.F.greaterThan(0));
    }

    @Override
    public C.ReversibleSequence take(int n) {
        return head(n);
    }

    @Override
    public C.ReversibleSequence takeWhile($.Function predicate) {
        return FilteredRSeq.of(this, predicate, FilteredIterator.Type.WHILE);
    }

    @Override
    public C.ReversibleSequence drop(int n) throws IllegalArgumentException {
        int sz = size();
        if (n < 0) {
            n = -n;
            if (n >= sz) return Nil.rseq();
            return take(sz - n);
        }
        if (n == 0) {
            return this;
        }
        return IndexFilteredRSeq.of(this, $.F.gte(n));
    }

    @Override
    public C.ReversibleSequence dropWhile($.Function predicate) {
        return FilteredRSeq.of(this, $.F.negate(predicate), FilteredIterator.Type.UNTIL);
    }

    @Override
    public C.ReversibleSequence append(T t) {
        return CompositeRSeq.of(this, $.val(t));
    }

    @Override
    public C.ReversibleSequence prepend(T t) {
        return CompositeRSeq.of($.val(t), this);
    }

    @Override
    public C.ReversibleSequence filter($.Function predicate) {
        return FilteredRSeq.of(this, predicate);
    }

    @Override
    public  C.ReversibleSequence map($.Function mapper) {
        return MappedRSeq.of(this, mapper);
    }

    @Override
    public  C.ReversibleSequence flatMap($.Function> mapper) {
        return FlatMappedRSeq.of(this, mapper);
    }

    @Override
    public  C.Sequence collect(String path) {
        return CollectorRSeq.of(this, path);
    }

    @Override
    public C.ReversibleSequence append(C.ReversibleSequence seq) {
        if (seq.isEmpty()) {
            return this;
        }
        return CompositeRSeq.of(this, seq);
    }

    @Override
    public C.ReversibleSequence prepend(C.ReversibleSequence seq) {
        if (seq.isEmpty()) {
            return this;
        }
        return CompositeRSeq.of(seq, this);
    }

    @Override
    public T last() throws UnsupportedOperationException, NoSuchElementException {
        return reverseIterator().next();
    }

    @Override
    public C.ReversibleSequence tail(int n) throws UnsupportedOperationException, IndexOutOfBoundsException {
        int sz = size();
        if (n < 0) {
            return head(-n);
        } else if (n == 0) {
            return Nil.rseq();
        } else if (n >= sz) {
            return this;
        }
        return reverse().take(n).reverse();
    }

    @Override
    public C.ReversibleSequence reverse() throws UnsupportedOperationException {
        if (isEmpty()) {
            return Nil.rseq();
        }
        return ReversedRSeq.of(this);
    }

    @Override
    public  R reduceRight(R identity, $.Func2 accumulator) {
        return reverse().reduceLeft(identity, accumulator);
    }

    @Override
    public $.Option reduceRight($.Func2 accumulator) {
        return reverse().reduceLeft(accumulator);
    }

    @Override
    public $.Option findLast($.Function predicate) {
        return reverse().findFirst(predicate);
    }

    @Override
    public C.ReversibleSequence acceptRight($.Visitor visitor) {
        return reverse().acceptLeft(visitor);
    }

    @Override
    public  C.ReversibleSequence<$.Binary> zip(C.ReversibleSequence rseq) {
        return new ZippedRSeq<>(this, rseq);
    }

    @Override
    public  C.ReversibleSequence<$.Binary> zipAll(C.ReversibleSequence rseq, T def1, T2 def2) {
        return new ZippedRSeq<>(this, rseq, def1, def2);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy