package com.github.nylle.javafixture;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Context {
    private final Configuration configuration;
    private final Map<Integer, Object> cache = new ConcurrentHashMap<>();

    public Context(Configuration configuration) {

        if (configuration == null) {
            throw new IllegalArgumentException("configuration: null");
        }

        this.configuration = configuration;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public boolean isCached(SpecimenType type) {
        return cache.containsKey(type.hashCode());
    }

    public <T> T cached(SpecimenType type, T instance) {
        cache.putIfAbsent(type.hashCode(), instance);
        return (T) cache.get(type.hashCode());
    }

    public <T> T cached(SpecimenType type) {
        return (T) cache.get(type.hashCode());
    }
}


@@ -220,38 +220,10 @@
 ache
- = new ConcurrentHashMap%3C%3E()
 ;%0A
+
 %0A   
@@ -425,32 +425,410 @@
  configuration;%0A
+        this.cache = new ConcurrentHashMap%3C%3E();%0A    %7D%0A%0A    public Context(Configuration configuration, Map%3CInteger, Object%3E predefinedInstances) %7B%0A%0A        if (configuration == null) %7B%0A            throw new IllegalArgumentException(%22configuration: null%22);%0A        %7D%0A%0A        this.configuration = configuration;%0A        this.cache = new ConcurrentHashMap%3C%3E(predefinedInstances);%0A
     %7D%0A%0A    publi
@@ -1265,17 +1265,181 @@
 ode());%0A
-
     %7D%0A
+%0A    public %3CT%3E T preDefined(SpecimenType type, T instance) %7B%0A        return cache.containsKey(type.hashCode()) ? (T) cache.get(type.hashCode()) : instance;%0A    %7D%0A%0A
 %7D%0A%0A

Add read-only cache to context