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

ru.progrm_jarvis.javacommons.object.valuestorage.SimpleValueStorage Maven / Gradle / Ivy

package ru.progrm_jarvis.javacommons.object.valuestorage;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import org.jetbrains.annotations.NotNull;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Simple {@link ValueStorage value storage} based on {@link AbstractValueStorage}
 * using {@link String strings} as its keys.
 *
 * @param  type of stored values
 */
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public final class SimpleValueStorage extends AbstractValueStorage<@NotNull String, V> {

    /**
     * Counter used for generating new keys
     */
    @NotNull AtomicReference<@NotNull BigInteger> counter;

    private SimpleValueStorage(final @NotNull Map<@NotNull String, V> values,
                               final @NotNull AtomicReference<@NotNull BigInteger> counter) {
        super(values);
        this.counter = counter;
    }

    /**
     * Creates a new simple value storage.
     *
     * @param  type of stored values
     * @return created simple value storage
     */
    public static  @NotNull ValueStorage<@NotNull String, V> create() {
        return new SimpleValueStorage<>(new ConcurrentHashMap<>(), new AtomicReference<>(BigInteger.ZERO));
    }

    @Override
    protected String generateNewKey() {
        return counter.updateAndGet(value -> value.add(BigInteger.ONE)).toString(Character.MAX_RADIX);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy