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

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

Go to download

Vaadin is a web application framework for Rich Internet Applications (RIA). Vaadin enables easy development and maintenance of fast and secure rich web applications with a stunning look and feel and a wide browser support. It features a server-side architecture with the majority of the logic running on the server. Ajax technology is used at the browser-side to ensure a rich and interactive user experience.

There is a newer version: 8.27.1
Show newest version
/*
 * Copyright 2008 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 com.google.gwt.core.shared.impl.InternalPreconditions.checkArgument;
import static com.google.gwt.core.shared.impl.InternalPreconditions.checkState;

import com.google.gwt.lang.Array;

/**
 * A {@link java.util.Map} of {@link Enum}s. [Sun
 * docs]
 * 
 * @param  key type
 * @param  value type
 */
public class EnumMap, V> extends AbstractMap {

  private final class EntrySet extends AbstractSet> {

    @Override
    public void clear() {
      EnumMap.this.clear();
    }

    @Override
    public boolean contains(Object o) {
      if (o instanceof Map.Entry) {
        return containsEntry((Map.Entry) o);
      }
      return false;
    }

    @Override
    public Iterator> iterator() {
      return new EntrySetIterator();
    }

    @Override
    public boolean remove(Object entry) {
      if (contains(entry)) {
        Object key = ((Map.Entry) entry).getKey();
        EnumMap.this.remove(key);
        return true;
      }
      return false;
    }

    public int size() {
      return EnumMap.this.size();
    }
  }

  private final class EntrySetIterator implements Iterator> {
    private Iterator it = keySet.iterator();
    private K key;

    public boolean hasNext() {
      return it.hasNext();
    }

    public Entry next() {
      key = it.next();
      return new MapEntry(key);
    }

    public void remove() {
      checkState(key != null);

      EnumMap.this.remove(key);
      key = null;
    }
  }

  private class MapEntry extends AbstractMapEntry {

    private final K key;

    public MapEntry(K key) {
      this.key = key;
    }

    public K getKey() {
      return key;
    }

    public V getValue() {
      return values[key.ordinal()];
    }

    public V setValue(V value) {
      return set(key.ordinal(), value);
    }
  }

  private EnumSet keySet;

  private V[] values;

  public EnumMap(Class type) {
    init(type);
  }

  public EnumMap(EnumMap m) {
    init(m);
  }

  public EnumMap(Map m) {
    if (m instanceof EnumMap) {
      init((EnumMap) m);
    } else {
      checkArgument(!m.isEmpty(), "Specified map is empty");
      init(m.keySet().iterator().next().getDeclaringClass());
      putAll(m);
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public void clear() {
    keySet.clear();
    values = (V[]) new Object[values.length];
  }

  public EnumMap clone() {
    return new EnumMap(this);
  }

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

  @Override
  public boolean containsValue(Object value) {
    for (K key : keySet) {
      if (Objects.equals(value, values[key.ordinal()])) {
        return true;
      }
    }
    return false;
  }

  @Override
  public Set> entrySet() {
    return new EntrySet();
  }

  @Override
  public V get(Object k) {
    return keySet.contains(k) ? values[asOrdinal(k)] : null;
  }

  @Override
  public V put(K key, V value) {
    keySet.add(key);
    return set(key.ordinal(), value);
  }

  @Override
  public V remove(Object key) {
    return keySet.remove(key) ? set(asOrdinal(key), null) : null;
  }

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

  /**
   * Returns key as K. Only runtime checks that
   * key is an Enum, not that it's the particular Enum K. Should only be called
   * when you are sure key is of type K.
   */
  @SuppressWarnings("unchecked")
  private K asKey(Object key) {
    return (K) key;
  }

  private int asOrdinal(Object key) {
    return asKey(key).ordinal();
  }

  @SuppressWarnings("unchecked")
  private void init(Class type) {
    keySet = EnumSet.noneOf(type);
    values = (V[]) new Object[keySet.capacity()];
  }

  private void init(EnumMap m) {
    keySet = m.keySet.clone();
    values = Array.clone(m.values);
  }

  private V set(int ordinal, V value) {
    V was = values[ordinal];
    values[ordinal] = value;
    return was;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy