Skip to content

Commit 548b2f0

Browse files
committed
lombok & util method for line writing
1 parent e85f374 commit 548b2f0

File tree

9 files changed

+57
-104
lines changed

9 files changed

+57
-104
lines changed

src/main/java/com/probejs/compiler/EventCompiler.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.probejs.formatter.FormatterComments;
77
import com.probejs.info.EventInfo;
88
import com.probejs.info.type.TypeClass;
9+
import com.probejs.util.PUtil;
910
import lombok.val;
1011

1112
import java.io.BufferedWriter;
@@ -64,10 +65,7 @@ private static void writeForgeEvents(BufferedWriter writer) throws IOException {
6465
)
6566
.forEach(lines::add);
6667
lines.add("");
67-
for (final String line : lines) {
68-
writer.write(line);
69-
writer.write("\n");
70-
}
68+
PUtil.writeLines(writer, lines);
7169
}
7270

7371
private static void writeWildcardEvents(BufferedWriter writer) throws IOException {
@@ -95,10 +93,7 @@ private static void writeWildcardEvents(BufferedWriter writer) throws IOExceptio
9593
"declare function onEvent(name: `${string}.${string}`, handler: (event: Internal.EventJS) => void): void;"
9694
);
9795
lines.add("");
98-
for (val line : lines) {
99-
writer.write(line);
100-
writer.write("\n");
101-
}
96+
PUtil.writeLines(writer, lines);
10297
}
10398

10499
private static void writeEvents(BufferedWriter writer) throws IOException {
@@ -127,10 +122,6 @@ private static void writeEvents(BufferedWriter writer) throws IOException {
127122
""
128123
)
129124
);
130-
for (final String line : lines) {
131-
writer.write(line);
132-
writer.write("\n");
133-
}
125+
PUtil.writeLines(writer, lines);
134126
}
135-
136127
}

src/main/java/com/probejs/compiler/SnippetCompiler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ public static void compile() throws IOException {
9696
private static void compileClassNames() throws IOException {
9797
JsonObject resultJson = new JsonObject();
9898
for (Map.Entry<String, NameResolver.ResolvedName> entry : NameResolver.resolvedNames.entrySet()) {
99-
final String className = entry.getKey();
100-
final NameResolver.ResolvedName resolvedName = entry.getValue();
101-
final JsonObject classJson = new JsonObject();
102-
final JsonArray prefix = new JsonArray();
99+
val className = entry.getKey();
100+
val resolvedName = entry.getValue();
101+
val classJson = new JsonObject();
102+
val prefix = new JsonArray();
103103
prefix.add(String.format("!%s", resolvedName.getFullName()));
104104
classJson.add("prefix", prefix);
105105
classJson.addProperty("body", className);
106106
resultJson.add(resolvedName.getFullName(), classJson);
107107
}
108108

109-
Path codeFile = ProbePaths.WORKSPACE.resolve("classNames.code-snippets");
110-
BufferedWriter writer = Files.newBufferedWriter(codeFile);
109+
val codeFile = ProbePaths.WORKSPACE.resolve("classNames.code-snippets");
110+
val writer = Files.newBufferedWriter(codeFile);
111111
ProbeJS.GSON.toJson(resultJson, writer);
112112
writer.close();
113113
}

src/main/java/com/probejs/compiler/TypingCompiler.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ public static void compileGlobal(Set<Class<?>> globalClasses) throws IOException
9898
val path = entry.getKey();
9999
val formatters = entry.getValue();
100100
val namespace = new FormatterNamespace(path, formatters);
101-
for (val line : namespace.formatLines(0, 4)) {
102-
writer.write(line);
103-
writer.write('\n');
104-
}
101+
PUtil.writeLines(writer, namespace.formatLines(0, 4));
105102
}
106103

107104
for (val entry : DocManager.classAdditions.entrySet()) {
@@ -111,24 +108,14 @@ public static void compileGlobal(Set<Class<?>> globalClasses) throws IOException
111108
}
112109

113110
//namespace::Document
114-
for (String line : new FormatterNamespace(
111+
PUtil.writeLines(writer, new FormatterNamespace(
115112
"Document",
116113
DocManager.classAdditions.values().stream().map(l -> l.get(0)).collect(Collectors.toList())
117-
)
118-
.formatLines(0, 4)) {
119-
writer.write(line);
120-
writer.write("\n");
121-
}
114+
).formatLines(0, 4));
122115
//namespace::Type
123-
for (String line : new FormatterNamespace("Type", DocManager.typeDocuments).formatLines(0, 4)) {
124-
writer.write(line);
125-
writer.write("\n");
126-
}
116+
PUtil.writeLines(writer, new FormatterNamespace("Type", DocManager.typeDocuments).formatLines(0, 4));
127117
//no namespace
128-
for (String line : new FormatterRaw(DocManager.rawTSDoc).formatLines(0, 4)) {
129-
writer.write(line);
130-
writer.write("\n");
131-
}
118+
PUtil.writeLines(writer, new FormatterRaw(DocManager.rawTSDoc).formatLines(0, 4));
132119

133120
writer.close();
134121
}

src/main/java/com/probejs/compiler/special/PlatformDataCompiler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
import java.util.ArrayList;
99
import java.util.List;
1010
import java.util.stream.Collectors;
11+
12+
import com.probejs.util.PUtil;
13+
import lombok.val;
1114
import me.shedaniel.architectury.platform.Platform;
1215

1316
public class PlatformDataCompiler {
1417

1518
public static void compile(BufferedWriter writer) throws IOException {
16-
final List<String> lines = new ArrayList<>();
19+
val lines = new ArrayList<String>();
1720
//modids
18-
final String modids = Platform
21+
val modids = Platform
1922
.getModIds()
2023
.stream()
2124
.map(ProbeJS.GSON::toJson)
2225
.collect(Collectors.joining("|"));
2326
lines.add(String.format("type modids = %s;", modids));
2427
//more?
2528

26-
for (final String line : new FormatterNamespace("platform", new FormatterRaw(lines, false))
27-
.formatLines(0, 4)) {
28-
writer.write(line);
29-
writer.write('\n');
30-
}
29+
PUtil.writeLines(
30+
writer,
31+
new FormatterNamespace("platform", new FormatterRaw(lines, false)).formatLines(0, 4)
32+
);
3133
writer.write('\n');
3234
}
3335
}

src/main/java/com/probejs/compiler/special/RecipeHoldersCompiler.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.io.IOException;
1414
import java.util.*;
1515
import java.util.Map.Entry;
16+
17+
import lombok.val;
1618
import net.minecraft.resources.ResourceLocation;
1719

1820
public abstract class RecipeHoldersCompiler {
@@ -25,17 +27,17 @@ public abstract class RecipeHoldersCompiler {
2527
public static void init(Map<ResourceLocation, RecipeTypeJS> recipeHandlers) {
2628
namespace2Method.clear();
2729
recipeHandlers.forEach((key, value) -> {
28-
String namespace = key.getNamespace();
29-
String invoke = key.getPath();
30-
String recipeJSName = NameResolver.resolveName(value.factory.get().getClass()).getFullName();
30+
val namespace = key.getNamespace();
31+
val invoke = key.getPath();
32+
val recipeJSName = NameResolver.resolveName(value.factory.get().getClass()).getFullName();
3133

3234
namespace2Method.put(namespace, new Pair<>(invoke, recipeJSName));
3335
});
3436
}
3537

3638
public static List<String> format(int indent, int stepIndent) {
37-
final List<IFormatter> namespecedFmtr = new ArrayList<>();
38-
final String step = PUtil.indent(stepIndent);
39+
val namespecedFmtr = new ArrayList<IFormatter>();
40+
val step = PUtil.indent(stepIndent);
3941

4042
{
4143
List<String> base = new ArrayList<>();
@@ -50,7 +52,7 @@ public static List<String> format(int indent, int stepIndent) {
5052
}
5153

5254
for (Entry<String, Collection<Pair<String, String>>> entry : namespace2Method.asMap().entrySet()) {
53-
String name = entry.getKey();
55+
val name = entry.getKey();
5456
List<String> lines = new ArrayList<>();
5557
//name
5658
lines.add(String.format("class %s {", name));
@@ -70,10 +72,7 @@ public static List<String> format(int indent, int stepIndent) {
7072
}
7173

7274
public static void compile(BufferedWriter writer) throws IOException {
73-
for (String line : format(0, 4)) {
74-
writer.write(line);
75-
writer.write('\n');
76-
}
75+
PUtil.writeLines(writer, format(0, 4));
7776
writer.write('\n');
7877
namespace2Method.clear();
7978
}

src/main/java/com/probejs/compiler/special/TagCompiler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.stream.Collectors;
1010

1111
import com.probejs.info.SpecialData;
12+
import com.probejs.util.PUtil;
1213
import lombok.val;
1314
import net.minecraft.resources.ResourceLocation;
1415

@@ -40,10 +41,7 @@ public static List<String> format(int indent, int stepIndent) {
4041
}
4142

4243
public static void compile(BufferedWriter writer) throws IOException {
43-
for (String line : TagCompiler.format(0, 4)) {
44-
writer.write(line);
45-
writer.write('\n');
46-
}
44+
PUtil.writeLines(writer, TagCompiler.format(0, 4));
4745
writer.write('\n');
4846
}
4947
}

src/main/java/com/probejs/formatter/resolver/NameResolver.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
import java.util.function.Supplier;
1717
import java.util.stream.Collectors;
1818

19+
import lombok.EqualsAndHashCode;
20+
import lombok.ToString;
1921
import lombok.val;
2022
import net.minecraft.world.damagesource.DamageSource;
2123

2224
public class NameResolver {
2325

26+
@EqualsAndHashCode
27+
@ToString
2428
public static class ResolvedName {
2529

2630
public static final ResolvedName UNRESOLVED = new ResolvedName(Collections.singletonList("Unresolved"));
@@ -41,24 +45,6 @@ public String getNamespace() {
4145
public String getLastName() {
4246
return names.get(names.size() - 1);
4347
}
44-
45-
@Override
46-
public boolean equals(Object o) {
47-
if (this == o) return true;
48-
if (o == null || getClass() != o.getClass()) return false;
49-
ResolvedName that = (ResolvedName) o;
50-
return names.equals(that.names);
51-
}
52-
53-
@Override
54-
public int hashCode() {
55-
return Objects.hash(names);
56-
}
57-
58-
@Override
59-
public String toString() {
60-
return "ResolvedName{" + "names=" + names + '}';
61-
}
6248
}
6349

6450
public static final HashMap<String, ResolvedName> resolvedNames = new HashMap<>();

src/main/java/com/probejs/util/PUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import com.google.gson.JsonObject;
44
import lombok.val;
55

6+
import java.io.IOException;
7+
import java.io.Writer;
68
import java.lang.reflect.Field;
79
import java.util.Collections;
10+
import java.util.List;
811
import java.util.Map;
912

10-
public class PUtil {
13+
public abstract class PUtil {
1114

1215
private static final String[] INDENT_CACHE;
1316

@@ -18,6 +21,13 @@ public class PUtil {
1821
}
1922
}
2023

24+
public static void writeLines(Writer writer, List<String> lines) throws IOException {
25+
for (val line : lines) {
26+
writer.write(line);
27+
writer.write('\n');
28+
}
29+
}
30+
2131
public static void mergeJsonRecursive(JsonObject base, JsonObject addition) {
2232
for (val entry : addition.entrySet()) {
2333
val key = entry.getKey();
Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package com.probejs.util;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.ToString;
6+
7+
@AllArgsConstructor
8+
@EqualsAndHashCode
9+
@ToString
310
public class Pair<F, S> {
411

512
private final F first;
613
private final S second;
714

8-
public Pair(F first, S second) {
9-
this.first = first;
10-
this.second = second;
11-
}
12-
1315
/**
1416
* get the first element in such pair
1517
*/
@@ -23,26 +25,4 @@ public F first() {
2325
public S second() {
2426
return second;
2527
}
26-
27-
@Override
28-
public String toString() {
29-
return String.format("Pair{first=%s, second=%s}", this.first, this.second);
30-
}
31-
32-
@Override
33-
public int hashCode() {
34-
return java.util.Objects.hash(this.first, this.second);
35-
}
36-
37-
@Override
38-
public boolean equals(Object obj) {
39-
if (this == obj) {
40-
return true;
41-
}
42-
if (!(obj instanceof Pair)) {
43-
return false;
44-
}
45-
Pair<?, ?> p = (Pair<?, ?>) obj;
46-
return this.first.equals(p.first) && this.second.equals(p.second);
47-
}
4828
}

0 commit comments

Comments
 (0)