<commit_before>package fr.liglab.adele.cream.runtime.handler.entity.utils;

import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.FieldInterceptor;
import org.apache.felix.ipojo.InstanceManager;
import org.apache.felix.ipojo.MethodInterceptor;
import org.apache.felix.ipojo.metadata.Element;
import org.apache.felix.ipojo.parser.FieldMetadata;
import org.apache.felix.ipojo.parser.MethodMetadata;
import org.apache.felix.ipojo.parser.PojoMetadata;

import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Interceptor to handle state fields that are not handler by direct access, but using synchronization
 * functions (push,pull,apply) 
 */
public class SynchronisationInterceptor extends AbstractStateInterceptor implements StateInterceptor, FieldInterceptor, MethodInterceptor {

	/**
	 * The invocation handlers used in every field access
	 */
	private final Map<String,BiConsumer<Object,Object>> applyFunctions	= new HashMap<>();
	private final Map<String,Function<Object,Object>> pullFunctions 	= new HashMap<>();

	/**
	 * The periodic tasks associated to pull fields
	 */
	private final Map<String,AbstractContextHandler.PeriodicTask> pullTasks 	= new HashMap<>();

	/**
	 * The associated entity handler in charge of keeping the context state
	 */
	private final AbstractContextHandler abstractContextHandler;

	/**
	 * The mapping from methods handled by this interceptor to states of the context
	 */
	private final Map<String,String> methodToState = new HashMap<>();

	/**
	 * @param abstractContextHandler
	 */
	public SynchronisationInterceptor(AbstractContextHandler abstractContextHandler) {
		this.abstractContextHandler = abstractContextHandler;
	}

	@Override
	public Object onGet(Object pojo, String fieldName, Object value) {
		Function<Object,Object> pullFunction = pullFunctions.get(fieldName);
		if (pullFunction != null) {
			return pullFunction.apply(pojo);
		}

		return abstractContextHandler.getStateValue(fieldToState.get(fieldName));
	}

	@Override
	public void onSet(Object pojo, String fieldName, Object value) {
		BiConsumer<Object,Object> applyFunction = applyFunctions.get(fieldName);
		if (applyFunction != null && pojo != null && value != null) {
			applyFunction.accept(pojo,value);
		}
	}
<commit_msg>Fix : state is not updated when the pull function is not periodic<commit_after>package fr.liglab.adele.cream.runtime.handler.entity.utils;

import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.FieldInterceptor;
import org.apache.felix.ipojo.InstanceManager;
import org.apache.felix.ipojo.MethodInterceptor;
import org.apache.felix.ipojo.metadata.Element;
import org.apache.felix.ipojo.parser.FieldMetadata;
import org.apache.felix.ipojo.parser.MethodMetadata;
import org.apache.felix.ipojo.parser.PojoMetadata;

import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Interceptor to handle state fields that are not handler by direct access, but using synchronization
 * functions (push,pull,apply) 
 */
public class SynchronisationInterceptor extends AbstractStateInterceptor implements StateInterceptor, FieldInterceptor, MethodInterceptor {

	/**
	 * The invocation handlers used in every field access
	 */
	private final Map<String,BiConsumer<Object,Object>> applyFunctions	= new HashMap<>();
	private final Map<String,Function<Object,Object>> pullFunctions 	= new HashMap<>();

	/**
	 * The periodic tasks associated to pull fields
	 */
	private final Map<String,AbstractContextHandler.PeriodicTask> pullTasks 	= new HashMap<>();

	/**
	 * The associated entity handler in charge of keeping the context state
	 */
	private final AbstractContextHandler abstractContextHandler;

	/**
	 * The mapping from methods handled by this interceptor to states of the context
	 */
	private final Map<String,String> methodToState = new HashMap<>();

	/**
	 * @param abstractContextHandler
	 */
	public SynchronisationInterceptor(AbstractContextHandler abstractContextHandler) {
		this.abstractContextHandler = abstractContextHandler;
	}

	@Override
	public Object onGet(Object pojo, String fieldName, Object value) {
		Function<Object,Object> pullFunction = pullFunctions.get(fieldName);
		if (pullFunction != null) {
			Object pulledValue =  pullFunction.apply(pojo);
			abstractContextHandler.update(fieldToState.get(fieldName),pulledValue);
		}

		return abstractContextHandler.getStateValue(fieldToState.get(fieldName));
	}

	@Override
	public void onSet(Object pojo, String fieldName, Object value) {
		BiConsumer<Object,Object> applyFunction = applyFunctions.get(fieldName);
		if (applyFunction != null && pojo != null && value != null) {
			applyFunction.accept(pojo,value);
		}
	}

{'fraction_non_alphanumeric': 0.09890539483971852, 'fraction_numerical': 0.0, 'mean_word_length': 5.577120822622108, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '5569264', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1409, 'n_words': 359}