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

uk.gov.gchq.gaffer.mapstore.multimap.MultiMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017-2024 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.gov.gchq.gaffer.mapstore.multimap;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * Map-like data structure where keys may correspond to multiple values.
 *
 * @param  the type of key
 * @param  the type of value
 */
public interface MultiMap {

    /**
     * Add a value to the specified key.
     *
     * @param key the key to add a value to
     * @param value the value to add
     * @return true if the operation was successful, otherwise false
     */
    boolean put(final K key, final V value);

    /**
     * Add a collection of values to the specified key.
     *
     * @param key the key to add the values to
     * @param values the values to add
     */
    void put(final K key, final Collection values);

    /**
     * Remove value for the specified key.
     *
     * @param key the key for the value to be removed
     * @param value the value to be removed
     * @return true if the operation was successful, otherwise false
     */
    boolean remove(final K key, final V value);

    /**
     * Get all of the values associated with the specified key.
     *
     * @param key the key to lookup
     * @return a collection containing all of the values
     */
    Collection get(final K key);

    /**
     * Get the Set containing all keys in the map.
     *
     * @return the set of keys
     */
    Set keySet();

    /**
     * Clear the map of all entries.
     */
    void clear();

    /**
     * Add all of the contents of another {@link MultiMap} to this instance.
     *
     * @param map the map containing the entries to add to this map instance
     */
    default void putAll(final MultiMap map) {
        if (map instanceof MapOfSets) {
            for (final Map.Entry> entry : ((MapOfSets) map).entrySet()) {
                put(entry.getKey(), entry.getValue());
            }
        } else {
            for (final K key : map.keySet()) {
                put(key, map.get(key));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy