com.landawn.abacus.util.ImmutableMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* Copyright (C) 2016 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.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
*
* @since 0.8
*
* @author Haiyang Li
*/
public class ImmutableMap implements Map {
@SuppressWarnings("rawtypes")
private static final ImmutableMap EMPTY = new ImmutableMap(Collections.EMPTY_MAP);
private final Map map;
ImmutableMap(final Map extends K, ? extends V> map) {
this.map = Collections.unmodifiableMap(map);
}
public static ImmutableMap empty() {
return EMPTY;
}
public static ImmutableMap of(final k k1, final v v1) {
final Map map = N.asLinkedHashMap(k1, v1);
return new ImmutableMap(map);
}
public static ImmutableMap of(final k k1, final v v1, final k k2, final v v2) {
final Map map = N.asLinkedHashMap(k1, v1, k2, v2);
return new ImmutableMap(map);
}
public static ImmutableMap of(final k k1, final v v1, final k k2, final v v2, final k k3, final v v3) {
final Map map = N.asLinkedHashMap(k1, v1, k2, v2, k3, v3);
return new ImmutableMap(map);
}
public static ImmutableMap 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 Map map = N.asLinkedHashMap(k1, v1, k2, v2, k3, v3, k4, v4);
return new ImmutableMap(map);
}
public static ImmutableMap 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 Map map = N.asLinkedHashMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
return new ImmutableMap(map);
}
public static ImmutableMap 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 Map map = N.asLinkedHashMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6);
return new ImmutableMap(map);
}
public static ImmutableMap 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 Map map = N.asLinkedHashMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7);
return new ImmutableMap(map);
}
/**
*
* @param map the elements in this map
are shared by the returned ImmutableMap.
* @return
*/
public static ImmutableMap of(final Map extends K, ? extends V> map) {
if (map == null) {
return empty();
} else if (map instanceof ImmutableMap) {
return (ImmutableMap) map;
}
return new ImmutableMap<>(map);
}
public static ImmutableMap copyOf(final Map extends K, ? extends V> map) {
if (N.isNullOrEmpty(map)) {
return empty();
}
final Map tmp = map instanceof IdentityHashMap ? new IdentityHashMap<>(map)
: ((map instanceof LinkedHashMap || map instanceof SortedMap) ? new LinkedHashMap<>(map) : new HashMap<>(map));
return new ImmutableMap<>(tmp);
}
@Override
public V getOrDefault(Object key, V defaultValue) {
final V val = get(key);
return val == null && containsKey(key) == false ? defaultValue : val;
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V put(K k, V v) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V remove(Object o) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final void putAll(Map extends K, ? extends V> map) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V putIfAbsent(K key, V value) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final boolean remove(Object key, Object value) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final boolean replace(K key, V oldValue, V newValue) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V replace(K key, V value) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V computeIfAbsent(K key, Function super K, ? extends V> mappingFunction) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V computeIfPresent(K key, BiFunction super K, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V compute(K key, BiFunction super K, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final V merge(K key, V value, BiFunction super V, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException();
}
/**
* @deprecated Unsupported operation.
*/
@Deprecated
@Override
public final void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public V get(Object key) {
return map.get(key);
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public Collection values() {
return map.values();
}
@Override
public Set> entrySet() {
return map.entrySet();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean equals(Object obj) {
return obj instanceof ImmutableMap && ((ImmutableMap) obj).map.equals(map);
}
@Override
public int hashCode() {
return map.hashCode();
}
@Override
public String toString() {
return map.toString();
}
}