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

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

/*
 * @(#)ReadOnlyNavigableMapFacade.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */

package org.jhotdraw8.icollection.facade;

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

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
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 ReadOnlyNavigableMap} facade to a set of {@code ReadOnlyNavigableMap} functions.
 *
 * @param  the key type
 * @param  the value type
 * @author Werner Randelshofer
 */
public class ReadOnlyNavigableMapFacade extends ReadOnlyMapFacade
        implements ReadOnlyNavigableMap {
    private final Supplier> firstEntryFunction;
    private final Supplier> lastEntryFunction;
    private final Supplier>> reverseIteratorFunction;
    private final int characteristics;
    final Function> ceilingFunction;
    final Function> floorFunction;
    final Function> higherFunction;
    final Function> lowerFunction;
    private final Supplier> comparatorSupplier;

    public ReadOnlyNavigableMapFacade(NavigableMap target) {
        super(target);
        this.firstEntryFunction = target::firstEntry;
        this.lastEntryFunction = target::lastEntry;
        this.reverseIteratorFunction = () -> target.reversed().sequencedEntrySet().iterator();
        this.characteristics = Spliterator.ORDERED | Spliterator.SIZED | Spliterator.DISTINCT;
        this.comparatorSupplier = target::comparator;
        this.ceilingFunction = target::ceilingEntry;
        this.floorFunction = target::floorEntry;
        this.higherFunction = target::higherEntry;
        this.lowerFunction = target::lowerEntry;
    }

    public ReadOnlyNavigableMapFacade(
            Supplier>> iteratorFunction,
            Supplier>> reverseIteratorFunction,
            IntSupplier sizeFunction,
            Predicate containsKeyFunction,
            Function getFunction,
            Supplier> firstEntryFunction,
            Supplier> lastEntryFunction,
            final Function> ceilingFunction,
            final Function> floorFunction,
            final Function> higherFunction,
            final Function> lowerFunction,
            int characteristics, @Nullable Supplier> comparator) {
        super(iteratorFunction, sizeFunction, containsKeyFunction, getFunction);
        this.firstEntryFunction = firstEntryFunction;
        this.lastEntryFunction = lastEntryFunction;
        this.reverseIteratorFunction = reverseIteratorFunction;
        this.characteristics = characteristics;
        this.comparatorSupplier = comparator;
        this.ceilingFunction = ceilingFunction;
        this.floorFunction = floorFunction;
        this.higherFunction = higherFunction;
        this.lowerFunction = lowerFunction;
    }

    @Override
    public Map.@Nullable Entry ceilingEntry(K k) {
        return ceilingFunction.apply(k);
    }

    @Override
    public Map.@Nullable Entry floorEntry(K k) {
        return floorFunction.apply(k);
    }

    @Override
    public Map.@Nullable Entry higherEntry(K k) {
        return higherFunction.apply(k);
    }

    @Override
    public Map.@Nullable Entry lowerEntry(K k) {
        return lowerFunction.apply(k);
    }

    @Override
    public ReadOnlyNavigableMap readOnlyReversed() {
        return new ReadOnlyNavigableMapFacade<>(
                reverseIteratorFunction,
                iteratorFunction,
                sizeFunction,
                containsKeyFunction,
                getFunction,
                lastEntryFunction,
                firstEntryFunction,
                floorFunction,
                ceilingFunction,
                lowerFunction,
                higherFunction,
                characteristics, () -> comparatorSupplier.get().reversed());
    }

    @Override
    public Map.@Nullable Entry firstEntry() {
        return firstEntryFunction.get();
    }

    @Override
    public Map.@Nullable Entry lastEntry() {
        return lastEntryFunction.get();
    }

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

    @Override
    public int characteristics() {
        return characteristics;
    }
}