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

com.caoccao.javet.swc4j.utils.SimpleMap Maven / Gradle / Ivy

/*
 * Copyright (c) 2024. caoccao.com Sam Cao
 *
 * 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.caoccao.javet.swc4j.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Simple map is a polyfill because Map.of() is not available at JDK 8 .
 *
 * @since 0.2.0
 */
public final class SimpleMap {
    private SimpleMap() {
    }

    /**
     * Immutable map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param map the map
     * @return the map
     * @since 0.4.0
     */
    public static  Map immutable(Map map) {
        return Collections.unmodifiableMap(map);
    }

    /**
     * Immutable of map.
     *
     * @param      the type parameter
     * @param      the type parameter
     * @param objects the objects
     * @return the map
     * @since 0.8.0
     */
    public static  Map immutableOf(Object... objects) {
        return immutable(of(objects));
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1
    ) {
        HashMap map = new HashMap();
        map.put(k1, v1);
        return map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2
    ) {
        HashMap map = new HashMap();
        map.put(k1, v1);
        map.put(k2, v2);
        return map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @param k3  the k3
     * @param v3  the v3
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2,
            K k3, V v3
    ) {
        HashMap map = new HashMap();
        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        return map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @param k3  the k3
     * @param v3  the v3
     * @param k4  the k4
     * @param v4  the v4
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2,
            K k3, V v3,
            K k4, V v4
    ) {
        HashMap map = new HashMap();
        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        return map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @param k3  the k3
     * @param v3  the v3
     * @param k4  the k4
     * @param v4  the v4
     * @param k5  the k5
     * @param v5  the v5
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2,
            K k3, V v3,
            K k4, V v4,
            K k5, V v5
    ) {
        HashMap map = new HashMap();
        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);
        return map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @param k3  the k3
     * @param v3  the v3
     * @param k4  the k4
     * @param v4  the v4
     * @param k5  the k5
     * @param v5  the v5
     * @param k6  the k6
     * @param v6  the v6
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2,
            K k3, V v3,
            K k4, V v4,
            K k5, V v5,
            K k6, V v6
    ) {
        HashMap map = new HashMap();
        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 map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @param k3  the k3
     * @param v3  the v3
     * @param k4  the k4
     * @param v4  the v4
     * @param k5  the k5
     * @param v5  the v5
     * @param k6  the k6
     * @param v6  the v6
     * @param k7  the k7
     * @param v7  the v7
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2,
            K k3, V v3,
            K k4, V v4,
            K k5, V v5,
            K k6, V v6,
            K k7, V v7
    ) {
        HashMap map = new HashMap();
        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 map;
    }

    /**
     * Of map.
     *
     * @param  the type parameter
     * @param  the type parameter
     * @param k1  the k1
     * @param v1  the v1
     * @param k2  the k2
     * @param v2  the v2
     * @param k3  the k3
     * @param v3  the v3
     * @param k4  the k4
     * @param v4  the v4
     * @param k5  the k5
     * @param v5  the v5
     * @param k6  the k6
     * @param v6  the v6
     * @param k7  the k7
     * @param v7  the v7
     * @param k8  the k8
     * @param v8  the v8
     * @return the map
     * @since 0.2.0
     */
    public static  Map of(
            K k1, V v1,
            K k2, V v2,
            K k3, V v3,
            K k4, V v4,
            K k5, V v5,
            K k6, V v6,
            K k7, V v7,
            K k8, V v8
    ) {
        HashMap map = new HashMap();
        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);
        map.put(k8, v8);
        return map;
    }

    /**
     * Of map.
     *
     * @param      the type parameter
     * @param      the type parameter
     * @param objects the objects
     * @return the map
     * @since 0.2.0
     */
    @SuppressWarnings("unchecked")
    public static  Map of(Object... objects) {
        final int length = objects.length;
        AssertionUtils.notTrue(length % 2 == 0, "Object count must be an odd number.");
        HashMap map = new HashMap();
        for (int i = 0; i < length; i += 2) {
            map.put((K) objects[i], (V) objects[i + 1]);
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy