<commit_before>package net.popsim.src.fxui;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import net.popsim.src.simu.script.Script;
import net.popsim.src.util.config.JsonConfigLoader;

import java.io.File;
import java.util.*;

public class Context implements JsonConfigLoader.Target {

    @Expose
    @SerializedName("Update interval")
    private long mTick;

    @Expose
    @SerializedName("Random seed")
    private String mRandomSeedString;
    private long mRngSeed;

    @Expose
    @SerializedName("World dimensions")
    private int[] mWorldSize;

    @Expose
    @SerializedName("Scripts")
    private Map<String, String> mScriptInfoMap;
    private Map<String, Script> mScriptMap;

    public Context() {
        // Default values
        mTick = 16; // 60 fps
        mRandomSeedString = "";
        mWorldSize = new int[] {100, 100};
        mScriptInfoMap = new HashMap<>();
    }

    @Override
    public void postLoad() throws Exception {
        // Random seed
        mRngSeed = ContextHelper.parseSeed(mRandomSeedString);
        // World dimensions
        if (mWorldSize.length != 2)
            throw new Exception("World dimensions should be two dimensional");
        // Script Map
        HashMap<String, Script> sMap = new HashMap<>(mScriptInfoMap.size());
        for (Map.Entry<String, String> e : mScriptInfoMap.entrySet()) {
            String name = e.getKey();
            Script script = Script.compile(name, new File(ContextHelper.DIR_HOME, e.getValue()));
            sMap.put(name, script);
        }
        mScriptMap = Collections.unmodifiableMap(sMap);
    }

    public long getTick() {
        return mTick;
    }

    public long getRngSeed() {
        return mRngSeed;
    }

    public int getWorldWidth() {
        return mWorldSize[0];
    }

    public int getWorldHeight() {
        return mWorldSize[1];
    }

    public Script getScript(String name) {
        return mScriptMap.get(name);
    }

    public <T extends Script> T getScript(String name, Class<T> type) {
        Script s = mScriptMap.get(name);
        if (s == null || !type.isInstance(s))
            return null;
        else return type.cast(s);
    }

    public <T extends Script> List<T> getScripts(Class<T> type) {
        ArrayList<T> result = new ArrayList<>();
        mScriptMap.forEach((key, value) -> {
            if (type.isInstance(value))
                result.add(type.cast(value));
        });
        return result;
    }
}<commit_msg>Made getScripts() much more flexible<commit_after>package net.popsim.src.fxui;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import net.popsim.src.simu.script.Script;
import net.popsim.src.util.config.JsonConfigLoader;

import java.io.File;
import java.util.*;

public class Context implements JsonConfigLoader.Target {

    @Expose
    @SerializedName("Update interval")
    private long mTick;

    @Expose
    @SerializedName("Random seed")
    private String mRandomSeedString;
    private long mRngSeed;

    @Expose
    @SerializedName("World dimensions")
    private int[] mWorldSize;

    @Expose
    @SerializedName("Scripts")
    private Map<String, String> mScriptInfoMap;
    private Map<String, Script> mScriptMap;

    public Context() {
        // Default values
        mTick = 16; // 60 fps
        mRandomSeedString = "";
        mWorldSize = new int[] {100, 100};
        mScriptInfoMap = new HashMap<>();
    }

    @Override
    public void postLoad() throws Exception {
        // Random seed
        mRngSeed = ContextHelper.parseSeed(mRandomSeedString);
        // World dimensions
        if (mWorldSize.length != 2)
            throw new Exception("World dimensions should be two dimensional");
        // Script Map
        HashMap<String, Script> sMap = new HashMap<>(mScriptInfoMap.size());
        for (Map.Entry<String, String> e : mScriptInfoMap.entrySet()) {
            String name = e.getKey();
            if (sMap.containsKey(name))
                throw new Exception("Duplicate script name: " + name);
            Script script = Script.compile(name, new File(ContextHelper.DIR_HOME, e.getValue()));
            sMap.put(name, script);
        }
        mScriptMap = Collections.unmodifiableMap(sMap);
    }

    public long getTick() {
        return mTick;
    }

    public long getRngSeed() {
        return mRngSeed;
    }

    public int getWorldWidth() {
        return mWorldSize[0];
    }

    public int getWorldHeight() {
        return mWorldSize[1];
    }

    @SuppressWarnings("unchecked")
    public <T extends Script> List<T> getScripts(Class<? extends T> type, String... names) {
        ArrayList<T> result = new ArrayList<>();
        List<String> toCheck;
        if (names.length > 0)
            toCheck = Arrays.asList(names);
        else toCheck = new ArrayList<>(mScriptMap.keySet());
        for (String name : toCheck) {
            Script s = mScriptMap.get(name);
            if (s != null && type.isInstance(s))
                result.add((T) s);
        }
        return result;
    }
}
{'fraction_non_alphanumeric': 0.113038394075229, 'fraction_numerical': 0.005262132137984798, 'mean_word_length': 2.4675675675675675, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '2475382', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1500, 'n_words': 369}