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

sir.wellington.alchemy.collections.maps.Maps Maven / Gradle / Ivy

There is a newer version: 2.1
Show newest version
/*
 * Copyright 2015 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.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.annotations.concurrency.ThreadSafe;
import tech.sirwellington.alchemy.annotations.concurrency.ThreadUnsafe;

import static java.util.Collections.EMPTY_MAP;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
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)
        {
            Arrays.asList(others)
                    .stream()
                    .filter(map -> !isEmpty(map))
                    .forEach(map -> result.putAll(map));
        }

        return result;
    }

    public static  Map nullToEmpty(Map map)
    {
        return isEmpty(map) ? EMPTY_MAP : map;
    }

    public static  Map immutableCopyOf(Map map) throws IllegalArgumentException
    {
        if (map == null)
        {
            return Collections.emptyMap();
        }

        Map copy = new HashMap<>(map);
        return Collections.unmodifiableMap(copy);
    }

    public static  Map mutableCopyOf(Map map)
    {
        return copyOf(map, () -> new HashMap<>());
    }

    public static  Map copyOf(Map map, Supplier> mapSupplier)
    {

        checkThat(mapSupplier)
                .usingMessage("map supplier cannot be null")
                .is(notNull());

        Map result = mapSupplier.get();
        checkThat(result)
                .usingMessage("supplier returned a null map")
                .is(notNull());

        if (!isEmpty(map))
        {
            result.putAll(map);
        }

        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy