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

se.ugli.java.util.juc.ImmutableMapImpl Maven / Gradle / Ivy

The newest version!
package se.ugli.java.util.juc;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;
import static se.ugli.java.util.stream.Collectors.toImmutableList;
import static se.ugli.java.util.stream.Collectors.toImmutableSet;

import java.util.Map;
import java.util.Optional;

import se.ugli.java.util.ImmutableCollection;
import se.ugli.java.util.ImmutableMap;
import se.ugli.java.util.ImmutableSet;
import se.ugli.java.util.MapEntry;

public class ImmutableMapImpl extends ImmutableSetImpl> implements ImmutableMap {

    private static final long serialVersionUID = -6330686482444969304L;
    private final Map map;

    public ImmutableMapImpl(final Map map) {
        super(map.entrySet().stream().map(MapEntry::apply).collect(toSet()));
        this.map = map;
    }

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

    @Override
    public boolean containsValue(final V value) {
        return map.containsValue(value);
    }

    @Override
    public Optional get(final K key) {
        return Optional.ofNullable(map.get(key));
    }

    @Override
    public ImmutableSet keySet() {
        return map.keySet().stream().collect(toImmutableSet());
    }

    @Override
    public ImmutableCollection values() {
        return map.values().stream().collect(toImmutableList());
    }

    @Override
    public String toString() {
        return "Map(" + stream().map(Object::toString).collect(joining(",")) + ")";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy