Skip to content

Commit

Permalink
Config is now static.
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Aug 5, 2023
1 parent 90f8e4e commit 311f0a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/atatctech/hephaestus/Hephaestus.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class Hephaestus {
if (Text.wrappedBy(expr, '{', '}')) {
if (temp.tagName.isEmpty()) return Text.PARSER.parse(temp.inner);
temp.tagName = temp.tagName.replaceAll(" ", "");
Parser<?> parser = Config.getInstance().getParser(temp.tagName);
Parser<?> parser = Config.getParser(temp.tagName);
Component component = parser == null ? temp : parser.parse(temp.inner);
if (component == null) return null;
AttributeUtils.injectAttributes(component, attributesAndBody.attributesExpr());
Expand All @@ -57,7 +57,7 @@ public final class Hephaestus {
}

public static @NotNull String @NotNull [] listTagNames() {
return Config.getInstance().listTagNames();
return Config.listTagNames();
}

public static @NotNull String clean(@NotNull String expr) {
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/atatctech/hephaestus/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
* The global configuration manager.
*/
public final class Config {
private final static Config instance = new Config();
private static final Map<String, Parser<?>> parserMap = new ConcurrentHashMap<>();

public static @NotNull Config getInstance() {
return instance;
}

private final Map<String, Parser<?>> parserMap = new ConcurrentHashMap<>();
private static final Map<String, Transform> transformMap = new ConcurrentHashMap<>();

private final Map<String, Transform> transformMap = new ConcurrentHashMap<>();
static {
scanPackages(Component.class.getPackageName());
}

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

public void scanPackage(@NotNull String pkg) {
public static void scanPackage(@NotNull String pkg) {
Reflections reflections = new Reflections(pkg);
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(ComponentConfig.class);
for (Class<?> clz : classes) {
Expand All @@ -52,27 +49,27 @@ public void scanPackage(@NotNull String pkg) {
}
}

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

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

public void putParser(@NotNull String tagName, @NotNull Parser<?> parser) {
public static void putParser(@NotNull String tagName, @NotNull Parser<?> parser) {
parserMap.put(tagName, parser);
}

public @Nullable Parser<?> getParser(@NotNull String tagName) {
public static @Nullable Parser<?> getParser(@NotNull String tagName) {
return parserMap.get(tagName);
}

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

public @Nullable Transform getTransform(@NotNull String tagName) {
public static @Nullable Transform getTransform(@NotNull String tagName) {
return transformMap.get(tagName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public boolean write(@NotNull String filename) {
}

public static @NotNull ComponentFile read(@NotNull String filename) throws IOException, HephaestusException {
Transform transform = Config.getInstance().getTransform(filename.substring(filename.lastIndexOf(".") + 1));
Transform transform = Config.getTransform(filename.substring(filename.lastIndexOf(".") + 1));
return new ComponentFile((transform == null ? new Transform() : transform).afterRead(Basics.NativeHandler.readFile(filename)));
}
}

0 comments on commit 311f0a0

Please sign in to comment.