<commit_before>package org.neuron_di.api;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.FixedValue;

import java.lang.reflect.Method;

/**
 * Enumerates strategies for caching dependencies at an injection point
 * (synapse).
 */
public enum CachingStrategy {

    /**
     * Does not cache the return value of the annotated method:
     * A subsequent call by any thread may return another instance.
     */
    DISABLED {

        @Override
        Callback callback(Brain brain, Method method) {
            return (FixedValue) () -> brain.make(method.getReturnType());
        }
    },

    /**
     * Caches the return value of the annotated method:
     * Although not strictly required, a subsequent call by the same thread
     * should return the same instance.
     * A subsequent call by any other thread may return another instance.
     * This definition recursively applies to all threads.
     */
    NOT_THREAD_SAFE {

        @Override
        Callback callback(Brain brain, Method method) {
            return new FixedValue() {

                // TODO: Consider removing `volatile` modifier.
                volatile Object returnValue;

                @Override
                public Object loadObject() {
                    final Object value = returnValue;
                    return null != value
                            ? value
                            : (returnValue = brain.make(method.getReturnType()));
                }
            };
        }
    },

    /**
     * Caches the return value of the annotated method:
     * A subsequent call by the same thread must return the same instance.
     * Although not strictly required, a subsequent call by any other thread
     * should return another instance.
     * This definition recursively applies to all threads.
     */
    THREAD_LOCAL {

        @Override
        Callback callback(Brain brain, Method method) {
            return new FixedValue() {

                final ThreadLocal<Object> returnValues = new ThreadLocal<Object>() {
                    @Override
                    protected Object initialValue() {
                        return brain.make(method.getReturnType());
                    }
                };

                @Override
                public Object loadObject() { return returnValues.get(); }
            };
        }
    },

    /**
     * Caches the return value of the annotated method:<commit_msg>Improves specification of caching strategies.<commit_after>package org.neuron_di.api;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.FixedValue;

import java.lang.reflect.Method;

/**
 * Enumerates strategies for caching dependencies at an injection point
 * (synapse).
 */
public enum CachingStrategy {

    /**
     * Does not cache the return value of the annotated method:
     * Although not strictly required, a subsequent call by any thread should
     * return another instance.
     */
    DISABLED {

        @Override
        Callback callback(Brain brain, Method method) {
            return (FixedValue) () -> brain.make(method.getReturnType());
        }
    },

    /**
     * Caches the return value of the annotated method:
     * Although not strictly required, a subsequent call by the same thread
     * should return the same instance.
     * A subsequent call by any other thread may return another instance.
     * This definition recursively applies to any thread.
     */
    NOT_THREAD_SAFE {

        @Override
        Callback callback(Brain brain, Method method) {
            return new FixedValue() {

                // TODO: Consider removing `volatile` modifier.
                volatile Object returnValue;

                @Override
                public Object loadObject() {
                    final Object value = returnValue;
                    return null != value
                            ? value
                            : (returnValue = brain.make(method.getReturnType()));
                }
            };
        }
    },

    /**
     * Caches the return value of the annotated method:
     * A subsequent call by the same thread must return the same instance.
     * Although not strictly required, a subsequent call by any other thread
     * should return another instance.
     * This definition recursively applies to any thread.
     */
    THREAD_LOCAL {

        @Override
        Callback callback(Brain brain, Method method) {
            return new FixedValue() {

                final ThreadLocal<Object> returnValues = new ThreadLocal<Object>() {
                    @Override
                    protected Object initialValue() {
                        return brain.make(method.getReturnType());
                    }
                };

                @Override
                public Object loadObject() { return returnValues.get(); }
            };
        }
    },

    /**
     * Caches the return value of the annotated method:
{'fraction_non_alphanumeric': 0.07461786001609011, 'fraction_numerical': 0.0, 'mean_word_length': 1.7597114317425082, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3134059', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1116, 'n_words': 411}