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

org.testng.collections.MultiMap Maven / Gradle / Ivy

package org.testng.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class MultiMap> {
  protected final Map m_objects;

  protected MultiMap(boolean isSorted) {
    if (isSorted) {
      m_objects = Maps.newLinkedHashMap();
    } else {
      m_objects = Maps.newHashMap();
    }
  }

  protected abstract C createValue();

  public boolean put(K key, V method) {
    boolean setExists = true;
    C l = m_objects.get(key);
    if (l == null) {
      setExists = false;
      l = createValue();
      m_objects.put(key, l);
    }
    return l.add(method) && setExists;
  }

  public C get(K key) {
    C list = m_objects.get(key);
    if (list == null) {
      list = createValue();
      m_objects.put(key, list);
    }
    return list;
  }

  @Deprecated
  public List getKeys() {
    return new ArrayList<>(keySet());
  }

  public Set keySet() {
    return new HashSet<>(m_objects.keySet());
  }

  public boolean containsKey(K k) {
    return m_objects.containsKey(k);
  }

  @Override
  public String toString() {
    StringBuilder result = new StringBuilder();
    Set indices = keySet();
    for (K i : indices) {
      result.append("\n    ").append(i).append(" <-- ");
      for (Object o : m_objects.get(i)) {
        result.append(o).append(" ");
      }
    }
    return result.toString();
  }

  public boolean isEmpty() {
    return m_objects.size() == 0;
  }

  @Deprecated
  public int getSize() {
    return size();
  }

  public int size() {
    return m_objects.size();
  }

  @Deprecated
  public C remove(K key) {
    return removeAll(key);
  }

  public boolean remove(K key, V value) {
    return get(key).remove(value);
  }

  public C removeAll(K key) {
    return m_objects.remove(key);
  }

  @Deprecated
  public Set> getEntrySet() {
    return entrySet();
  }

  public Set> entrySet() {
    return m_objects.entrySet();
  }

  @Deprecated
  public Collection getValues() {
    return values();
  }

  public Collection values() {
    return m_objects.values();
  }

  public boolean putAll(K k, Collection values) {
    boolean result = false;
    for (V v : values) {
      result = put(k, v) || result;
    }
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy