Skip to content

Class MultiMap

Ori Roth edited this page Apr 2, 2017 · 3 revisions

Synopsis of Class MultiMap<K, V>

public final class MultiMap<K, V> implements Iterable<K> { 
    /*
     * Forge (3)
     */
        MultiMap(); 
        MultiMap(int initialCapacity); 
        MultiMap(int initialCapacity, float loadFactor); 
    /*
     * Type (7)
     */
        String toString(); 
        Set<V> values(); 
        void put(K k, V v); 
        Set<V> get(K k); 
        Set<V> clear(K k); 
        Iterator<K> iterator(); 
        int size(); 
} 

Output types: Iterator<K>, Set<V>.

Code

// SSDLPedia
package il.ac.technion.cs.ssdl.collections;

import il.ac.technion.cs.ssdl.stereotypes.Canopy;

import java.util.*;

/**
 * A 1-to-many mapping, that is: a relation where each source value is
 * associated with several "images". This class is in fact a façade for
 * an instance of the JRE's Map in which the mapping range is a
 * Set.
 * 
 * Author: Itay Maman <[email protected]>
 * <K> Type of source values
 * <V> Type of images
 */
@Canopy public final class MultiMap<K, V> implements Iterable<K> {
    /**
     * Create a new empty MultiMap
     */
    public MultiMap() {
        implementation = new HashMap<K, Set<V>>();
    }
    
    /**
     * Create a new empty MultiMap with a given capacity
     * 
     * initialCapacity initial capacity of the map
     */
    public MultiMap(final int initialCapacity) {
        this(initialCapacity, 0.75f);
    }
    
    /**
     * Create a new empty MultiMap with a given capacity and load factor
     * 
     * initialCapacity initial capacity of the map
     * loadFactor recreate map if it is filled to this extent
     */
    public MultiMap(final int initialCapacity, final float loadFactor) {
        implementation = new HashMap<K, Set<V>>(initialCapacity, loadFactor);
    }
    
    @Override public String toString() {
        final StringBuilder $ = new StringBuilder();
        for (final K k : this)
            $.append(k + "=>" + get(k) + '\n');
        return $.toString();
    }
    
    /**
     * Obtain all images
     * 
     * Return: Set of V objects
     */
    public Set<V> values() {
        final Set<V> $ = new HashSet<V>();
        for (final Set<V> curr : implementation.values())
            $.addAll(curr);
        return $;
    }
    
    /**
     * Add an image to the given source value
     * 
     * k Source value
     * v Image value
     */
    public void put(final K k, final V v) {
        get(k).add(v);
    }
    
    /**
     * Find the set of all images of the given source value. If this key does
     * not exist yet, add it with an empty set.
     * 
     * k key value
     * Return: A non-null representing the set of images
     *         associated with k
     */
    public Set<V> get(final K k) {
        final Set<V> $ = implementation.get(k);
        return $ != null ? $ : clear(k);
    }
    
    /**
     * Clear the set of all images of the given source value
     * 
     * k Source value
     * Return: the newly created set object
     */
    public Set<V> clear(final K k) {
        final Set<V> $ = new HashSet<V>();
        implementation.put(k, $);
        return $;
    }
    
    /**
     * Return an iterator over all keys
     * 
     * Return: Iterator<K> object
     */
    public Iterator<K> iterator() {
        return implementation.keySet().iterator();
    }
    
    /**
     * How many keys are stored in this map?
     * 
     * Return: number of keys
     */
    public int size() {
        return implementation.size();
    }
    
    /**
     * Actual implementation
     */
    private final HashMap<K, Set<V>> implementation;
}

Metrics

Metric Value Acronym Explanation
LOC 122 Lines Of Code Total number of lines in the code
SCC 21 SemiColons Count Total number of semicolon tokens found in the code.
NOT 366 Number Of Tokens Comments, whitespace and text which cannot be made into a token not included.
VCC 2294 Visible Characters Count The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC 1053 Code Characters Count Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC 38 Unique Identifiers Count The number of different identifiers found in the code
WHC 3 Weighted Horizontal Complexity A heuritistic on horizontal complexity

Statistics

Statistic Value
Average token length 2.9
Tokens/line 3
Visible characters/line 19
Code characters/line 8.6
Semicolons/tokens 5%
Comment text percentage 54%

Tokens by kind

Token Kind Occurrences
KEYWORD 53
OPERATOR 45
LITERAL 3
ID 125
PUNCTUATION 140
COMMENT 12
OTHER 173
Clone this wiki locally