Skip to content

Commit 0aa6c30

Browse files
committed
Make setter methods return fluent interface
1 parent 6bbd957 commit 0aa6c30

4 files changed

Lines changed: 87 additions & 128 deletions

File tree

‎src/main/java/org/glavo/nbt/tag/CompoundTag.java‎

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import java.io.IOException;
2727
import java.util.*;
28-
import java.util.function.Consumer;
2928

3029
/// A [parent tag][ParentTag] that holds an unordered collection of named tags.
3130
///
@@ -68,25 +67,6 @@ public CompoundTag setName(String name) throws IllegalArgumentException {
6867
return this;
6968
}
7069

71-
/// Executes the given action on this compound tag.
72-
///
73-
/// This method is useful for configuring the tag.
74-
///
75-
/// For example:
76-
///
77-
/// ```java
78-
/// var parent = new CompoundTag();
79-
/// parent.put("sub", new CompoundTag().tap(t -> {
80-
/// t.setByte( "byte", (byte) 1);
81-
/// t.setShort("short", (short) 2);
82-
/// }));
83-
/// ```
84-
@Contract("_ -> this")
85-
public CompoundTag tap(Consumer<? super CompoundTag> action) {
86-
action.accept(this);
87-
return this;
88-
}
89-
9070
/// Returns the subtag with the given name, or `null` if no such subtag exists.
9171
@Contract(pure = true)
9272
public @Nullable Tag get(String name) {
@@ -293,7 +273,7 @@ public CompoundTag addTag(Tag tag) {
293273
///
294274
/// If another tag with the same name already exists, the old tag will be removed.
295275
@Contract(mutates = "this,param2")
296-
public void addTag(String name, Tag tag) {
276+
public CompoundTag addTag(String name, Tag tag) {
297277
@SuppressWarnings("unchecked")
298278
var oldParent = (NBTParent<Tag>) tag.getParent();
299279
if (oldParent != null) {
@@ -302,6 +282,7 @@ public void addTag(String name, Tag tag) {
302282

303283
tag.setName0(name);
304284
addTag(tag);
285+
return this;
305286
}
306287

307288
@SuppressWarnings("unchecked")
@@ -328,119 +309,131 @@ private <T extends Tag> T getOrPutTag(String name, TagType<T> tagType) throws Il
328309
/// If no byte tag with the given name exists, a new byte tag will be created.
329310
///
330311
/// @throws IllegalStateException if the tag with the given name exists but is not a byte tag.
331-
@Contract(mutates = "this")
332-
public void setByte(String name, byte value) throws IllegalStateException {
312+
@Contract(value = "_, _ -> this", mutates = "this")
313+
public CompoundTag setByte(String name, byte value) throws IllegalStateException {
333314
getOrPutTag(name, TagType.BYTE).set(value);
315+
return this;
334316
}
335317

336318
/// Sets the boolean value of the [ByteTag] with the given name.
337319
///
338320
/// If no byte tag with the given name exists, a new byte tag will be created.
339321
///
340322
/// @throws IllegalStateException if the tag with the given name exists but is not a byte tag.
341-
@Contract(mutates = "this")
342-
public void setBoolean(String name, boolean value) throws IllegalStateException {
323+
@Contract(value = "_, _ -> this", mutates = "this")
324+
public CompoundTag setBoolean(String name, boolean value) throws IllegalStateException {
343325
getOrPutTag(name, TagType.BYTE).setBoolean(value);
326+
return this;
344327
}
345328

346329
/// Sets the short value of the [ShortTag] with the given name.
347330
///
348331
/// If no short tag with the given name exists, a new short tag will be created.
349332
///
350333
/// @throws IllegalStateException if the tag with the given name exists but is not a short tag.
351-
@Contract(mutates = "this")
352-
public void setShort(String name, short value) throws IllegalStateException {
334+
@Contract(value = "_, _ -> this", mutates = "this")
335+
public CompoundTag setShort(String name, short value) throws IllegalStateException {
353336
getOrPutTag(name, TagType.SHORT).set(value);
337+
return this;
354338
}
355339

356340
/// Sets the int value of the [IntTag] with the given name.
357341
///
358342
/// If no int tag with the given name exists, a new int tag will be created.
359343
///
360344
/// @throws IllegalStateException if the tag with the given name exists but is not an int tag.
361-
@Contract(mutates = "this")
362-
public void setInt(String name, int value) throws IllegalStateException {
345+
@Contract(value = "_, _ -> this", mutates = "this")
346+
public CompoundTag setInt(String name, int value) throws IllegalStateException {
363347
getOrPutTag(name, TagType.INT).set(value);
348+
return this;
364349
}
365350

366351
/// Sets the long value of the [LongTag] with the given name.
367352
///
368353
/// If no long tag with the given name exists, a new long tag will be created.
369354
///
370355
/// @throws IllegalStateException if the tag with the given name exists but is not a long tag.
371-
@Contract(mutates = "this")
372-
public void setLong(String name, long value) throws IllegalStateException {
356+
@Contract(value = "_, _ -> this", mutates = "this")
357+
public CompoundTag setLong(String name, long value) throws IllegalStateException {
373358
getOrPutTag(name, TagType.LONG).set(value);
359+
return this;
374360
}
375361

376362
/// Sets the float value of the [FloatTag] with the given name.
377363
///
378364
/// If no float tag with the given name exists, a new float tag will be created.
379365
///
380366
/// @throws IllegalStateException if the tag with the given name exists but is not a float tag.
381-
@Contract(mutates = "this")
382-
public void setFloat(String name, float value) throws IllegalStateException {
367+
@Contract(value = "_, _ -> this", mutates = "this")
368+
public CompoundTag setFloat(String name, float value) throws IllegalStateException {
383369
getOrPutTag(name, TagType.FLOAT).set(value);
370+
return this;
384371
}
385372

386373
/// Sets the double value of the [DoubleTag] with the given name.
387374
///
388375
/// If no double tag with the given name exists, a new double tag will be created.
389376
///
390377
/// @throws IllegalStateException if the tag with the given name exists but is not a double tag.
391-
@Contract(mutates = "this")
392-
public void setDouble(String name, double value) throws IllegalStateException {
378+
@Contract(value = "_, _ -> this", mutates = "this")
379+
public CompoundTag setDouble(String name, double value) throws IllegalStateException {
393380
getOrPutTag(name, TagType.DOUBLE).set(value);
381+
return this;
394382
}
395383

396384
/// Sets the string value of the [StringTag] with the given name.
397385
///
398386
/// If no string tag with the given name exists, a new string tag will be created.
399387
///
400388
/// @throws IllegalStateException if the tag with the given name exists but is not a string tag.
401-
@Contract(mutates = "this")
402-
public void setString(String name, String value) throws IllegalStateException {
389+
@Contract(value = "_, _ -> this", mutates = "this")
390+
public CompoundTag setString(String name, String value) throws IllegalStateException {
403391
getOrPutTag(name, TagType.STRING).set(value);
392+
return this;
404393
}
405394

406395
/// Sets the byte array value of the [ByteArrayTag] with the given name.
407396
///
408397
/// If no byte array tag with the given name exists, a new byte array tag will be created.
409398
///
410399
/// @throws IllegalStateException if the tag with the given name exists but is not a byte array tag.
411-
@Contract(mutates = "this")
412-
public void setByteArray(String name, byte[] value) throws IllegalStateException {
400+
@Contract(value = "_, _ -> this", mutates = "this")
401+
public CompoundTag setByteArray(String name, byte[] value) throws IllegalStateException {
413402
getOrPutTag(name, TagType.BYTE_ARRAY).setAll(value);
403+
return this;
414404
}
415405

416406
/// Sets the int array value of the [IntArrayTag] with the given name.
417407
///
418408
/// If no int array tag with the given name exists, a new int array tag will be created.
419409
///
420410
/// @throws IllegalStateException if the tag with the given name exists but is not an int array tag.
421-
@Contract(mutates = "this")
422-
public void setIntArray(String name, int[] value) throws IllegalStateException {
411+
@Contract(value = "_, _ -> this", mutates = "this")
412+
public CompoundTag setIntArray(String name, int[] value) throws IllegalStateException {
423413
getOrPutTag(name, TagType.INT_ARRAY).setAll(value);
414+
return this;
424415
}
425416

426417
/// Sets the long array value of the [LongArrayTag] with the given name.
427418
///
428419
/// If no long array tag with the given name exists, a new long array tag will be created.
429420
///
430421
/// @throws IllegalStateException if the tag with the given name exists but is not a long array tag.
431-
@Contract(mutates = "this")
432-
public void setLongArray(String name, long[] value) throws IllegalStateException {
422+
@Contract(value = "_, _ -> this", mutates = "this")
423+
public CompoundTag setLongArray(String name, long[] value) throws IllegalStateException {
433424
getOrPutTag(name, TagType.LONG_ARRAY).setAll(value);
425+
return this;
434426
}
435427

436428
/// Set the UUID value of the [IntArrayTag] with the given name.
437429
///
438430
/// If no int array tag with the given name exists, a new int array tag will be created.
439431
///
440432
/// @throws IllegalStateException if the tag with the given name exists but is not an int array tag.
441-
@Contract(mutates = "this")
442-
public void setUUID(String name, UUID value) {
433+
@Contract(value = "_, _ -> this", mutates = "this")
434+
public CompoundTag setUUID(String name, UUID value) throws IllegalStateException {
443435
getOrPutTag(name, TagType.INT_ARRAY).setUUID(value);
436+
return this;
444437
}
445438

446439
@Override

‎src/main/java/org/glavo/nbt/tag/ListTag.java‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.IOException;
2424
import java.util.Arrays;
2525
import java.util.Objects;
26-
import java.util.function.Consumer;
2726

2827
/// A [parent tag][ParentTag] that holds an ordered collection of isomorphic unnamed tags.
2928
///
@@ -86,25 +85,6 @@ public ListTag<T> setName(String name) throws IllegalArgumentException {
8685
return this;
8786
}
8887

89-
/// Executes the given action on this list tag.
90-
///
91-
/// This method is useful for configuring the tag.
92-
///
93-
/// For example:
94-
///
95-
/// ```java
96-
/// var parent = new CompoundTag();
97-
/// parent.put("sub", new ListTag<>(TagType.BYTE).tap(t -> {
98-
/// t.addTag(new ByteTag("", (byte) 1));
99-
/// t.addTag(new ByteTag("", (byte) 2));
100-
/// }));
101-
/// ```
102-
@Contract("_ -> this")
103-
public ListTag<T> tap(Consumer<? super ListTag<T>> action) {
104-
action.accept(this);
105-
return this;
106-
}
107-
10888
/// Returns the type of the elements in the list.
10989
///
11090
/// If the element type is `null`, the list is empty.

‎src/test/java/org/glavo/nbt/NBTPathTest.java‎

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,30 @@
3131
final class NBTPathTest {
3232

3333
private static CompoundTag createSampleRoot() {
34-
return new CompoundTag().tap(root -> {
35-
root.addTag("player", new CompoundTag().tap(player2 -> {
36-
player2.setString("name", "Alex");
37-
player2.setInt("score", 42);
38-
}));
39-
40-
root.addTag("players", new ListTag<>(TagType.COMPOUND).tap(players -> {
41-
players.addTag(new CompoundTag().tap(player1 -> {
42-
player1.setString("name", "Alex");
43-
player1.setInt("score", 10);
44-
}));
45-
players.addTag(new CompoundTag().tap(player -> {
46-
player.setString("name", "Steve");
47-
player.setInt("score", 20);
48-
}));
49-
}));
50-
51-
root.addTag("profiles", new ListTag<>(TagType.COMPOUND).tap(profiles -> {
52-
profiles.addTag(new CompoundTag());
53-
profiles.addTag(new CompoundTag().tap(profile -> profile.setString("name", "Alex")));
54-
profiles.addTag(new CompoundTag().tap(profile -> {
55-
profile.setString("name", "Alex");
56-
profile.setInt("score", 10);
57-
}));
58-
}));
59-
60-
root.addTag("empty", new CompoundTag());
61-
root.addTag("metadata", new CompoundTag().tap(metadata -> {
62-
metadata.setString("display name", "Alex The Great");
63-
metadata.setString("quote\"key", "Escaped");
64-
}));
65-
root.setString("player.name", "literal");
66-
root.setIntArray("numbers", new int[]{3, 5, 8});
67-
root.setLongArray("longs", new long[]{13L, 21L});
68-
});
34+
return new CompoundTag()
35+
.addTag("player", new CompoundTag()
36+
.setString("name", "Alex")
37+
.setInt("score", 42))
38+
.addTag("players", new ListTag<>(TagType.COMPOUND)
39+
.addTag(new CompoundTag()
40+
.setString("name", "Alex")
41+
.setInt("score", 10))
42+
.addTag(new CompoundTag()
43+
.setString("name", "Steve")
44+
.setInt("score", 20)))
45+
.addTag("profiles", new ListTag<>(TagType.COMPOUND)
46+
.addTag(new CompoundTag())
47+
.addTag(new CompoundTag().setString("name", "Alex"))
48+
.addTag(new CompoundTag()
49+
.setString("name", "Alex")
50+
.setInt("score", 10)))
51+
.addTag("empty", new CompoundTag())
52+
.addTag("metadata", new CompoundTag()
53+
.setString("display name", "Alex The Great")
54+
.setString("quote\"key", "Escaped"))
55+
.setString("player.name", "literal")
56+
.setIntArray("numbers", new int[]{3, 5, 8})
57+
.setLongArray("longs", new long[]{13L, 21L});
6958
}
7059

7160
@Test

‎src/test/java/org/glavo/nbt/internal/snbt/SNBTParserTest.java‎

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,32 @@ void testNextTag() {
135135
assertEquals(new LongArrayTag(new long[]{1L, 2L, 3L}), parseTag("[L; 1L, 2L, 3L, ]"));
136136

137137
assertEquals(new ListTag<>(), parseTag("[]"));
138-
assertEquals(new ListTag<>().tap(l -> {
139-
l.addTag(new StringTag("Hello"));
140-
l.addTag(new StringTag("Glavo"));
141-
}), parseTag("[Hello, 'Glavo']"));
142-
assertEquals(new ListTag<>().tap(l -> {
143-
l.addTag(new StringTag("Hello"));
144-
l.addTag(new StringTag("Glavo"));
145-
}), parseTag("[Hello, 'Glavo',]"));
138+
assertEquals(new ListTag<>()
139+
.addTag(new StringTag("Hello"))
140+
.addTag(new StringTag("Glavo")),
141+
parseTag("[Hello, 'Glavo']"));
142+
assertEquals(new ListTag<>()
143+
.addTag(new StringTag("Hello"))
144+
.addTag(new StringTag("Glavo")),
145+
parseTag("[Hello, 'Glavo',]"));
146146

147147
assertEquals(new CompoundTag(), parseTag("{}"));
148-
assertEquals(new CompoundTag().tap(c -> {
149-
c.setString("name", "Glavo");
150-
c.setInt("age", 9);
151-
c.setUUID("id", UUID.fromString("01bb64c8-2a2f-4509-931b-366513bfb5a8"));
152-
c.setBoolean("bool", true);
153-
c.addTag("nested", new CompoundTag().tap(c2 -> {
154-
c2.addTag("very", new CompoundTag().tap(c3 -> {
155-
c3.addTag("deep", new CompoundTag().tap(c4 -> {
156-
c4.setString("structure", "ok");
157-
}));
158-
}));
159-
}));
160-
}), parseTag("""
161-
{
162-
name: 'Glavo',
163-
age: 9,
164-
id: uuid('01bb64c8-2a2f-4509-931b-366513bfb5a8'),
165-
bool: true,
166-
nested: {very:{deep:{structure:"ok"}}}
167-
}"""));
148+
assertEquals(new CompoundTag()
149+
.setString("name", "Glavo")
150+
.setInt("age", 9)
151+
.setUUID("id", UUID.fromString("01bb64c8-2a2f-4509-931b-366513bfb5a8"))
152+
.setBoolean("bool", true)
153+
.addTag("nested", new CompoundTag()
154+
.addTag("very", new CompoundTag()
155+
.addTag("deep", new CompoundTag()
156+
.setString("structure", "ok")))),
157+
parseTag("""
158+
{
159+
name: 'Glavo',
160+
age: 9,
161+
id: uuid('01bb64c8-2a2f-4509-931b-366513bfb5a8'),
162+
bool: true,
163+
nested: {very:{deep:{structure:"ok"}}}
164+
}"""));
168165
}
169166
}

0 commit comments

Comments
 (0)