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

com.vladsch.flexmark.experimental.util.collection.KeyedItemFactoryMap Maven / Gradle / Ivy

Go to download

Contains experimental classes that may or may not work in all cases. Use at your own risk.

The newest version!
package com.vladsch.flexmark.experimental.util.collection;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

public class KeyedItemFactoryMap implements Map> {
    protected final @NotNull HashMap> factoryMap;
    protected final @NotNull HashMap itemMap;
    protected final @NotNull P param;

    public KeyedItemFactoryMap(@NotNull P param) {
        this(param, 0);
    }

    public KeyedItemFactoryMap(@NotNull P param, int capacity) {
        this.factoryMap = new HashMap<>(capacity);
        this.itemMap = new HashMap<>();
        this.param = param;
    }

    public @Nullable I getItem(@NotNull K key) {
        I item = itemMap.get(key);

        if (item == null) {
            Function factory = factoryMap.get(key);
            if (factory == null) {
                throw new IllegalStateException("Factory for key: " + key + " is not defined");
            }
            item = factory.apply(param);
            itemMap.put(key, item);
        }

        return item;
    }

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

    @Override
    public boolean isEmpty() {return factoryMap.isEmpty();}

    @Override
    public @Nullable Function get(@Nullable Object o) {return factoryMap.get(o);}

    @Override
    public boolean containsKey(@Nullable Object o) {return factoryMap.containsKey(o);}

    @Override
    public @Nullable Function put(@NotNull K k, @NotNull Function factory) {return factoryMap.put(k, factory);}

    @Override
    public void putAll(@NotNull Map> map) {factoryMap.putAll(map);}

    @Override
    public @Nullable Function remove(@Nullable Object o) {return factoryMap.remove(o);}

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

    @Override
    public boolean containsValue(Object o) {return factoryMap.containsValue(o);}

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

    @NotNull
    @Override
    public Collection> values() {return factoryMap.values();}

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy