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

com.opencredo.concursus.domain.events.indexing.InMemoryIndex Maven / Gradle / Ivy

The newest version!
package com.opencredo.concursus.domain.events.indexing;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

final class InMemoryIndex {

    public static  InMemoryIndex create() {
        return create(new ConcurrentHashMap<>());
    }

    public static  InMemoryIndex create(Map> indexData) {
        return new InMemoryIndex<>(indexData);
    }

    private final Map> indexData;

    private InMemoryIndex(Map> indexData) {
        this.indexData = indexData;
    }

    public void add(K key, V value) {
        indexData.compute(key, (k, v) -> {
            Set values = v == null ? new HashSet<>() : v;
            values.add(value);
            return values;
        });
    }

    public void remove(K key, V value) {
        indexData.compute(key, (k, v) -> {
            if (v == null) {
                return null;
            }
            v.remove(value);
            return v.isEmpty() ? null : v;
        });
    }

    public Set get(K key) {
        return indexData.getOrDefault(key, Collections.emptySet());
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy