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

org.pitest.sequence.Context1 Maven / Gradle / Ivy

There is a newer version: 1.17.1
Show newest version
package org.pitest.sequence;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Specialisation of context for single values
 */
final class Context1 implements Context {
    private final boolean debug;
    private final Slot slot;
    private final Object value;

    Context1(Slot slot, Object value, boolean debug) {
        this.slot = slot;
        this.value = value;
        this.debug = debug;
    }

    @Override
    public boolean debug() {
        return debug;
    }

    @Override
    public  Context store(SlotWrite slot, S value) {
        Map mutatedSlots = new IdentityHashMap<>();
        mutatedSlots.put(this.slot, this.value);
        mutatedSlots.put(slot.slot(), value);
        return new MultiContext(mutatedSlots, debug);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Optional retrieve(SlotRead read) {
        if (read.slot().equals(slot)) {
            return (Optional) Optional.ofNullable(value);
        }
        return Optional.empty();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Context1)) {
            return false;
        }
        Context1 context1 = (Context1) o;
        return slot.equals(context1.slot) && Objects.equals(value, context1.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(slot, value);
    }
}