io.microsphere.collection.MapUtils Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 io.microsphere.collection;
import io.microsphere.util.BaseUtils;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Function;
import static io.microsphere.collection.CollectionUtils.size;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableMap;
/**
* The utilities class for Java {@link Map}
*
* @author Mercy
* @see Map
* @since 1.0.0
*/
public abstract class MapUtils extends BaseUtils {
/**
* The min load factor for {@link HashMap} or {@link Hashtable}
*/
public static final float MIN_LOAD_FACTOR = Float.MIN_NORMAL;
public static boolean isEmpty(Map, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isNotEmpty(Map, ?> map) {
return !isEmpty(map);
}
public static Map of(K key, V value) {
return singletonMap(key, value);
}
public static Map of(K key1, V value1, K key2, V value2) {
return ofMap(key1, value1, key2, value2);
}
public static Map of(K key1, V value1, K key2, V value2, K key3, V value3) {
return ofMap(key1, value1, key2, value2, key3, value3);
}
public static Map of(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) {
return ofMap(key1, value1, key2, value2, key3, value3, key4, value4);
}
public static Map of(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4, K key5, V value5) {
return ofMap(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5);
}
public static Map of(Object... values) {
return ofMap(values);
}
public static Map ofMap(Object... keyValuePairs) {
int length = keyValuePairs.length;
Map map = new HashMap(length / 2, MIN_LOAD_FACTOR);
for (int i = 0; i < length; ) {
map.put(keyValuePairs[i++], keyValuePairs[i++]);
}
return unmodifiableMap(map);
}
public static HashMap newHashMap() {
return new HashMap<>();
}
public static HashMap newHashMap(int initialCapacity) {
return new HashMap<>(initialCapacity);
}
public static HashMap newHashMap(int initialCapacity, float loadFactor) {
return new HashMap<>(initialCapacity, loadFactor);
}
public static HashMap newHashMap(Map extends K, ? extends V> map) {
return new HashMap<>(map);
}
public static LinkedHashMap newLinkedHashMap() {
return new LinkedHashMap<>();
}
public static LinkedHashMap newLinkedHashMap(int initialCapacity) {
return new LinkedHashMap<>(initialCapacity);
}
public static LinkedHashMap newLinkedHashMap(int initialCapacity,
float loadFactor) {
return new LinkedHashMap<>(initialCapacity, loadFactor);
}
public static LinkedHashMap newLinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
return new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
}
public static LinkedHashMap newLinkedHashMap(Map extends K, ? extends V> map) {
return new LinkedHashMap(map);
}
public static ConcurrentHashMap newConcurrentHashMap() {
return new ConcurrentHashMap<>();
}
public static ConcurrentHashMap newConcurrentHashMap(int initialCapacity) {
return new ConcurrentHashMap<>(initialCapacity);
}
public static ConcurrentHashMap newConcurrentHashMap(int initialCapacity,
float loadFactor) {
return new ConcurrentHashMap<>(initialCapacity, loadFactor);
}
public static ConcurrentHashMap newConcurrentHashMap(Map extends K, ? extends V> map) {
return new ConcurrentHashMap<>(map);
}
public static TreeMap newTreeMap() {
return new TreeMap<>();
}
public static TreeMap newTreeMap(Comparator super K> comparator) {
return new TreeMap<>(comparator);
}
public static TreeMap newTreeMap(Map extends K, ? extends V> map) {
return new TreeMap<>(map);
}
public static TreeMap newTreeMap(SortedMap map) {
return new TreeMap<>(map);
}
/**
* Shallow Clone {@link Map}
*
* @param source the source of {@link Map}
* @param the {@link Class type} of key
* @param the {@link Class type} of value
* @return non-null
*/
@Nonnull
public static Map shallowCloneMap(@Nonnull Map source) {
if (source instanceof SortedMap) {
return new TreeMap(source);
} else if (source instanceof LinkedHashMap) {
return new LinkedHashMap(source);
} else if (source instanceof IdentityHashMap) {
return new IdentityHashMap(source);
} else if (source instanceof ConcurrentNavigableMap) {
return new ConcurrentSkipListMap(source);
} else if (source instanceof ConcurrentMap) {
return new ConcurrentHashMap<>(source);
} else {
return new HashMap(source);
}
}
public static Map toFixedMap(Collection values,
Function> entryMapper) {
int size = size(values);
if (size < 1) {
return emptyMap();
}
Map fixedMap = newHashMap(size, MIN_LOAD_FACTOR);
for (E value : values) {
Map.Entry entry = entryMapper.apply(value);
fixedMap.put(entry.getKey(), entry.getValue());
}
return unmodifiableMap(fixedMap);
}
public static Map.Entry ofEntry(K key, V value) {
return immutableEntry(key, value);
}
public static Map.Entry immutableEntry(K key, V value) {
return new ImmutableEntry(key, value);
}
public static class ImmutableEntry implements Map.Entry {
private final K key;
private final V value;
public ImmutableEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException("ReadOnly Entry can't be modified");
}
}
}