Skip to content

Commit

Permalink
add path priority comparison to AnnotatedConfig and Config Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 6, 2024
1 parent 1621884 commit b4acb3d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setup() {
List<Field> validFields = new ArrayList<>();
Arrays.stream(this.getClass().getDeclaredFields())
.filter(this::checkPathField)
.sorted(Comparator.comparingInt(Field::hashCode))
.sorted(this::compareField)
.forEach(field -> {
ConfigPath configPath = field.getAnnotation(ConfigPath.class);
pathFieldMap.put(new PathString(configPath.value()), field);
Expand Down Expand Up @@ -97,6 +97,20 @@ private boolean checkPathField(Field field) {
return true;
}

/**
* Compare the fields
*
* @param field1 the first field
* @param field2 the second field
*
* @return the comparison result
*/
private int compareField(Field field1, Field field2) {
ConfigPath configPath1 = field1.getAnnotation(ConfigPath.class);
ConfigPath configPath2 = field2.getAnnotation(ConfigPath.class);
return Integer.compare(configPath1.priority(), configPath2.priority());
}

private void setupField(Field field) {
ConfigPath configPath = field.getAnnotation(ConfigPath.class);
PathString path = new PathString(configPath.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
* @return the converter
*/
@NotNull Class<? extends Converter> converter() default DefaultConverter.class;

/**
* Get the priority
*
* @return the priority
*/
int priority() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* The internal invocation handler to map the interface to the config
Expand Down Expand Up @@ -50,7 +53,7 @@ public class ConfigInvocationHandler<T> implements InvocationHandler {
this.stickyValue = stickyValue;

Arrays.stream(this.clazz.getDeclaredMethods())
.sorted(Comparator.comparingInt(Method::hashCode))
.sorted(ConfigInvocationHandler::compareMethod)
.forEach(this::setupMethod);

if (addDefault) {
Expand All @@ -60,6 +63,15 @@ public class ConfigInvocationHandler<T> implements InvocationHandler {
}
}

private static int compareMethod(Method method1, Method method2) {
if (method1.equals(method2) || !method1.isAnnotationPresent(ConfigPath.class) || !method2.isAnnotationPresent(ConfigPath.class)) {
return 0;
}
ConfigPath configPath1 = method1.getAnnotation(ConfigPath.class);
ConfigPath configPath2 = method2.getAnnotation(ConfigPath.class);
return Integer.compare(configPath1.priority(), configPath2.priority());
}

/**
* Check if the class is a primitive type
*
Expand Down

0 comments on commit b4acb3d

Please sign in to comment.