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

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

There is a newer version: 1.10.1
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.Comparator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 
 * @since 1.1.4
 * 
 * @author Haiyang Li
 */
public class ImmutableSortedMap extends ImmutableMap implements SortedMap {

    @SuppressWarnings("rawtypes")
    private static final ImmutableSortedMap EMPTY = new ImmutableSortedMap(N.emptySortedMap());

    private final SortedMap sortedMap;

    ImmutableSortedMap(SortedMap sortedMap) {
        super(sortedMap);
        this.sortedMap = (SortedMap) sortedMap;
    }

    public static  ImmutableSortedMap empty() {
        return EMPTY;
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap of(final k k1, final v v1) {
        final SortedMap map = N.newTreeMap();

        map.put(k1, v1);

        return new ImmutableSortedMap<>(map);
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap of(final k k1, final v v1, final k k2, final v v2) {
        final SortedMap map = N.newTreeMap();

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

        return new ImmutableSortedMap<>(map);
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap of(final k k1, final v v1, final k k2, final v v2,
            final k k3, final v v3) {
        final SortedMap map = N.newTreeMap();

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

        return new ImmutableSortedMap<>(map);
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap 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 SortedMap map = N.newTreeMap();

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

        return new ImmutableSortedMap<>(map);
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap 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 SortedMap 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 ImmutableSortedMap<>(map);
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap 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 SortedMap 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 ImmutableSortedMap<>(map);
    }

    public static , V, k extends K, v extends V> ImmutableSortedMap 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 SortedMap 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 ImmutableSortedMap<>(map);
    }

    /**
     * 
     * @param sortedMap the elements in this map are shared by the returned ImmutableSortedMap.
     * @return
     */
    public static  ImmutableSortedMap of(final SortedMap sortedMap) {
        if (sortedMap == null) {
            return empty();
        } else if (sortedMap instanceof ImmutableSortedMap) {
            return (ImmutableSortedMap) sortedMap;
        }

        return new ImmutableSortedMap<>(sortedMap);
    }

    public static  ImmutableSortedMap copyOf(final SortedMap sortedMap) {
        if (N.isNullOrEmpty(sortedMap)) {
            return empty();
        }

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

    @Deprecated
    public static  ImmutableMap of(final Map map) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static  ImmutableMap copyOf(final Map map) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Comparator comparator() {
        return sortedMap.comparator();
    }

    @Override
    public SortedMap subMap(K fromKey, K toKey) {
        return of(sortedMap.subMap(fromKey, toKey));
    }

    @Override
    public SortedMap headMap(K toKey) {
        return of(sortedMap.headMap(toKey));
    }

    @Override
    public SortedMap tailMap(K fromKey) {
        return of(sortedMap.tailMap(fromKey));
    }

    @Override
    public K firstKey() {
        return sortedMap.firstKey();
    }

    @Override
    public K lastKey() {
        return sortedMap.lastKey();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy