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

org.jhotdraw8.icollection.facade.ReadOnlyNavigableSetFacade Maven / Gradle / Ivy

/*
 * @(#)ReadOnlyNavigableSetFacade.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */
package org.jhotdraw8.icollection.facade;

import org.jhotdraw8.icollection.readonly.ReadOnlyNavigableSet;
import org.jspecify.annotations.Nullable;

import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.Spliterator;
import java.util.function.Function;
import java.util.function.IntSupplier;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Provides a {@link ReadOnlyNavigableSet} facade to a set of {@code NavigableSet} functions.
 *
 * @param  the element type
 * @author Werner Randelshofer
 */
public class ReadOnlyNavigableSetFacade extends ReadOnlySetFacade
        implements ReadOnlyNavigableSet {

    final Supplier getFirstFunction;
    final Function ceilingFunction;
    final Function floorFunction;
    final Function higherFunction;
    final Function lowerFunction;
    final Supplier getLastFunction;
    final Supplier> comparatorSupplier;
    final Supplier> reverseIteratorFunction;

    public ReadOnlyNavigableSetFacade(NavigableSet s) {
        this(s::iterator, () -> s.reversed().iterator(), s::size,
                s::contains, s::getFirst, s::getLast,
                s::ceiling, s::floor, s::higher, s::lower, s::comparator,
                Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.ORDERED);
    }

    public ReadOnlyNavigableSetFacade(Supplier> iteratorFunction,
                                      Supplier> reverseIteratorFunction,
                                      IntSupplier sizeFunction,
                                      Predicate containsFunction,
                                      Supplier getFirstFunction,
                                      Supplier getLastFunction,
                                      final Function ceilingFunction,
                                      final Function floorFunction,
                                      final Function higherFunction,
                                      final Function lowerFunction,
                                      final Supplier> comparatorSupplier,
                                      int characteristics) {
        super(iteratorFunction, sizeFunction, containsFunction, characteristics);
        this.getFirstFunction = getFirstFunction;
        this.getLastFunction = getLastFunction;
        this.reverseIteratorFunction = reverseIteratorFunction;
        this.ceilingFunction = ceilingFunction;
        this.floorFunction = floorFunction;
        this.higherFunction = higherFunction;
        this.lowerFunction = lowerFunction;
        this.comparatorSupplier = comparatorSupplier;
    }

    @Override
    public @Nullable E ceiling(E e) {
        return ceilingFunction.apply(e);
    }

    @Override
    public @Nullable Comparator comparator() {
        return comparatorSupplier.get();
    }

    @Override
    public @Nullable E floor(E e) {
        return floorFunction.apply(e);
    }

    @Override
    public @Nullable E higher(E e) {
        return higherFunction.apply(e);
    }

    @Override
    public @Nullable E lower(E e) {
        return lowerFunction.apply(e);
    }


    @Override
    public E getFirst() {
        return getFirstFunction.get();
    }

    @Override
    public E getLast() {
        return getLastFunction.get();
    }

    @Override
    public ReadOnlyNavigableSet readOnlyReversed() {
        return new ReadOnlyNavigableSetFacade<>(
                reverseIteratorFunction,
                iteratorFunction,
                sizeFunction,
                containsFunction,
                getLastFunction,
                getFirstFunction,
                floorFunction,
                ceilingFunction,
                lowerFunction,
                higherFunction,
                () -> comparatorSupplier.get().reversed(),
                super.characteristics);
    }
}