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

org.aksw.commons.collections.reversible.ReversibleMapImpl Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.collections.reversible;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ForwardingSet;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;


public class ReversibleMapImpl
    extends AbstractMap
    implements ReversibleMap
{
    protected ReversibleSetMultimap reverse;

    protected Map forward;
    protected SetMultimap backward;

    public ReversibleMapImpl() {
        this(new HashMap<>(), HashMultimap.create());
    }

   public ReversibleMapImpl(Map forward, SetMultimap backward) {
        this.forward = forward;
        this.backward = backward;

        this.reverse = new ReversibleSetMultimapImpl<>(this, this.backward);
    }


    @Override
    public V put(K key, V value) {
        V result = forward.get(key);
        backward.remove(result, key);

        forward.put(key, value);
        backward.put(value, key);

        return result;
    }

    @Override
    public V remove(Object key) {
        V result = forward.remove(key);
        backward.remove(result, key);
        return result;
    }

    @Override
    public void clear() {
        forward.clear();
        backward.clear();
    }

    @Override
    public V get(Object key) {
        return forward.get(key);
    }

    @Override
    public boolean containsKey(Object key) {
        return forward.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return backward.containsKey(value);
    }

    @Override
    public ReversibleSetMultimap reverse() {
        return reverse;
    }

    @Override
    public Set> entrySet() {
        return forward.entrySet();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy