<commit_before>package team.unstudio.udpl.variable;

import java.util.Set;
import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import com.google.common.base.Strings;
import com.google.common.collect.Sets;

public final class VariableHelper {

	private VariableHelper(){}
	
	private static final Set<VariableHandler> REGISTED_VARIABLE_HANDLERS = Sets.newLinkedHashSet();
	
	public static void registerVariable(@Nonnull VariableHandler handler){
		Validate.notNull(handler);
		REGISTED_VARIABLE_HANDLERS.add(handler);
	}
	
	public static void unregisterVariable(@Nonnull VariableHandler handler){
		Validate.notNull(handler);
		REGISTED_VARIABLE_HANDLERS.remove(handler.getName());
	}
	
	public static void unregisterVariable(@Nonnull String name){
		Validate.notEmpty(name);
		REGISTED_VARIABLE_HANDLERS.removeIf(handler->name.equals(handler.getName()));
	}
	
	public static void unregisterVariable(@Nonnull Plugin plugin){
		Validate.notNull(plugin);
		REGISTED_VARIABLE_HANDLERS.removeIf(handler->plugin.equals(handler.getPlugin()));
	}
	
	public static String apply(Player player, String value){
		return apply(player, value, '%', '%');
	}
	
	public static String apply(Player player, String value, char handlerStart, char handlerEnd){
		StringBuilder result = new StringBuilder(value.length());
		StringBuilder handlerName = new StringBuilder();
		boolean handlerMatchesing = false;
		for(char c:value.toCharArray()){
			if(handlerMatchesing){
				if(c == handlerEnd){
					handlerMatchesing = false;
					String handler = getValue(player, handlerName.toString());
					if(Strings.isNullOrEmpty(handler))
						result.append(handlerStart).append(handlerName.toString()).append(handlerEnd);
					else
						result.append(handler);
					handlerName.delete(0, handlerName.length());
				}else
					handlerName.append(c);
			}else{
				if(c == handlerStart)
					handlerMatchesing = true;
				else
					result.append(c);
			}
		}
		return result.toString();
	}
	
	private static String getValue(Player player, String name){
		for(VariableHandler handler : REGISTED_VARIABLE_HANDLERS){
			String value = handler.get(player, name);
			if(value == null)
				continue;
			
			return value;
		}
		return null;
	}
}<commit_msg>Update Variable API<commit_after>package team.unstudio.udpl.variable;

import java.util.Set;
import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import com.google.common.base.Strings;
import com.google.common.collect.Sets;

public final class VariableHelper {

	private VariableHelper(){}
	
	private static final Set<VariableHandler> REGISTED_VARIABLE_HANDLERS = Sets.newLinkedHashSet();
	
	public static void registerVariable(@Nonnull VariableHandler handler){
		Validate.notNull(handler);
		REGISTED_VARIABLE_HANDLERS.add(handler);
	}
	
	public static void unregisterVariable(@Nonnull VariableHandler handler){
		Validate.notNull(handler);
		REGISTED_VARIABLE_HANDLERS.remove(handler.getName());
	}
	
	public static void unregisterVariable(@Nonnull String name){
		Validate.notEmpty(name);
		REGISTED_VARIABLE_HANDLERS.removeIf(handler->name.equals(handler.getName()));
	}
	
	public static void unregisterVariable(@Nonnull Plugin plugin){
		Validate.notNull(plugin);
		REGISTED_VARIABLE_HANDLERS.removeIf(handler->plugin.equals(handler.getPlugin()));
	}
	
	public static String apply(Player player, String value){
		return apply(player, value, '%', '%');
	}
	
	public static String apply(Player player, String value, char handlerStart, char handlerEnd){
		StringBuilder result = new StringBuilder(value.length());
		StringBuilder handlerName = new StringBuilder();
		boolean handlerMatchesing = false;
		for(char c:value.toCharArray()){
			if(handlerMatchesing){
				if(c == handlerEnd){
					handlerMatchesing = false;
					String handler = getValue(player, handlerName.toString());
					if(Strings.isNullOrEmpty(handler))
						result.append(handlerStart).append(handlerName.toString()).append(handlerEnd);
					else
						result.append(handler);
					handlerName.delete(0, handlerName.length());
				}else
					handlerName.append(c);
			}else{
				if(c == handlerStart)
					handlerMatchesing = true;
				else
					result.append(c);
			}
		}
		return result.toString();
	}
	
	private static String getValue(Player player, String name){
		for(VariableHandler handler : REGISTED_VARIABLE_HANDLERS){
			try{
				String value = handler.get(player, name);
				if(value == null)
					continue;
				
				return value;
			}catch(Exception e){}
		}
		return null;
	}
}
{'fraction_non_alphanumeric': 0.11192109777015437, 'fraction_numerical': 0.0004288164665523156, 'mean_word_length': 4.867924528301887, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6719112', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1382, 'n_words': 287}