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

xapi.collect.proxy.MapOf Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.collect.proxy;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import xapi.collect.X_Collect;
import xapi.collect.api.CollectionOptions;
import xapi.collect.api.HasValues;
import xapi.collect.api.ObjectTo;

public class MapOf 
implements CollectionProxy, Map, HasValues, ObjectTo
{

  private final Class keyClass;
  private final Map map;
  private final Class valueClass;

  public MapOf(final Map map, final Class keyClass, final Class valueClass) {
    this.map = map;
    this.keyClass = keyClass;
    this.valueClass = valueClass;
  }

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

  @Override
  public ObjectTo clone(final CollectionOptions options) {
    final ObjectTo into = X_Collect.newMap(keyClass, valueClass, options);
    for (final Entry entry : map.entrySet()) {
      // do not give access to our map's entry objects;
      // this method is clone(), which implies copy and not reference sharing
      into.put(entry.getKey(), entry.getValue());
    }
    return into;
  }

  @Override
  public Class componentType() {
    return valueType();
  }

  @Override
  public boolean containsKey(final Object key) {
    return map.containsKey(key);
  }

  @Override
  public boolean containsValue(final Object value) {
    return map.containsValue(value);
  }

  @Override
  public Iterable> entries() {
    return entrySet();
  }

  @Override
  @SuppressWarnings("unchecked")
  public Entry entryFor(final Object key) {
    // Encourage inlining; EntryProxy won't get loaded if we only
    // use CollectionProxy based maps.
    if (map instanceof CollectionProxy) {
      return ((CollectionProxy)map).entryFor(key);
    }
    // Rather than iterate to get the map's actual Entry,
    // We'll just return a proxy object that fulfills the same duty.
    class EntryProxy implements Entry {

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

      @Override
      public V getValue() {
        return map.get(key);
      }

      @Override
      public V setValue(final V value) {
        return map.put((K)key, value);
      }
    }

    return new EntryProxy();
  }

  @Override
  public Set> entrySet() {
    return map.entrySet();
  }

  @Override
  public V get(final Object key) {
    return map.get(key);
  }

  @Override
  public boolean isEmpty() {
    return map.isEmpty();
  }

  @Override
  public Iterable keys() {
    return keySet();
  }

  @Override
  public Set keySet() {
    return map.keySet();
  }

  @Override
  public Class keyType() {
    return keyClass;
  }

  @Override
  public V put(final Entry item) {
    return map.put(item.getKey(), item.getValue());
  }

  @Override
  public V put(final K key, final V value) {
    return map.put(key, value);
  }

  @Override
  public void putAll(final Iterable> items) {
    for (final Entry entry : items) {
      map.put(entry.getKey(), entry.getValue());
    }
  }

  @Override
  public void putAll(final Map m) {
    map.putAll(m);
  }

  @Override
  public V remove(final Object key) {
    return map.remove(key);
  }

  @Override
  public void removeAll(final Iterable keys) {
    for (final K key : keys) {
      map.remove(key);
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public void setValue(final Object key, final Object value) {
    map.put((K)key, (V)value);
  }

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

  @Override
  @SuppressWarnings("unchecked")
  public V[] toArray() {
    final V[] values = (V[]) Array.newInstance(valueClass, map.size());
    map.values().toArray(values);
    return values;
  }

  @Override
  public Collection toCollection(Collection into) {
    if (into == null) {
      into = new ArrayList();
    }
    into.addAll(map.values());
    return into;
  }

  @Override
  public Map toMap(Map into) {
    if (into == null) {
      into = new LinkedHashMap();
    }
    into.putAll(map);
    return into;
  }

  @Override
  public Collection values() {
    return map.values();
  }

  @Override
  public Class valueType() {
    return valueClass;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy