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

com.landawn.abacus.util.ImmutableNavigableMap Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.2.4
Show newest version
/*
 * Copyright (C) 2017 HaiYang Li
 *
 * 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 com.landawn.abacus.util;

import java.util.Map;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * @author Haiyang Li
 * @param  the key type
 * @param  the value type
 * @since 1.1.4
 */
@SuppressWarnings("java:S2160")
public class ImmutableNavigableMap extends ImmutableSortedMap implements NavigableMap {

    @SuppressWarnings("rawtypes")
    private static final ImmutableNavigableMap EMPTY = new ImmutableNavigableMap(N.emptyNavigableMap());

    private final NavigableMap navigableMap;

    ImmutableNavigableMap(NavigableMap navigableMap) {
        super(navigableMap);
        this.navigableMap = (NavigableMap) navigableMap;
    }

    /**
     *
     * @param  the key type
     * @param  the value type
     * @return
     */
    public static  ImmutableNavigableMap empty() {
        return EMPTY;
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @param k2 
     * @param v2 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1, final K k2, final V v2) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);
        map.put(k2, v2);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @param k2 
     * @param v2 
     * @param k3 
     * @param v3 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1, final K k2, final V v2, final K k3, final V v3) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @param k2 
     * @param v2 
     * @param k3 
     * @param v3 
     * @param k4 
     * @param v4 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1, final K k2, final V v2, final K k3, final V v3,
            final K k4, final V v4) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @param k2 
     * @param v2 
     * @param k3 
     * @param v3 
     * @param k4 
     * @param v4 
     * @param k5 
     * @param v5 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1, final K k2, final V v2, final K k3, final V v3,
            final K k4, final V v4, final K k5, final V v5) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @param k2 
     * @param v2 
     * @param k3 
     * @param v3 
     * @param k4 
     * @param v4 
     * @param k5 
     * @param v5 
     * @param k6 
     * @param v6 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1, final K k2, final V v2, final K k3, final V v3,
            final K k4, final V v4, final K k5, final V v5, final K k6, final V v6) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);
        map.put(k6, v6);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param k1 
     * @param v1 
     * @param k2 
     * @param v2 
     * @param k3 
     * @param v3 
     * @param k4 
     * @param v4 
     * @param k5 
     * @param v5 
     * @param k6 
     * @param v6 
     * @param k7 
     * @param v7 
     * @return 
     */
    public static , V> ImmutableNavigableMap of(final K k1, final V v1, final K k2, final V v2, final K k3, final V v3,
            final K k4, final V v4, final K k5, final V v5, final K k6, final V v6, final K k7, final V v7) {
        final NavigableMap map = N.newTreeMap();

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);
        map.put(k6, v6);
        map.put(k7, v7);

        return new ImmutableNavigableMap<>(map);
    }

    /**
     *
     * @param  the key type
     * @param  the value type
     * @param sortedMap
     * @return
     */
    public static  ImmutableNavigableMap copyOf(final SortedMap sortedMap) {
        if (N.isNullOrEmpty(sortedMap)) {
            return empty();
        }

        return new ImmutableNavigableMap<>(new TreeMap<>(sortedMap));
    }

    /**
     *
     * @param  the key type
     * @param  the value type
     * @param navigableMap
     * @return an {@code ImmutableNavigableMap} backed by the specified {@code navigableMap}
     * @deprecated the ImmutableNavigableMap may be modified through the specified {@code navigableMap}
     */
    @Deprecated
    public static  ImmutableNavigableMap wrap(final NavigableMap navigableMap) {
        if (navigableMap == null) {
            return empty();
        } else if (navigableMap instanceof ImmutableNavigableMap) {
            return (ImmutableNavigableMap) navigableMap;
        }

        return new ImmutableNavigableMap<>(navigableMap);
    }

    /**
     * 
     *
     * @param  the key type
     * @param  the value type
     * @param sortedMap 
     * @return 
     * @throws UnsupportedOperationException 
     * @deprecated throws {@code UnsupportedOperationException}
     */
    @Deprecated
    public static  ImmutableSortedMap wrap(final SortedMap sortedMap) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public ImmutableEntry lowerEntry(K key) {
        return ImmutableEntry.copyOf(navigableMap.lowerEntry(key));
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public K lowerKey(K key) {
        return navigableMap.lowerKey(key);
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public ImmutableEntry floorEntry(K key) {
        return ImmutableEntry.copyOf(navigableMap.floorEntry(key));
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public K floorKey(K key) {
        return navigableMap.floorKey(key);
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public ImmutableEntry ceilingEntry(K key) {
        return ImmutableEntry.copyOf(navigableMap.ceilingEntry(key));
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public K ceilingKey(K key) {
        return navigableMap.ceilingKey(key);
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public ImmutableEntry higherEntry(K key) {
        return ImmutableEntry.copyOf(navigableMap.higherEntry(key));
    }

    /**
     *
     * @param key
     * @return
     */
    @Override
    public K higherKey(K key) {
        return navigableMap.higherKey(key);
    }

    /**
     * 
     *
     * @return 
     */
    @Override
    public ImmutableEntry firstEntry() {
        return ImmutableEntry.copyOf(navigableMap.firstEntry());
    }

    /**
     * 
     *
     * @return 
     */
    @Override
    public ImmutableEntry lastEntry() {
        return ImmutableEntry.copyOf(navigableMap.lastEntry());
    }

    /**
     * Poll first entry.
     *
     * @return 
     * @throws UnsupportedOperationException 
     * @deprecated throws {@code UnsupportedOperationException}
     */
    @Deprecated
    @Override
    public Map.Entry pollFirstEntry() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Poll last entry.
     *
     * @return 
     * @throws UnsupportedOperationException 
     * @deprecated throws {@code UnsupportedOperationException}
     */
    @Deprecated
    @Override
    public Map.Entry pollLastEntry() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * 
     *
     * @return 
     */
    @Override
    public ImmutableNavigableMap descendingMap() {
        return wrap(navigableMap.descendingMap());
    }

    /**
     * Navigable key set.
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    @Override
    public ImmutableNavigableSet navigableKeySet() {
        return ImmutableNavigableSet.wrap(navigableMap.navigableKeySet());
    }

    /**
     * Descending key set.
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    @Override
    public ImmutableNavigableSet descendingKeySet() {
        return ImmutableNavigableSet.wrap(navigableMap.descendingKeySet());
    }

    /**
     *
     * @param fromKey
     * @param fromInclusive
     * @param toKey
     * @param toInclusive
     * @return
     */
    @Override
    public ImmutableNavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
        return wrap(navigableMap.subMap(fromKey, fromInclusive, toKey, toInclusive));
    }

    /**
     *
     * @param toKey
     * @param inclusive
     * @return
     */
    @Override
    public ImmutableNavigableMap headMap(K toKey, boolean inclusive) {
        return wrap(navigableMap.headMap(toKey, inclusive));
    }

    /**
     *
     * @param fromKey
     * @param inclusive
     * @return
     */
    @Override
    public ImmutableNavigableMap tailMap(K fromKey, boolean inclusive) {
        return wrap(navigableMap.tailMap(fromKey, inclusive));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy