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

org.sejda.common.collection.ListValueMap Maven / Gradle / Ivy

There is a newer version: 5.1.7
Show newest version
/*
 * Created on 18/apr/2010
 *
 * Copyright 2010 by Andrea Vacondio ([email protected]).
 * 
 * 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 org.sejda.common.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Map where the elements added to the same key are enqueued in a List.
 * 
 * @author Andrea Vacondio
 * 
 * @param 
 *            key generic type
 * @param 
 *            value generic type
 */
public final class ListValueMap {

    private Map> map;

    public ListValueMap() {
        map = new HashMap>();
    }

    /**
     * Removes all the elements form the map
     */
    public void clear() {
        map.clear();
    }

    /**
     * Adds the input value to the {@link List} associated to the input key
     * 
     * @param key
     * @param value
     * @return the List with the input value added
     */
    public List put(K key, V value) {
        List list = map.get(key);
        if (list == null) {
            list = new ArrayList();
        }
        list.add(value);
        return map.put(key, list);
    }

    /**
     * Removes the input value from the {@link List} associated to the input key.
     * 
     * @param key
     * @param value
     * @return true if the value was found and removed.
     */
    public boolean remove(K key, V value) {
        List list = map.get(key);
        if (list != null && !list.isEmpty()) {
            return list.remove(value);
        }
        return false;
    }

    /**
     * A null safe getter for the given key.
     * 
     * @param key
     * @return the list associated to the input key or an empty list of nothing is associated.
     */
    public List get(K key) {
        List list = map.get(key);
        if (list != null) {
            return list;
        }
        return Collections.emptyList();
    }

    /**
     * @return number of values stored by this map
     */
    public int size() {
        int retVal = 0;
        for (Entry> entry : map.entrySet()) {
            retVal += entry.getValue().size();
        }
        return retVal;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy