com.landawn.abacus.util.ImmutableSortedMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* 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;
/**
*
* @author Haiyang Li
* @param the key type
* @param the value type
* @since 1.1.4
*/
public class ImmutableSortedMap extends ImmutableMap implements SortedMap {
@SuppressWarnings("rawtypes")
private static final ImmutableSortedMap EMPTY = new ImmutableSortedMap(N.emptySortedMap());
private final SortedMap sortedMap;
ImmutableSortedMap(SortedMap extends K, ? extends V> sortedMap) {
super(sortedMap);
this.sortedMap = (SortedMap) sortedMap;
}
/**
*
* @param the key type
* @param the value type
* @return
*/
public static ImmutableSortedMap empty() {
return EMPTY;
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @param k1
* @param v1
* @return
*/
public static , V> ImmutableSortedMap of(final K k1, final V v1) {
final SortedMap map = N.newTreeMap();
map.put(k1, v1);
return new ImmutableSortedMap<>(map);
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @param k1
* @param v1
* @param k2
* @param v2
* @return
*/
public static , 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);
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @param k1
* @param v1
* @param k2
* @param v2
* @param k3
* @param v3
* @return
*/
public static , 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);
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @param k1
* @param v1
* @param k2
* @param v2
* @param k3
* @param v3
* @param k4
* @param v4
* @return
*/
public static , 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);
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @param k1
* @param v1
* @param k2
* @param v2
* @param k3
* @param v3
* @param k4
* @param v4
* @param k5
* @param v5
* @return
*/
public static , 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);
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @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> 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);
}
/**
*
* @param the key type
* @param the value type
* @param
* @param
* @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> 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 the key type
* @param the value type
* @param sortedMap the elements in this map
are shared by the returned ImmutableSortedMap.
* @return
*/
public static ImmutableSortedMap of(final SortedMap extends K, ? extends V> sortedMap) {
if (sortedMap == null) {
return empty();
} else if (sortedMap instanceof ImmutableSortedMap) {
return (ImmutableSortedMap) sortedMap;
}
return new ImmutableSortedMap<>(sortedMap);
}
/**
*
* @param the key type
* @param the value type
* @param sortedMap
* @return
*/
public static ImmutableSortedMap copyOf(final SortedMap extends K, ? extends V> sortedMap) {
if (N.isNullOrEmpty(sortedMap)) {
return empty();
}
return new ImmutableSortedMap<>(new TreeMap<>(sortedMap));
}
/**
*
* @param the key type
* @param the value type
* @param map
* @return
* @deprecated throws {@code UnsupportedOperationException}
* @throws UnsupportedOperationException
*/
@Deprecated
public static ImmutableMap of(final Map extends K, ? extends V> map) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
*
* @param the key type
* @param the value type
* @param map
* @return
* @deprecated throws {@code UnsupportedOperationException}
* @throws UnsupportedOperationException
*/
@Deprecated
public static ImmutableMap copyOf(final Map extends K, ? extends V> map) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public Comparator super K> comparator() {
return sortedMap.comparator();
}
/**
*
* @param fromKey
* @param toKey
* @return
*/
@Override
public SortedMap subMap(K fromKey, K toKey) {
return of(sortedMap.subMap(fromKey, toKey));
}
/**
*
* @param toKey
* @return
*/
@Override
public SortedMap headMap(K toKey) {
return of(sortedMap.headMap(toKey));
}
/**
*
* @param fromKey
* @return
*/
@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();
}
}