All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atatctech.hephaestus.config.Config Maven / Gradle / Ivy

Go to download

The most suitable text language for documentation. Native support for Markdown and HTML.

There is a newer version: 1.0.4
Show newest version
package com.atatctech.hephaestus.config;

import com.atatctech.hephaestus.component.Component;
import com.atatctech.hephaestus.component.ComponentConfig;
import com.atatctech.hephaestus.exception.MissingFieldException;
import com.atatctech.hephaestus.export.fs.Transform;
import com.atatctech.hephaestus.parser.Parser;
import org.jetbrains.annotations.NotNull;
import org.reflections.Reflections;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The global configuration manager.
 */
public final class Config {
    private final static Config instance = new Config();

    public static Config getInstance() {
        return instance;
    }

    private final Map> parserMap = new ConcurrentHashMap<>();

    private final Map transformMap = new ConcurrentHashMap<>();

    private Config() {
        scanPackages(Component.class.getPackageName());
    }

    public void scanPackage(String pkg) {
        Reflections reflections = new Reflections(pkg);
        Set> classes = reflections.getTypesAnnotatedWith(ComponentConfig.class);
        for (Class clz : classes) {
            if (!Component.class.isAssignableFrom(clz)) continue;
            ComponentConfig componentConfig = clz.getAnnotation(ComponentConfig.class);
            try {
                Field field = clz.getField("PARSER");
                field.setAccessible(true);
                putParser(componentConfig.tagName(), (Parser) field.get(null));
            } catch (NoSuchFieldException ignored) {
                throw new MissingFieldException(clz, "PARSER");
            } catch (IllegalAccessException ignored) {
            }
            if (clz.isAnnotationPresent(Transform.RequireTransform.class)) {
                Transform transform = Transform.getTransform(clz);
                if (transform != null) putTransform(componentConfig.tagName(), transform);
            }
        }
    }

    public void scanPackages(String @NotNull ... packages) {
        for (String pkg : packages) scanPackage(pkg);
    }

    public @NotNull String[] listTagNames() {
        return parserMap.keySet().toArray(String[]::new);
    }

    public void putParser(String tagName, Parser parser) {
        parserMap.put(tagName, parser);
    }

    public Parser getParser(String tagName) {
        return parserMap.get(tagName);
    }

    public void putTransform(String tagName, Transform transform) {
        transformMap.put(tagName, transform);
    }

    public Transform getTransform(String tagName) {
        return transformMap.get(tagName);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy