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

org.pitest.sequence.MultiContext 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.Optional;

/**
 * Specialisation of context for unlimited values
 */
final class MultiContext implements Context {

    private final boolean debug;
    private final Map slots;

    MultiContext(Map slots, boolean debug) {
        this.slots = slots;
        this.debug = debug;
    }

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

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

    @SuppressWarnings("unchecked")
    @Override
    public  Optional retrieve(SlotRead slot) {
        return Optional.ofNullable((S)slots.get(slot.slot()));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof MultiContext)) {
            return false;
        }
        MultiContext context = (MultiContext) o;
        return slots.equals(context.slots);
    }

    @Override
    public int hashCode() {
        return slots.hashCode();
    }
}