
package de.ivu.elect.util;

import java.util.Map;

import javax.annotation.Nonnull;

public class MapUtil {

  private MapUtil() {
    // static utility class
  }

  public static <K, V> V putIfNewOrReturnExisting(Map<K, V> map, K key, V value) {
    if (map.containsKey(key)) {
      return map.get(key);
    }

    map.put(key, value);
    return value;
  }

  public static <K, V> void putUnique(@Nonnull Map<K, V> map, K key, V value) {
    if (map.containsKey(key)) {
      throw new IllegalArgumentException("Der Schlüssel [" + key + "] ist bereits enthalten!");
    }

    map.put(key, value);
  }

}
