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

org.wiztools.commons.MultiValueMapArrayList Maven / Gradle / Ivy

/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */
package org.wiztools.commons;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * This class does not implement java.util.Map<K, V> interface.
 * This class encapsulates a HashMap holding a List of values associated with a
 * particular key.
 * @author subwiz
 */
public class MultiValueMapArrayList implements MultiValueMap{

    private Map> map = new LinkedHashMap>();

    public Collection put(K key, V value){
        List l = map.get(key);
        if(l == null){
            l = new ArrayList();
        }
        l.add(value);
        return map.put(key, l);
    }

    public Collection get(K key){
        return map.get(key);
    }

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

    public int size() {
        return map.size();
    }

    public boolean isEmpty() {
        if(size() == 0)
            return true;
        return false;
    }

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

    public boolean containsValue(V value) {
        for(K key: map.keySet()){
            List values = map.get(key);
            if(values.contains(value)){
                return true;
            }
        }
        return false;
    }

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

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

    public Collection values() {
        List values = new ArrayList();
        for(K key: map.keySet()){
            List v = map.get(key);
            values.addAll(v);
        }
        return values;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof MultiValueMap)) {
            return false;
        }
        final MultiValueMap other = (MultiValueMap) obj;
        
        if (this.keySet() != other.keySet() && (this.keySet() == null || !this.keySet().equals(other.keySet()))) {
            return false;
        }
        final Collection thisValues = this.values();
        final Collection otherValues = other.values();
        if (thisValues != otherValues && (thisValues == null || !thisValues.equals(otherValues))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.map != null ? this.map.hashCode() : 0);
        return hash;
    }

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy