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

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

/*
 * 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.checkNotNull;

/**
 * Skeletal implementation of the Map interface. [Sun
 * docs]
 *
 * @param  the key type.
 * @param  the value type.
 */
public abstract class AbstractMap implements Map {

  /**
   * A mutable {@link Map.Entry} shared by several {@link Map} implementations.
   */
  public static class SimpleEntry extends AbstractEntry {
    public SimpleEntry(K key, V value) {
      super(key, value);
    }

    public SimpleEntry(Entry entry) {
      super(entry.getKey(), entry.getValue());
    }
  }

  /**
   * An immutable {@link Map.Entry} shared by several {@link Map} implementations.
   */
  public static class SimpleImmutableEntry extends AbstractEntry {
    public SimpleImmutableEntry(K key, V value) {
      super(key, value);
    }

    public SimpleImmutableEntry(Entry entry) {
      super(entry.getKey(), entry.getValue());
    }

    @Override
    public V setValue(V value) {
      throw new UnsupportedOperationException();
    }
  }

  /**
   * Basic {@link Map.Entry} implementation used by {@link SimpleEntry}
   * and {@link SimpleImmutableEntry}.
   */
  private abstract static class AbstractEntry implements Entry {
    private final K key;
    private V value;

    protected AbstractEntry(K key, V value) {
      this.key = key;
      this.value = value;
    }

    @Override
    public K getKey() {
      return key;
    }

    @Override
    public V getValue() {
      return value;
    }

    @Override
    public V setValue(V value) {
      V oldValue = this.value;
      this.value = value;
      return oldValue;
    }

    @Override
    public boolean equals(Object other) {
      if (!(other instanceof Entry)) {
        return false;
      }
      Entry entry = (Entry) other;
      return Objects.equals(key, entry.getKey())
          && Objects.equals(value, entry.getValue());
    }

    /**
     * Calculate the hash code using Sun's specified algorithm.
     */
    @Override
    public int hashCode() {
      return Objects.hashCode(key) ^ Objects.hashCode(value);
    }

    @Override
    public String toString() {
      // for compatibility with the real Jre: issue 3422
      return key + "=" + value;
    }
  }

  protected AbstractMap() {
  }

  @Override
  public void clear() {
    entrySet().clear();
  }

  @Override
  public boolean containsKey(Object key) {
    return implFindEntry(key, false) != null;
  }

  @Override
  public boolean containsValue(Object value) {
    for (Entry entry : entrySet()) {
      V v = entry.getValue();
      if (Objects.equals(value, v)) {
        return true;
      }
    }
    return false;
  }

  boolean containsEntry(Entry entry) {
    Object key = entry.getKey();
    Object value = entry.getValue();
    Object ourValue = get(key);

    if (!Objects.equals(value, ourValue)) {
      return false;
    }

    // Perhaps it was null and we don't contain the key?
    if (ourValue == null && !containsKey(key)) {
      return false;
    }

    return true;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof Map)) {
      return false;
    }
    Map otherMap = (Map) obj;
    if (size() != otherMap.size()) {
      return false;
    }

    for (Entry entry : otherMap.entrySet()) {
      if (!containsEntry(entry)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public V get(Object key) {
    return getEntryValueOrNull(implFindEntry(key, false));
  }

  @Override
  public int hashCode() {
    return Collections.hashCode(entrySet());
  }

  @Override
  public boolean isEmpty() {
    return size() == 0;
  }

  @Override
  public Set keySet() {
    return new AbstractSet() {
      @Override
      public void clear() {
        AbstractMap.this.clear();
      }

      @Override
      public boolean contains(Object key) {
        return containsKey(key);
      }

      @Override
      public Iterator iterator() {
        final Iterator> outerIter = entrySet().iterator();
        return new Iterator() {
          @Override
          public boolean hasNext() {
            return outerIter.hasNext();
          }

          @Override
          public K next() {
            Entry entry = outerIter.next();
            return entry.getKey();
          }

          @Override
          public void remove() {
            outerIter.remove();
          }
        };
      }

      @Override
      public boolean remove(Object key) {
        if (containsKey(key)) {
          AbstractMap.this.remove(key);
          return true;
        }
        return false;
      }

      @Override
      public int size() {
        return AbstractMap.this.size();
      }
    };
  }

  @Override
  public V put(K key, V value) {
    throw new UnsupportedOperationException("Put not supported on this map");
  }

  @Override
  public void putAll(Map map) {
    checkNotNull(map);
    for (Entry e : map.entrySet()) {
      put(e.getKey(), e.getValue());
    }
  }

  @Override
  public V remove(Object key) {
    return getEntryValueOrNull(implFindEntry(key, true));
  }

  @Override
  public int size() {
    return entrySet().size();
  }

  @Override
  public String toString() {
    StringJoiner joiner = new StringJoiner(", ", "{", "}");
    for (Entry entry : entrySet()) {
      joiner.add(toString(entry));
    }
    return joiner.toString();
  }

  private String toString(Entry entry) {
    return toString(entry.getKey()) + "=" + toString(entry.getValue());
  }

  private String toString(Object o) {
    return o == this ? "(this Map)" : String.valueOf(o);
  }

  @Override
  public Collection values() {
    return new AbstractCollection() {
      @Override
      public void clear() {
        AbstractMap.this.clear();
      }

      @Override
      public boolean contains(Object value) {
        return containsValue(value);
      }

      @Override
      public Iterator iterator() {
        final Iterator> outerIter = entrySet().iterator();
        return new Iterator() {
          @Override
          public boolean hasNext() {
            return outerIter.hasNext();
          }

          @Override
          public V next() {
            Entry entry = outerIter.next();
            return entry.getValue();
          }

          @Override
          public void remove() {
            outerIter.remove();
          }
        };
      }

      @Override
      public int size() {
        return AbstractMap.this.size();
      }
    };
  }

  static  K getEntryKeyOrNull(Entry entry) {
    return entry == null ? null : entry.getKey();
  }

  static  V getEntryValueOrNull(Entry entry) {
    return entry == null ? null : entry.getValue();
  }

  private Entry implFindEntry(Object key, boolean remove) {
    for (Iterator> iter = entrySet().iterator(); iter.hasNext();) {
      Entry entry = iter.next();
      K k = entry.getKey();
      if (Objects.equals(key, k)) {
        if (remove) {
          entry = new SimpleEntry(entry.getKey(), entry.getValue());
          iter.remove();
        }
        return entry;
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy