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

com.google.gwt.emul.java.util.Map Maven / Gradle / Ivy

There is a newer version: 2.12.1
Show newest version
/*
 * Copyright 2007 Google Inc.
 *
 * 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 java.util;

import static javaemul.internal.InternalPreconditions.checkArgument;
import static javaemul.internal.InternalPreconditions.checkNotNull;

import java.io.Serializable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsNonNull;
import jsinterop.annotations.JsType;

/**
 * Abstract interface for maps.
 *
 * @param  key type.
 * @param  value type.
 */
@JsType
public interface Map {

  @JsIgnore
  static  Map of() {
    return Collections.unmodifiableMap(Collections.emptyMap());
  }

  @JsIgnore
  static  Map of(K key, V value) {
    return ofEntries(
            entry(key, value)
    );
  }

  @JsIgnore
  static  Map of(K k1, V v1, K k2, V v2) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2)
    );
  }

  @JsIgnore
  static  Map of(
      K k1, V v1,
      K k2, V v2,
      K k3, V v3
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3)
    );
  }

  @JsIgnore
  static  Map of(
      K k1, V v1,
      K k2, V v2,
      K k3, V v3,
      K k4, V v4
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4)
    );
  }

  @JsIgnore
  static  Map of(
      K k1, V v1,
      K k2, V v2,
      K k3, V v3,
      K k4, V v4,
      K k5, V v5
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4),
            entry(k5, v5)
    );
  }

  @JsIgnore
  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
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4),
            entry(k5, v5),
            entry(k6, v6)
    );
  }

  @JsIgnore
  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
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4),
            entry(k5, v5),
            entry(k6, v6),
            entry(k7, v7)
    );
  }

  @JsIgnore
  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
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4),
            entry(k5, v5),
            entry(k6, v6),
            entry(k7, v7),
            entry(k8, v8)
    );
  }

  @JsIgnore
  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,
      K k9, V v9
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4),
            entry(k5, v5),
            entry(k6, v6),
            entry(k7, v7),
            entry(k8, v8),
            entry(k9, v9)
    );
  }

  @JsIgnore
  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,
      K k9, V v9,
      K k10, V v10
  ) {
    return ofEntries(
            entry(k1, v1),
            entry(k2, v2),
            entry(k3, v3),
            entry(k4, v4),
            entry(k5, v5),
            entry(k6, v6),
            entry(k7, v7),
            entry(k8, v8),
            entry(k9, v9),
            entry(k10, v10)
    );
  }

  @JsIgnore
  static  Entry entry(K key, V value) {
    // This isn't quite consistent with the javadoc, since this is serializable, while entry()
    // need not be serializable.
    return new AbstractMap.SimpleImmutableEntry<>(
        checkNotNull(key),
        checkNotNull(value)
    );
  }

  @JsIgnore
  static  Map ofEntries(Entry... entries) {
    Map map = new HashMap<>();

    for (int i = 0; i < entries.length; i++) {
      // TODO this perhaps can be optimized if we know the entry is an instance of
      //  AbstractMap.SimpleImmutableEntry, or something more specialized?
      Entry entry = checkNotNull(entries[i]);
      checkArgument(map.put(checkNotNull(entry.getKey()), checkNotNull(entry.getValue())) == null,
              "Can't add multiple entries with the same key");
    }

    return Collections.unmodifiableMap(map);
  }

  /**
   * Represents an individual map entry.
   */
  interface Entry {
    @Override
    boolean equals(Object o);

    K getKey();

    V getValue();

    @Override
    int hashCode();

    V setValue(V value);

    static , V> Comparator> comparingByKey() {
      return comparingByKey(Comparator.naturalOrder());
    }

    static  Comparator> comparingByKey(Comparator cmp) {
      checkNotNull(cmp);
      return (Comparator> & Serializable)
          (a, b) -> cmp.compare(a.getKey(), b.getKey());
    }

    static > Comparator> comparingByValue() {
      return comparingByValue(Comparator.naturalOrder());
    }

    static  Comparator> comparingByValue(Comparator cmp) {
      checkNotNull(cmp);
      return (Comparator> & Serializable)
          (a, b) -> cmp.compare(a.getValue(), b.getValue());
    }
  }

  void clear();

  @JsIgnore
  default V compute(K key, BiFunction remappingFunction) {
    checkNotNull(remappingFunction);

    V value = remappingFunction.apply(key, get(key));
    if (value != null) {
      put(key, value);
    } else {
      remove(key);
    }
    return value;
  }

  @JsIgnore
  default V computeIfAbsent(K key, Function remappingFunction) {
    checkNotNull(remappingFunction);

    V value = get(key);
    if (value == null) {
      value = remappingFunction.apply(key);
      if (value != null) {
        put(key, value);
      }
    }
    return value;
  }

  @JsIgnore
  default V computeIfPresent(
      K key, BiFunction remappingFunction) {
    checkNotNull(remappingFunction);

    V value = get(key);
    if (value != null) {
      value = remappingFunction.apply(key, value);
      if (value != null) {
        put(key, value);
      } else {
        remove(key);
      }
    }
    return value;
  }

  boolean containsKey(Object key);

  boolean containsValue(Object value);

  @JsIgnore
  Set> entrySet();

  @JsIgnore
  default void forEach(BiConsumer consumer) {
    checkNotNull(consumer);
    for (Entry entry : entrySet()) {
      consumer.accept(entry.getKey(), entry.getValue());
    }
  }

  V get(Object key);

  default V getOrDefault(Object key, V defaultValue) {
    V currentValue = get(key);
    return (currentValue == null && !containsKey(key)) ? defaultValue : currentValue;
  }

  boolean isEmpty();

  @JsNonNull
  Set keySet();

  @JsIgnore
  default V merge(K key, V value, BiFunction remappingFunction) {
    checkNotNull(remappingFunction);
    checkNotNull(value);

    V currentValue = get(key);
    V newValue = currentValue == null ? value : remappingFunction.apply(currentValue, value);
    if (newValue == null) {
      remove(key);
    } else {
      put(key, newValue);
    }
    return newValue;
  }

  V put(K key, V value);

  default V putIfAbsent(K key, V value) {
    V currentValue = get(key);
    return currentValue != null ? currentValue : put(key, value);
  }

  void putAll(Map t);

  V remove(Object key);

  @JsIgnore
  default boolean remove(Object key, Object value) {
    Object currentValue = get(key);
    if (!Objects.equals(currentValue, value) || (currentValue == null && !containsKey(key))) {
      return false;
    }
    remove(key);
    return true;
  }

  default V replace(K key, V value) {
    return containsKey(key) ? put(key, value) : null;
  }

  @JsIgnore
  default boolean replace(K key, V oldValue, V newValue) {
    Object currentValue = get(key);
    if (!Objects.equals(currentValue, oldValue) || (currentValue == null && !containsKey(key))) {
      return false;
    }
    put(key, newValue);
    return true;
  }

  @JsIgnore
  default void replaceAll(BiFunction function) {
    checkNotNull(function);
    for (Entry entry : entrySet()) {
      entry.setValue(function.apply(entry.getKey(), entry.getValue()));
    }
  }

  int size();

  @JsNonNull Collection values();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy