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

com.ibm.wala.util.collections.MapUtil Maven / Gradle / Ivy

There is a newer version: 1.6.6
Show newest version
/*
 * Copyright (c) 2002 - 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 */
package com.ibm.wala.util.collections;

import com.ibm.wala.util.intset.MutableIntSet;
import com.ibm.wala.util.intset.MutableSparseIntSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/** utilities for managing {@link Map}s */
public class MapUtil {
  /**
   * @param M a mapping from Object -> Set
   * @return the Set corresponding to key in M; create one if needed
   * @throws IllegalArgumentException if M is null
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  Set findOrCreateSet(Map> M, K key) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    Set result = M.get(key);
    if (result == null) {
      result = HashSetFactory.make(2);
      M.put(key, result);
    }
    return result;
  }

  /**
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  MutableIntSet findOrCreateMutableIntSet(Map M, K key) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    MutableIntSet mis = M.get(key);
    if (mis == null) {
      mis = MutableSparseIntSet.makeEmpty();
      M.put(key, mis);
    }
    return mis;
  }

  /**
   * @return the Collection corresponding to key in M; create one if needed
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  Collection findOrCreateCollection(Map> M, K key) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    Collection result = M.get(key);
    if (result == null) {
      result = HashSetFactory.make(2);
      M.put(key, result);
    }
    return result;
  }

  /**
   * @return the Set corresponding to key in M; create one if needed
   * @throws IllegalArgumentException if M is null
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  List findOrCreateList(Map> M, K key) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    if (!M.containsKey(key)) {
      M.put(key, new ArrayList<>());
    }
    return M.get(key);
  }

  /**
   * @param M a mapping from Object -> Map
   * @return the Map corresponding to key in M; create one if needed
   * @throws IllegalArgumentException if M is null
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  Map findOrCreateMap(Map> M, K key) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    Map result = M.get(key);
    if (result == null) {
      result = HashMapFactory.make(2);
      M.put(key, result);
    }
    return result;
  }

  /**
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  V findOrCreateValue(Map M, K key, Factory factory) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    V result = M.get(key);
    if (result == null) {
      result = factory.make();
      M.put(key, result);
    }
    return result;
  }

  /**
   * @param M a mapping from Object -> WeakHashMap
   * @return the WeakHashMap corresponding to key in M; create one if needed
   * @throws IllegalArgumentException if M is null
   * @throws ClassCastException if the key is of an inappropriate type for this map (optional)
   * @throws NullPointerException if the specified key is null and this map does not permit null
   *     keys (optional)
   */
  public static  WeakHashMap findOrCreateWeakHashMap(
      Map> M, Object key) {
    if (M == null) {
      throw new IllegalArgumentException("M is null");
    }
    WeakHashMap result = M.computeIfAbsent(key, k -> new WeakHashMap<>(2));
    return result;
  }

  /**
   * @param m a map from key -> {@link Set}<value>
   * @return inverted map, value -> {@link Set}<key>
   * @throws IllegalArgumentException if m is null
   */
  public static  Map> inverseMap(Map> m) {
    if (m == null) {
      throw new IllegalArgumentException("m is null");
    }
    Map> result = HashMapFactory.make(m.size());
    for (Map.Entry> E : m.entrySet()) {
      K key = E.getKey();
      Set values = E.getValue();
      for (V v : values) {
        Set s = findOrCreateSet(result, v);
        s.add(key);
      }
    }
    return result;
  }

  /**
   * invert an input map that is one-to-one (i.e., it does not map two different keys to the same
   * value)
   *
   * @throws IllegalArgumentException if m is null
   * @throws IllegalArgumentException if m is not one-to-one
   */
  public static  Map invertOneToOneMap(Map m) {
    if (m == null) {
      throw new IllegalArgumentException("m is null");
    }
    Map result = HashMapFactory.make(m.size());
    for (Map.Entry entry : m.entrySet()) {
      K key = entry.getKey();
      V val = entry.getValue();
      if (result.containsKey(val)) {
        throw new IllegalArgumentException("input map not one-to-one");
      }
      result.put(val, key);
    }
    return result;
  }

  public static  Map, V> groupKeysByValue(Map m) {
    if (m == null) {
      throw new IllegalArgumentException("m is null");
    }
    Map, V> result = HashMapFactory.make();
    Map> valueToKeys = HashMapFactory.make();
    for (Map.Entry E : m.entrySet()) {
      K key = E.getKey();
      V value = E.getValue();
      findOrCreateSet(valueToKeys, value).add(key);
    }
    for (Map.Entry> E : valueToKeys.entrySet()) {
      V value = E.getKey();
      Set keys = E.getValue();
      result.put(keys, value);
    }
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy