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

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

The newest version!
package org.jhotdraw8.icollection.facade;

import org.jhotdraw8.icollection.impl.iteration.MappedIterator;
import org.jhotdraw8.icollection.impl.iteration.MappedSpliterator;
import org.jhotdraw8.icollection.navigable.DescendingNavigableSetView;
import org.jhotdraw8.icollection.navigable.SubsetNavigableSetView;
import org.jspecify.annotations.Nullable;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.SortedSet;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntSupplier;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class NavigableSetFacade extends SequencedSetFacade implements NavigableSet {
    private final IntSupplier modCount = () -> 0;


    @SuppressWarnings("ReturnOfNull")
    private final Supplier> comparatorSupplier = () -> null;
    private final Function higherFunction;
    private final Function lowerFunction;
    private final Function floorFunction;
    private final Function ceilingFunction;


    public NavigableSetFacade(Supplier> iteratorFunction, Supplier> spliteratorFunction, Supplier> reverseIteratorFunction, Supplier> reverseSpliteratorFunction, IntSupplier sizeFunction, Predicate containsFunction, @Nullable Runnable clearFunction, @Nullable Predicate removeFunction, @Nullable Supplier getFirstFunction, @Nullable Supplier getLastFunction, @Nullable Predicate addFunction, @Nullable Predicate reversedAddFunction, @Nullable Consumer addFirstFunction, @Nullable Consumer addLastFunction,
                              Function higherFunction,
                              Function lowerFunction,
                              Function floorFunction,
                              Function ceilingFunction) {
        super(iteratorFunction, spliteratorFunction, reverseIteratorFunction, reverseSpliteratorFunction, sizeFunction, containsFunction, clearFunction, removeFunction, getFirstFunction, getLastFunction, addFunction, reversedAddFunction, addFirstFunction, addLastFunction);
        this.higherFunction = higherFunction;
        this.lowerFunction = lowerFunction;
        this.floorFunction = floorFunction;
        this.ceilingFunction = ceilingFunction;
    }

    @SuppressWarnings({"SuspiciousMethodCalls", "ReturnOfNull"})
    public static  NavigableSet createKeySet(NavigableMap m) {
        return new NavigableSetFacade<>(
                () -> new MappedIterator<>(m.sequencedEntrySet().iterator(), Map.Entry::getKey),
                () -> new MappedSpliterator<>(m.sequencedEntrySet().spliterator(), Map.Entry::getKey,
                        Spliterator.DISTINCT | Spliterator.SIZED | Spliterator.ORDERED, null),
                () -> new MappedIterator<>(m.reversed().sequencedEntrySet().iterator(), Map.Entry::getKey),
                () -> new MappedSpliterator<>(m.reversed().sequencedEntrySet().spliterator(), Map.Entry::getKey,
                        Spliterator.DISTINCT | Spliterator.SIZED | Spliterator.ORDERED, null),
                m::size,
                m::containsKey,
                m::clear,
                o -> {
                    if (m.containsKey(o)) {
                        m.remove(o);
                        return true;
                    }
                    return false;
                },
                () -> {
                    Map.Entry e = m.firstEntry();
                    if (e == null) {
                        throw new NoSuchElementException();
                    }
                    return e.getKey();
                },
                () -> {
                    Map.Entry e = m.lastEntry();
                    if (e == null) {
                        throw new NoSuchElementException();
                    }
                    return e.getKey();
                },
                null, null, null, null,
                e -> {
                    var entry = m.higherEntry(e);
                    return entry == null ? null : entry.getKey();
                },
                e -> {
                    var entry = m.lowerEntry(e);
                    return entry == null ? null : entry.getKey();
                },
                e -> {
                    var entry = m.floorEntry(e);
                    return entry == null ? null : entry.getKey();
                },
                e -> {
                    var entry = m.ceilingEntry(e);
                    return entry == null ? null : entry.getKey();
                }
        );
    }

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

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

    @Override
    public Iterator descendingIterator() {
        return reversed().iterator();
    }

    @Override
    public NavigableSet descendingSet() {
        return reversed();
    }

    @Override
    public E first() {
        return getFirst();
    }

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

    @Override
    public NavigableSet headSet(E toElement, boolean inclusive) {
        return new SubsetNavigableSetView<>(this, modCount,
                true, null, true, false, toElement, inclusive, true);
    }

    @Override
    public SortedSet headSet(E toElement) {
        return headSet(toElement, false);
    }

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

    @Override
    public E last() {
        return getLast();
    }

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

    @Nullable
    @Override
    public E pollFirst() {
        return isEmpty() ? null : getFirst();
    }

    @Nullable
    @Override
    public E pollLast() {
        return isEmpty() ? null : getLast();
    }

    @Override
    public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
        return new SubsetNavigableSetView<>(this, modCount,
                false, fromElement, fromInclusive, false, toElement, toInclusive, true);
    }

    @Override
    public SortedSet subSet(E fromElement, E toElement) {
        return subSet(fromElement, true, toElement, false);
    }

    @Override
    public NavigableSet tailSet(E fromElement, boolean inclusive) {
        return new SubsetNavigableSetView<>(this, modCount,
                false, fromElement, inclusive, true, null, true, true);
    }

    @Override
    public SortedSet tailSet(E fromElement) {
        return tailSet(fromElement, true);
    }

    @Override
    public NavigableSet reversed() {
        return new DescendingNavigableSetView<>(this, modCount);
    }
}