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

io.yupiik.batch.runtime.iterator.FluentIterator Maven / Gradle / Ivy

/*
 * Copyright (c) 2021-2022 - Yupiik SAS - https://www.yupiik.com
 * 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.
 */
package io.yupiik.batch.runtime.iterator;

import io.yupiik.batch.runtime.batch.builder.BatchChain;

import java.util.Comparator;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Predicate;

// stream like API but iterator oriented with less overhead
public class FluentIterator implements Iterator, BatchChain.Commentifiable, AutoCloseable {
    private final Iterator delegate;

    private FluentIterator(final Iterator delegate) {
        this.delegate = delegate;
    }

    public FluentIterator filter(final Predicate tester) {
        return new FluentIterator<>(new FilteringIterator<>(delegate, tester));
    }

    public  FluentIterator map(final Function function) {
        return new FluentIterator<>(new MappingIterator<>(delegate, function));
    }

    public  FluentIterator flatMap(final Function> function) {
        return new FluentIterator<>(new FlatMappingIterator<>(delegate, function));
    }

    public FluentIterator sort(final Comparator comparator) {
        return new FluentIterator<>(new SortingIterator<>(delegate, comparator));
    }

    public  FluentIterator distinct(final Function keyExtractor, final Comparator comparator) {
        return new FluentIterator<>(new DistinctIterator<>(delegate, keyExtractor, comparator));
    }

    public CommentedIterator withComment(final String comment) {
        return new CommentedIterator<>(comment, delegate);
    }

    public Iterator unwrap() {
        return delegate;
    }

    public static  FluentIterator of(final Iterator it) {
        return new FluentIterator<>(it);
    }

    @Override
    public boolean hasNext() {
        return delegate.hasNext();
    }

    @Override
    public A next() {
        return delegate.next();
    }

    @Override
    public void close() throws Exception {
        if (AutoCloseable.class.isInstance(delegate)) {
            AutoCloseable.class.cast(delegate).close();
        }
    }

    public static class CommentedIterator implements Iterator, BatchChain.Commentifiable, AutoCloseable {
        private final String comment;
        private final Iterator delegate;

        public CommentedIterator(final String comment, final Iterator delegate) {
            this.comment = comment;
            this.delegate = delegate;
        }

        @Override
        public boolean hasNext() {
            return delegate.hasNext();
        }

        @Override
        public A next() {
            return delegate.next();
        }

        @Override
        public String toComment() {
            return comment;
        }

        @Override
        public void close() throws Exception {
            if (AutoCloseable.class.isInstance(delegate)) {
                AutoCloseable.class.cast(delegate).close();
            }
        }
    }
}