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

ru.mk.pump.web.utils.TestVars Maven / Gradle / Ivy

package ru.mk.pump.web.utils;

import static java.lang.String.format;

import com.google.common.collect.Maps;
import lombok.NonNull;
import lombok.ToString;
import org.apache.commons.lang3.tuple.Pair;
import ru.mk.pump.commons.interfaces.PrettyPrinter;
import ru.mk.pump.commons.listener.AbstractNotifier;
import ru.mk.pump.commons.listener.Event;
import ru.mk.pump.commons.listener.Listener;
import ru.mk.pump.commons.reporter.Reporter;
import ru.mk.pump.commons.utils.Strings;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
@ToString
public final class TestVars
        extends AbstractNotifier, TestVars.TestVarListener.TestVarEvent, TestVars.TestVarListener>
        implements PrettyPrinter {

    @SuppressWarnings("WeakerAccess")
    public final static String RESULT = "result";
    public final static String NULL = "null";
    public final static String NULL_STRING = "null_string";
    public final static String EMPTY_STRING = "empty";
    @SuppressWarnings("WeakerAccess")
    public final static String LAST_PUT = "last";

    private final Map sourceMap;

    private final Map sourceStringMap;

    private TestVars(final Map sourceMap, Reporter reporter) {
        /*HashMap because concurrent map not supplies null value*/
        this.sourceMap = new HashMap<>(sourceMap);
        this.sourceStringMap = new HashMap<>();
        this.sourceMap.put(NULL, null);
        this.sourceMap.put(NULL_STRING, "null");
        this.sourceMap.put(EMPTY_STRING, "");
        this.sourceStringMap.putAll(toStringMap());
        addListener(item -> {
            if (reporter != null && !sourceMap.containsKey(item.getKey())) {
                reporter.info(format("Add to global variables with key '%s'", item.getKey()),
                        format("key = %s%n%nvalue = %s", item.getKey(), item.getValue()),
                        reporter.attachments().dummy());
            }
        });
    }

    private TestVars(Map sourceMap) {
        this(sourceMap, null);
    }

    private TestVars() {
        this(Maps.newHashMap());
    }

    @NonNull
    public static TestVars of(@NonNull String key, @Nullable Object value) {
        return new TestVars().put(key, value);
    }

    @NonNull
    public static TestVars of(@NonNull Map sourceMap) {
        return new TestVars(sourceMap);
    }

    @NonNull
    public static TestVars of(@NonNull Map sourceMap, @NonNull Reporter reporter) {
        return new TestVars(sourceMap, reporter);
    }

    @NonNull
    public static TestVars of() {
        return new TestVars();
    }

    @NonNull
    public TestVars put(@NonNull String key, @Nullable Object value) {
        sourceMap.put(key, value);
        sourceMap.put(LAST_PUT, value);
        sourceStringMap.put(key, Strings.toString(value));
        notifyOnPut(Pair.of(key, value));
        return this;
    }

    @NonNull
    public TestVars putResult(@Nullable Object value) {
        put(RESULT, value);
        return this;
    }

    @Nullable
    public Object getResult() {
        return sourceMap.get(RESULT);
    }

    @Nullable
    public Object getLastPut() {
        return sourceMap.get(LAST_PUT);
    }

    @Nullable
    public Object get(@NonNull String key) {
        return sourceMap.get(key);
    }

    @Nullable
    public Object get(@NonNull String key, @Nullable String defaultValue) {
        return sourceMap.getOrDefault(key, defaultValue);
    }

    public Map getStringMap() {
        return Collections.unmodifiableMap(sourceStringMap);
    }

    public boolean has(@NonNull String key) {
        return sourceMap.containsKey(key);
    }

    @NonNull
    public Map asMap() {
        return Collections.unmodifiableMap(sourceMap);
    }

    @Override
    public String toPrettyString() {
        return Strings.toPrettyString(sourceMap);
    }

    protected void notifyOnPut(Pair object) {
        notify(event(object, TestVars.TestVarListener.TestVarEvent.PUT_VAR));
    }

    private Map toStringMap() {
        return sourceMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, i -> Strings.toString(i.getValue())));
    }

    public interface TestVarListener extends Listener, TestVarListener.TestVarEvent> {

        @Override
        default void on(Event, TestVarListener.TestVarEvent> event, Object... args) {
            switch (event.name()) {
                case PUT_VAR:
                    onPut(event.get());
                    break;
            }
        }

        void onPut(Pair item);

        enum TestVarEvent {
            PUT_VAR
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy