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

org.jetbrains.kotlin.js.util.Maps Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package org.jetbrains.kotlin.js.util;

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

/**
 * Utility methods for operating on memory-efficient maps. All maps of size 0 or
 * 1 are assumed to be immutable. All maps of size greater than 1 are assumed to
 * be mutable.
 */
public class Maps {
    private Maps() {
    }

    public static  Map put(Map map, K key, V value) {
        switch (map.size()) {
            case 0:
                // Empty -> Singleton
                return Collections.singletonMap(key, value);
            case 1: {
                if (map.containsKey(key)) {
                    return Collections.singletonMap(key, value);
                }
                // Singleton -> HashMap
                Map result = new HashMap();
                result.put(map.keySet().iterator().next(), map.values().iterator().next());
                result.put(key, value);
                return result;
            }
            default:
                // HashMap
                map.put(key, value);
                return map;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy