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

org.jvnet.hk2.component.MultiMap Maven / Gradle / Ivy

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2007-2011 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package org.jvnet.hk2.component;

import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Map from a key to multiple values.
 * Order is significant among values, and null values are allowed, although null keys are not.
 *
 * @author Kohsuke Kawaguchi
 * @author Jerome Dochez
 */
public class MultiMap implements org.glassfish.hk2.MultiMap, Serializable, Cloneable {
    private static final long serialVersionUID = 1L;

    private final Map> store;
    private final boolean concurrencyControls;
    private final boolean modifiable;

    /**
     * Creates an empty multi-map with default concurrency controls
     */
    public MultiMap() {
        this(new LinkedHashMap>(), Habitat.CONCURRENCY_CONTROLS_DEFAULT);
    }

    /**
     * Creates an empty multi-map with option to use concurrency controls.
     * Concurrency controls applies to the inner List collection held per each key.
     * There are currently no concurrency controls around the Map portion of the data
     * structure.
     */
    MultiMap(Map> store) {
        this(store, Habitat.CONCURRENCY_CONTROLS_DEFAULT);
    }

    /**
     * Creates an empty multi-map with option to use concurrency controls
     */
    MultiMap(boolean concurrencyControls) {
        this(new LinkedHashMap>(), concurrencyControls);
    }


    @SuppressWarnings({ "rawtypes", "unchecked" })
    MultiMap readOnly() {
        return new MultiMap(Collections.unmodifiableMap(store));
    }
    
    /**
     * Creates a multi-map backed by the given store.
     *
     * @param store map to copy
     */
    protected MultiMap(Map> store, boolean concurrencyControls) {
        this.store = store;
        this.concurrencyControls = concurrencyControls;
        
        // ugly, but no other way unfortunately
        this.modifiable = !store.getClass().getName().contains("Collections$UnmodifiableMap");
    }

    /**
     * Copy constructor.
     *
     * @param base map to copy
     */
    public MultiMap(org.glassfish.hk2.MultiMap base) {
        this();
        for (Entry> e : base.entrySet()) {
            store.put(e.getKey(), newList(e.getValue()));
        }
    }
    
    @Override
    public int hashCode() {
        return store.hashCode();
    }

    @Override
    public boolean equals(Object another) {
        if (!MultiMap.class.isInstance(another)) {
            return false;
        }
        
        @SuppressWarnings("rawtypes")
        MultiMap other = MultiMap.class.cast(another);
        if (size() != other.size()) {
            return false;
        }
        
        for (Entry> entry : store.entrySet()) {
            @SuppressWarnings("unchecked")
            List vColl = other.get(entry.getKey());
            if (!entry.getValue().equals(vColl)) {
                return false;
            }
        }
        
        return true;
    }
    
    boolean matches(org.glassfish.hk2.MultiMap other) {
        if (size() > other.size()) {
            return false;
        }
        
        for (Entry> entry : store.entrySet()) {
            List vColl = other.get(entry.getKey());
            if (!entry.getValue().equals(vColl)) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        final String newline = System.getProperty("line.separator");
        builder.append("{");
        for (final K key : store.keySet()) {
            builder.append(key).append(": ");
            builder.append(store.get(key));
            builder.append(newline);
        }
        builder.append("}");
        return builder.toString();
    }

    /**
     * Creates an optionally populated list to be used as an entry in the map.
     *
     * @param initialVal
     * @return
     */
    protected List newList(Collection initialVals) {
        if (concurrencyControls) {
            if (null == initialVals) {
                return new CopyOnWriteArrayList();
            } else {
                return new CopyOnWriteArrayList(initialVals);
            }
        } else {
            if (null == initialVals) {
                return new ArrayList(1);
            } else {
                return new ArrayList(initialVals);
            }
        }
    }

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

    /**
     * Adds one more key-value pair.
     *
     * @param k key to store the entry under
     * @param v value to store in the k's values.
     */
    public final void add(K k, V v) {
        if (!modifiable) {
            throw new UnsupportedOperationException("unmodifiable collection");
        }
        
        List l = store.get(k);
        if (l == null) {
            l = newList(null);
            store.put(k, l);
        }
        l.add(v);
    }

    /**
     * Replaces all the existing values associated with the key
     * by the given value.
     *
     * @param k key for the values
     * @param v Can be null or empty.
     */
    public void set(K k, Collection v) {
        store.put(k, newList(v));
    }

    /**
     * Replaces all the existing values associated with the key
     * by the given single value.
     *
     * @param k key for the values
     * @param v singleton value for k key
     *          

* This is short for set(k,Collections.singleton(v)) */ public void set(K k, V v) { List vlist = newList(null); vlist.add(v); store.put(k, vlist); } /** * Returns the elements indexed by the provided key * * @param k key for the values * @return Can be empty but never null. Read-only. */ @Override public final List get(K k) { List l = store.get(k); if (l == null) { return Collections.emptyList(); } return l; } /** * Returns the union of all elements indexed by the provided keys. * This is a disjunctive operation. * * @param keys the collection of keys * @return the union collection of values */ public List getUnionOfAll(Collection keys) { LinkedHashSet set = new LinkedHashSet(); for (K k : keys) { set.addAll(get(k)); } return Collections.unmodifiableList(new ArrayList(set)); } /** * Returns the intersection of all elements indexed by the provided keys. * That means that all values must appear for each and every key specified. * This is a conjunctive operation. * * @param keys the collection of keys * @return the intersecting collection of values */ public List getIntersectionOfAll(Collection keys) { if (1 == keys.size()) { return get(keys.iterator().next()); } LinkedHashSet set = new LinkedHashSet(); for (K k : keys) { List vals = get(k); if (vals.isEmpty()) { return Collections.emptyList(); } if (set.isEmpty()) { // 1st iteration set.addAll(vals); } else { // >1st iteration if (1 == set.size()) { V item = set.iterator().next(); if (vals.contains(item)) { set.clear(); set.add(item); } else { return Collections.emptyList(); } } else if (1 == vals.size()) { V item = vals.iterator().next(); if (set.contains(item)) { set.clear(); set.add(item); } else { return Collections.emptyList(); } } else { Iterator iter = set.iterator(); while (iter.hasNext()) { V item = iter.next(); if (!vals.contains(item)) { iter.remove(); } if (set.isEmpty()) { return Collections.emptyList(); } } } } } if (set.isEmpty()) { return Collections.emptyList(); } return Collections.unmodifiableList(new ArrayList(set)); } public void mergeAll(org.glassfish.hk2.MultiMap another) { if (null != another) { for (Entry> entry : another.entrySet()) { List ourList = store.get(entry.getKey()); if (null == ourList) { ourList = newList(entry.getValue()); store.put(entry.getKey(), ourList); } else { for (V v : entry.getValue()) { if (!ourList.contains(v)) { ourList.add(v); } } } } } } /** * Package private (for getting the raw map for direct manipulation by the habitat) * * @param k the key * @return */ final List _get(K k) { List l = store.get(k); if (l == null) { return Collections.emptyList(); } return l; } /** * Checks if the map contains the given key. * * @param k key to test * @return true if the map contains at least one element for this key */ @Override public boolean containsKey(K k) { return !get(k).isEmpty(); } /** * Checks if the map contains the given key(s), also extending the search * to including the sub collection. * * @param k1 key from top collection * @param k2 key (value) from inner collection * @return true if the map contains at least one element for these keys */ @Override public boolean contains(K k1, V k2) { List list = _get(k1); return list.contains(k2); } /** * Removes an key value from the map * * @param key key to be removed * @return the value stored under this key or null if there was none */ public List remove(K key) { return store.remove(key); } /** * Removes an key value pair from the map * * @param key key to be removed * @param entry the entry to be removed from the key'ed list * @return true if there was none that was deleted */ public boolean remove(K key, V entry) { List list = store.get(key); return (null == list) ? false : list.remove(entry); } /** * Gets the first value if any, or null. *

* This is useful when you know the given key only has one value and you'd like * to get to that value. * * @param k key for the values * @return null if the key has no values or it has a value but the value is null. */ public V getOne(K k) { return getFirst(k); } @Override public V getFirst(K k) { List lst = store.get(k); if (null == lst) { return null; } if (lst.isEmpty()) { return null; } return lst.get(0); } /** * Lists up all entries. * * @return a {@link java.util.Set} of {@link java.util.Map.Entry} of entries */ @Override public Set>> entrySet() { return store.entrySet(); } /** * @return the map as "key=value1,key=value2,...." */ public String toCommaSeparatedString() { StringBuilder buf = new StringBuilder(); for (Entry> e : entrySet()) { for (V v : e.getValue()) { if (buf.length() > 0) { buf.append(','); } buf.append(e.getKey()).append('=').append(v); } } return buf.toString(); } /** * Creates a copy of the map that contains the exact same key and value set. * Keys and values won't cloned. */ @Override public MultiMap clone() { return new MultiMap(this); } /** * Returns the size of the map * * @return integer or 0 if the map is empty */ @Override public int size() { return store.size(); } @SuppressWarnings({"rawtypes", "unchecked"}) private static final MultiMap EMPTY = new MultiMap(Collections.emptyMap()); /** * Gets the singleton read-only empty multi-map. * * @return an empty map * @see Collections#emptyMap() */ @SuppressWarnings("unchecked") public static MultiMap emptyMap() { return EMPTY; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy