All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.osgl.util.ReversibleSeqBase Maven / Gradle / Ivy
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 super T> visitor) {
super.accept(visitor);
return this;
}
@Override
public ReversibleSeqBase each($.Visitor super T> visitor) {
return accept(visitor);
}
@Override
public ReversibleSeqBase forEach($.Visitor super T> visitor) {
return accept(visitor);
}
@Override
public C.ReversibleSequence acceptLeft($.Visitor super T> 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 super T, Boolean> 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 super T, Boolean> 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 super T, Boolean> predicate) {
return FilteredRSeq.of(this, predicate);
}
@Override
public C.ReversibleSequence map($.Function super T, ? extends R> mapper) {
return MappedRSeq.of(this, mapper);
}
@Override
public C.ReversibleSequence flatMap($.Function super T, ? extends Iterable extends R>> 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 super T, Boolean> predicate) {
return reverse().findFirst(predicate);
}
@Override
public C.ReversibleSequence acceptRight($.Visitor super T> 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);
}
}