sir.wellington.alchemy.collections.maps.Maps Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemy-collections Show documentation
Show all versions of alchemy-collections Show documentation
Collect Yo'Self!
Part of the Alchemy Collection.
The newest version!
/*
* Copyright © 2019. Sir Wellington.
* 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 sir.wellington.alchemy.collections.maps;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.sirwellington.alchemy.annotations.access.Internal;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.annotations.concurrency.*;
import static tech.sirwellington.alchemy.arguments.Arguments.*;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
/**
*
* @author SirWellington
*/
@NonInstantiable
public final class Maps
{
private final static Logger LOG = LoggerFactory.getLogger(Maps.class);
Maps() throws IllegalAccessException
{
throw new IllegalAccessException("cannot instantiate this class");
}
/**
* Creates a new {@link HashMap}.
*
* @param
* @param
* @return
*/
@ThreadUnsafe
public static Map create()
{
return new HashMap<>();
}
/**
* Creates a {@link ThreadSafe} {@link ConcurrentHashMap}.
* @param
* @param
* @return
*/
@ThreadSafe
public static Map createSynchronized()
{
return new ConcurrentHashMap<>();
}
public static boolean isEmpty(Map, ?> map)
{
return map == null || map.isEmpty();
}
public static Map merge(Map firstMap, Map... others)
{
Map result = create();
if (!isEmpty(firstMap))
{
result.putAll(firstMap);
}
if (others != null)
{
for (Map map : Arrays.asList(others))
{
result.putAll(map);
}
}
return result;
}
/**
* Useful for ensuring you have an object to work with, even if its null.
*
* @param
* @param
* @param map
*
* @return An empty map if {@code map == null}, otherwise returns the map.
*/
public static Map nullToEmpty(Map map)
{
return isEmpty(map) ? Maps.emptyMap() : map;
}
/**
* Creates a shallow copy of the specified map, disallowing modification operations
* like {@linkplain Map#put(java.lang.Object, java.lang.Object) Put}.
*
* @param
* @param
* @param map
* @return
* @throws IllegalArgumentException
*/
public static Map immutableCopyOf(Map map) throws IllegalArgumentException
{
if (map == null)
{
return Collections.emptyMap();
}
Map copy = new HashMap<>(map);
return Collections.unmodifiableMap(copy);
}
/**
* Alias for {@link #copyOf(java.util.Map) }.
*
* @param
* @param
* @param map
* @return
*/
public static Map mutableCopyOf(Map map)
{
return copyOf(map);
}
/**
* Creates a shallow copy of map that is mutable.
*
* @param
* @param
* @param map
* @return
*/
public static Map copyOf(Map map)
{
Map result = create();
if (!isEmpty(map))
{
result.putAll(map);
}
return result;
}
private static void checkNotNull(Object object, String message)
{
checkThat(object).usingMessage(message).is(notNull());
}
/**
* This Singleton map is completely empty and immutable.
*/
@Internal
@Immutable
private static final Map EMPTY_MAP = immutableCopyOf(create());
/**
* Returns an Empty Map that is not designed to be mutable.
* @param
* @param
* @return
*/
@Immutable
public static Map emptyMap()
{
return EMPTY_MAP;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy