Skip to content

Commit 6961719

Browse files
committed
Add Adapter type. Add SkriptToYaml javadocs. Dumper cleanup.
1 parent a8a387f commit 6961719

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

src/main/java/io/github/goingoffskript/skriptvariabledump/SkriptToYaml.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
import java.util.Map;
1212
import java.util.function.BiConsumer;
1313

14+
/**
15+
* Skript data-to-YAML adapters.
16+
*/
1417
public class SkriptToYaml
1518
{
1619
private SkriptToYaml() {}
1720

18-
private static final Map<Class<?>, BiConsumer<Object, Map<String, Object>>> ADAPTERS = new HashMap<>();
21+
private static final Map<Class<?>, Adapter<?>> ADAPTERS = new HashMap<>();
1922

2023
static
2124
{
@@ -36,22 +39,48 @@ private SkriptToYaml() {}
3639
});
3740
}
3841

42+
/**
43+
* Adapts data from a specific type into a
44+
* {@code Map<String, Object>}.
45+
*
46+
* @param <T> data type
47+
*/
48+
@FunctionalInterface
49+
public interface Adapter<T> extends BiConsumer<T, Map<String, Object>> {}
50+
51+
/**
52+
* Registers an adapter for a specific data type.
53+
*
54+
* @param clazz the data type's class
55+
* @param adapter the adapter to register
56+
* @param <T> data type
57+
*/
3958
@SuppressWarnings("unchecked")
40-
public static <T> void adapts(Class<T> clazz, BiConsumer<T, Map<String, Object>> adapter)
59+
public static <T> void adapts(Class<T> clazz, Adapter<T> adapter)
4160
{
4261
ADAPTERS.put(clazz, (object, map) -> {
4362
map.put("==", clazz.getSimpleName());
4463
adapter.accept((T) object, map);
4564
});
4665
}
4766

67+
/**
68+
* Adapts an object into a string map or returns
69+
* it as-is if no adapter exists for its type.
70+
*
71+
* @param object the object to adapt
72+
*
73+
* @return the provided object
74+
* or a {@code Map<String, Object>}
75+
*/
76+
@SuppressWarnings("unchecked")
4877
public static Object adapt(Object object)
4978
{
50-
@NullOr BiConsumer<Object, Map<String, Object>> adapter = ADAPTERS.get(object.getClass());
79+
@NullOr Adapter<?> adapter = ADAPTERS.get(object.getClass());
5180
if (adapter == null) { return object; }
5281

5382
Map<String, Object> map = new LinkedHashMap<>();
54-
adapter.accept(object, map);
83+
((Adapter<Object>) adapter).accept(object, map);
5584
return map;
5685
}
5786
}

src/main/java/io/github/goingoffskript/skriptvariabledump/SkriptVariableDumper.java

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,53 @@
2121
import java.util.concurrent.atomic.AtomicBoolean;
2222
import java.util.concurrent.locks.Lock;
2323

24-
public class SkriptVariableDumper
24+
// No reason to expose this class, make it package-private
25+
final class SkriptVariableDumper
2526
{
2627
private SkriptVariableDumper() {}
2728

2829
private static final AtomicBoolean IS_DUMPING_VARIABLES = new AtomicBoolean(false);
2930

30-
private static final @NullOr Method GET_VARIABLES;
31+
private static final @NullOr Method GET_VARIABLES = method("getVariables");
3132

32-
static
33+
private static final @NullOr Method GET_READ_LOCK = method("getReadLock");
34+
35+
private static @NullOr Method method(String declaredMethodName)
3336
{
34-
@NullOr Method getVariables = null;
35-
3637
try
3738
{
38-
getVariables = Variables.class.getDeclaredMethod("getVariables");
39-
getVariables.setAccessible(true);
39+
Method method = Variables.class.getDeclaredMethod(declaredMethodName);
40+
method.setAccessible(true);
41+
return method;
4042
}
41-
catch (NoSuchMethodException e) { e.printStackTrace(); }
42-
43-
GET_VARIABLES = getVariables;
44-
}
45-
46-
private static final @NullOr Method GET_READ_LOCK;
47-
48-
static
49-
{
50-
@NullOr Method getReadLock = null;
51-
52-
try
43+
catch (NoSuchMethodException e)
5344
{
54-
getReadLock = Variables.class.getDeclaredMethod("getReadLock");
55-
getReadLock.setAccessible(true);
45+
e.printStackTrace();
46+
return null;
5647
}
57-
catch (NoSuchMethodException e) { e.printStackTrace(); }
58-
59-
GET_READ_LOCK = getReadLock;
6048
}
6149

6250
static boolean isInvalid() { return GET_VARIABLES == null || GET_READ_LOCK == null; }
6351

6452
@SuppressWarnings("unchecked")
65-
private static Map<String, Object> variables()
53+
private static <T> T invoke(@NullOr Method method)
6654
{
67-
if (GET_VARIABLES == null) { throw new IllegalStateException(); }
68-
try { return (Map<String, Object>) GET_VARIABLES.invoke(null); }
55+
if (method == null) { throw new IllegalArgumentException(); }
56+
try { return (T) method.invoke(null); }
6957
catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); }
7058
}
7159

72-
private static Lock readLock()
73-
{
74-
if (GET_READ_LOCK == null) { throw new IllegalStateException(); }
75-
try { return (Lock) GET_READ_LOCK.invoke(null); }
76-
catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); }
77-
}
60+
private static Map<String, Object> variables() { return invoke(GET_VARIABLES); }
61+
62+
private static Lock readLock() { return invoke(GET_READ_LOCK); }
7863

79-
@SuppressWarnings("ConstantConditions")
80-
private static String key(Map.Entry<String, Object> entry)
64+
private static String key(@NullOr String key)
8165
{
82-
@NullOr String key = entry.getKey();
8366
return (key == null || key.isEmpty()) ? "<none>" : key;
8467
}
8568

86-
@SuppressWarnings("ConstantConditions")
87-
private static @NullOr Object value(Map.Entry<String, Object> entry)
69+
private static @NullOr Object value(@NullOr Object value)
8870
{
89-
@NullOr Object value = entry.getValue();
9071
return (value == null) ? null : SkriptToYaml.adapt(value);
9172
}
9273

@@ -95,8 +76,8 @@ private static void dump(ConfigurationSection section, Map<String, Object> vars)
9576
{
9677
for (Map.Entry<String, Object> entry : vars.entrySet())
9778
{
98-
String key = key(entry);
99-
@NullOr Object value = value(entry);
79+
String key = key(entry.getKey());
80+
@NullOr Object value = value(entry.getValue());
10081

10182
if (value instanceof Map) { dump(section.createSection(key), (Map<String, Object>) value); }
10283
else { section.set(key, value); }
@@ -116,9 +97,9 @@ static Runnable task(CommandSender sender)
11697
{
11798
return () ->
11899
{
119-
boolean available = IS_DUMPING_VARIABLES.compareAndSet(false, true);
100+
boolean isAvailable = IS_DUMPING_VARIABLES.compareAndSet(false, true);
120101

121-
if (!available)
102+
if (!isAvailable)
122103
{
123104
sender.sendMessage("Already dumping variables, be patient...");
124105
return;

0 commit comments

Comments
 (0)