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

se.kth.iss.ug2.MultivalueMap Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
/*
 * MIT License
 *
 * Copyright (c) 2017 Kungliga Tekniska högskolan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package se.kth.iss.ug2;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MultivalueMap, V> {
    private Map> map;

    public class NaturalAndNullComparator implements Comparator {

        @Override
        public int compare(K o1, K o2) {
            if (o1 == null && o2 == null) {
                return 0;
            }
            if (o1 == null) {
                return 1;
            }
            if (o2 == null) {
                return -1;
            }
            return o1.compareTo(o2);
        }

    }

    public MultivalueMap() {
        map = new TreeMap<>(new NaturalAndNullComparator());
    }

    public void clear() {
        map.clear();
    }

    public boolean containsKey(K key) {
        return map.containsKey(key);
    }

    public boolean containsValue(V value) {
        for (List element : map.values()) {
            if (element.contains(value)) {
                return true;
            }
        }

        return false;
    }

    public List get(K key) {
        if (map.containsKey(key)) {
            return map.get(key);
        }

        return new LinkedList<>();
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }

    public Set keySet() {
        return map.keySet();
    }

    public void put(K key, V value) {
        if (!map.containsKey(key)) {
            map.put(key, new LinkedList<>());
        }

        map.get(key).add(value);
    }

    public void putAll(K key, Collection value) {
        if (!map.containsKey(key)) {
            map.put(key, new LinkedList<>());
        }

        map.get(key).addAll(value);
    }

    public List remove(K key) {
        return map.remove(key);
    }

    public void removeFirst(K key) {
        if (containsKey(key)) {
            get(key).remove(0);
        }
    }

    public int size() {
        throw new UnsupportedOperationException();
    }

    public Collection values() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return map.toString();
    }

    public Map> asMap() {
        return Collections.unmodifiableMap(map);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy