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

fatjar.implementations.cache.MapCache Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package fatjar.implementations.cache;

import fatjar.Cache;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class MapCache implements Cache {

    private final String name;
    private static Map map = new ConcurrentHashMap<>();

    public MapCache(String name) {
        this.name = name;
        map.putIfAbsent(name, new HashMap());
    }

    @SuppressWarnings("unchecked")
    private Map cache() {
        return map.get(this.name);
    }

    @Override
    public Type getType() {
        return Type.Map;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public V get(K key) {
        return cache().get(key);
    }

    @Override
    public Map getAll() {
        Map map = new HashMap<>();
        cache().keySet()
                .stream()
                .forEach(key -> {
                    map.put(key, cache().get(key));
                });
        return map;
    }

    @Override
    public Map getAll(Set keys) {
        Map map = new HashMap<>();
        cache().keySet()
                .stream()
                .filter(keys::contains)
                .forEach(key -> {
                    map.put(key, cache().get(key));
                });
        return map;
    }

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

    @Override
    public void put(K key, V value) {
        cache().put(key, value);
    }

    @Override
    public void putAll(Map map) {
        cache().putAll(map);
    }

    @Override
    public V putIfAbsent(K key, V value) {
        return cache().putIfAbsent(key, value);
    }

    @Override
    public V remove(K key) {
        return cache().remove(key);
    }

    @Override
    public boolean replace(K key, V oldValue, V newValue) {
        return cache().replace(key, oldValue, newValue);
    }

    @Override
    public void removeAll(Set keys) {
        cache().keySet()
                .stream()
                .filter(keys::contains)
                .forEach(key -> {
                    cache().remove(key);
                });
    }

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


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy