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

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

The newest version!
/*
 * 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.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author subwiz
 */
public class MultiValueMapLinkedHashSet implements MultiValueMap {
    private final Map> map = new LinkedHashMap<>();

    @Override
    public Collection put(K key, V value){
        Set l = map.get(key);
        if(l == null){
            l = new LinkedHashSet<>();
        }
        l.add(value);
        return map.put(key, l);
    }

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

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

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

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

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

    @Override
    public boolean containsValue(V value) {
        if (map.keySet().stream().map((key) -> map.get(key)).anyMatch((values) -> (values.contains(value)))) {
            return true;
        }
        return false;
    }

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

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

    @Override
    public Collection values() {
        List values = new ArrayList<>();
        map.keySet().stream().map((key) -> map.get(key)).forEach((v) -> {
            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;
        }
        if (this.values() != other.values() && (this.values() == null || !this.values().equals(other.values()))) {
            return false;
        }
        return true;
    }

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy