<commit_before>package com.quirkygaming.propertylib;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

public class ProtectedEnumPropertyMap<E extends Enum<E>, T> {
	
	private static final HashMap<Class<?>, Enum<?>[]> enumCache = new HashMap<Class<?>, Enum<?>[]>();
	
	private HashMap<Class<?>, Integer> positions = null;
	private boolean multiEnums = false;
	
	private final Class<E> mainEnumClass;
	
	private final int size;
	private final Object[] items;
	
	protected ProtectedEnumPropertyMap(Class<E> enumClass) {
		size = getEnumValues(enumClass).length;
		items = new Object[size];
		mainEnumClass = enumClass;
	}
	
	protected ProtectedEnumPropertyMap(Class<E> mainEnumClass, Class<?>... otherEnums) {
		int currentPos = getEnumValues(mainEnumClass).length;
		
		multiEnums = true;
		positions = new HashMap<Class<?>, Integer>();

		for (Class<?> ec : otherEnums) {
			if (!ec.isEnum()) throw new RuntimeException(ec.getName() + " is not an enum");
			positions.put(ec, currentPos);
			currentPos += getEnumValues(ec).length;
		}
		
		size = currentPos;
		items = new Object[size];
		this.mainEnumClass = mainEnumClass;
	}
	
	private Enum<?>[] getEnumValues(Class<?> enumClass) {
		
		if (!enumCache.containsKey(enumClass)) {
			Method m;
			try {
				m=enumClass.getMethod("values");
				if (!m.isAccessible()) m.setAccessible(true);
				Enum<?>[] vals = (Enum<?>[]) m.invoke(null);
				enumCache.put(enumClass, vals);
				return vals;
			} catch (IllegalArgumentException e) {
				throw getEx(e, enumClass.getName());
			} catch (SecurityException e) {
				throw getEx(e, enumClass.getName());
			} catch (IllegalAccessException e) {
				throw getEx(e, enumClass.getName());
			} catch (InvocationTargetException e) {
				throw getEx(e, enumClass.getName());
			} catch (NoSuchMethodException e) {
				throw getEx(e, enumClass.getName());
			} catch (ClassCastException e) {
				throw getEx(e, enumClass.getName());
			}
		} else {
			return enumCache.get(enumClass);
		}
		
	}
	private RuntimeException getEx(Exception e, String name) {
		return new RuntimeException("Runtime error while processing enum " + name, e);
	}
	private RuntimeException getCastMismatch(Exception e) {
		return new RuntimeException("Internal casting mismatch in EnumPropertyMap; check implementation consistancy", e);
	}

	protected int size() {
		return size;
	}
	
	int getIndex(Enum<?> enumKey) {
		if (mainEnumClass == enumKey.getClass()) return enumKey.ordinal();
		if (multiEnums) {
			Integer position = positions.get(enumKey.getClass());
			if (position == null) return -1;
			return positions.get(enumKey.getClass()) + enumKey.ordinal();
		}
		return -1;
	}

	protected T get(Enum<?> enumKey) {
		return get(getIndex(enumKey));
	}
	
	protected <X> X getAsType(Enum<?> enumKey) {
		return get(getIndex(enumKey));
	}
	@SuppressWarnings("unchecked")
	<X> X get(int index) {
		if (index == -1) return null;
		try {
			return (X) items[index];
		} catch (ClassCastException e) {
			throw getCastMismatch(e);
		}
	}
	
	protected T set(Enum<?> enumKey, T value) {
		return set(getIndex(enumKey), value);
	}
	<X> X set(int index, X value) {
		if (index == -1) return null;
		try {
			items[index] = value;
			return value;
		} catch (ClassCastException e) {
			throw getCastMismatch(e);
		}
	}<commit_msg>Added missing setAsType method.<commit_after>package com.quirkygaming.propertylib;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

public class ProtectedEnumPropertyMap<E extends Enum<E>, T> {
	
	private static final HashMap<Class<?>, Enum<?>[]> enumCache = new HashMap<Class<?>, Enum<?>[]>();
	
	private HashMap<Class<?>, Integer> positions = null;
	private boolean multiEnums = false;
	
	private final Class<E> mainEnumClass;
	
	private final int size;
	private final Object[] items;
	
	protected ProtectedEnumPropertyMap(Class<E> enumClass) {
		size = getEnumValues(enumClass).length;
		items = new Object[size];
		mainEnumClass = enumClass;
	}
	
	protected ProtectedEnumPropertyMap(Class<E> mainEnumClass, Class<?>... otherEnums) {
		int currentPos = getEnumValues(mainEnumClass).length;
		
		multiEnums = true;
		positions = new HashMap<Class<?>, Integer>();

		for (Class<?> ec : otherEnums) {
			if (!ec.isEnum()) throw new RuntimeException(ec.getName() + " is not an enum");
			positions.put(ec, currentPos);
			currentPos += getEnumValues(ec).length;
		}
		
		size = currentPos;
		items = new Object[size];
		this.mainEnumClass = mainEnumClass;
	}
	
	private Enum<?>[] getEnumValues(Class<?> enumClass) {
		
		if (!enumCache.containsKey(enumClass)) {
			Method m;
			try {
				m=enumClass.getMethod("values");
				if (!m.isAccessible()) m.setAccessible(true);
				Enum<?>[] vals = (Enum<?>[]) m.invoke(null);
				enumCache.put(enumClass, vals);
				return vals;
			} catch (IllegalArgumentException e) {
				throw getEx(e, enumClass.getName());
			} catch (SecurityException e) {
				throw getEx(e, enumClass.getName());
			} catch (IllegalAccessException e) {
				throw getEx(e, enumClass.getName());
			} catch (InvocationTargetException e) {
				throw getEx(e, enumClass.getName());
			} catch (NoSuchMethodException e) {
				throw getEx(e, enumClass.getName());
			} catch (ClassCastException e) {
				throw getEx(e, enumClass.getName());
			}
		} else {
			return enumCache.get(enumClass);
		}
		
	}
	private RuntimeException getEx(Exception e, String name) {
		return new RuntimeException("Runtime error while processing enum " + name, e);
	}
	private RuntimeException getCastMismatch(Exception e) {
		return new RuntimeException("Internal casting mismatch in EnumPropertyMap; check implementation consistancy", e);
	}

	protected int size() {
		return size;
	}
	
	int getIndex(Enum<?> enumKey) {
		if (mainEnumClass == enumKey.getClass()) return enumKey.ordinal();
		if (multiEnums) {
			Integer position = positions.get(enumKey.getClass());
			if (position == null) return -1;
			return positions.get(enumKey.getClass()) + enumKey.ordinal();
		}
		return -1;
	}

	protected T get(Enum<?> enumKey) {
		return get(getIndex(enumKey));
	}
	
	protected <X> X getAsType(Enum<?> enumKey) {
		return get(getIndex(enumKey));
	}
	@SuppressWarnings("unchecked")
	<X> X get(int index) {
		if (index == -1) return null;
		try {
			return (X) items[index];
		} catch (ClassCastException e) {
			throw getCastMismatch(e);
		}
	}
	
	protected T set(Enum<?> enumKey, T value) {
		return set(getIndex(enumKey), value);
	}
	protected <X> X setAsType(Enum<?> enumKey, X value) {
		return set(getIndex(enumKey), value);
	}
	<X> X set(int index, X value) {
		if (index == -1) return null;
		try {
			items[index] = value;
			return value;
		} catch (ClassCastException e) {
			throw getCastMismatch(e);
		}
	}
{'fraction_non_alphanumeric': 0.13910876575784228, 'fraction_numerical': 0.0011726766344180592, 'mean_word_length': 4.285050348567002, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '936401', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2197, 'n_words': 557}