Skip to content

Commit d791ad8

Browse files
committed
added property builder
1 parent 609da80 commit d791ad8

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<groupId>net.azalealibrary</groupId>
1515
<artifactId>configuration</artifactId>
16-
<version>1.0.7</version>
16+
<version>1.0.8</version>
1717

1818
<repositories>
1919
<repository>

src/main/java/net/azalealibrary/configuration/property/ConfigurableProperty.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ public abstract class ConfigurableProperty<T, P> {
2222
private final Supplier<P> defaultValue;
2323
private @Nullable P value;
2424

25-
@SafeVarargs
26-
protected ConfigurableProperty(PropertyType<T> type, Supplier<P> defaultValue, String name, String description, Consumer<P> callback, AssignmentPolicy<T>... policies) {
25+
protected ConfigurableProperty(PropertyType<T> type, Supplier<P> defaultValue, String name, String description, Consumer<P> callback, List<AssignmentPolicy<T>> policies) {
2726
this.type = type;
2827
this.name = name;
2928
this.description = description;
3029
this.defaultValue = defaultValue;
3130
this.callback = callback;
32-
this.policies = List.of(policies);
31+
this.policies = policies;
3332
}
3433

3534
public PropertyType<T> getType() {

src/main/java/net/azalealibrary/configuration/property/ListProperty.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,7 @@ public final class ListProperty<T> extends ConfigurableProperty<T, List<T>> {
1919
private static final String REMOVE = "remove";
2020
private static final String REPLACE = "replace";
2121

22-
@SafeVarargs
23-
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, AssignmentPolicy<T>... policies) {
24-
this(type, defaultValue, name, v -> {}, policies);
25-
}
26-
27-
@SafeVarargs
28-
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, Consumer<List<T>> callback, AssignmentPolicy<T>... policies) {
29-
this(type, defaultValue, name, name, callback, policies);
30-
}
31-
32-
@SafeVarargs
33-
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, String description, Consumer<List<T>> callback, AssignmentPolicy<T>... policies) {
22+
private ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, String description, Consumer<List<T>> callback, List<AssignmentPolicy<T>> policies) {
3423
super(type, defaultValue, name, description, callback, policies);
3524
set(defaultValue.get());
3625
}
@@ -103,4 +92,43 @@ public boolean equals(Object object) {
10392
public String toString() {
10493
return isSet() ? get().stream().map(getType()::print).collect(Collectors.joining(", ")) : "<empty>";
10594
}
95+
96+
public static <T> Builder<T> create(String name, PropertyType<T> type, Supplier<List<T>> defaultValue) {
97+
return new Builder<>(name, type, defaultValue);
98+
}
99+
100+
public static final class Builder<T> {
101+
102+
private final String name;
103+
private final PropertyType<T> type;
104+
private final Supplier<List<T>> defaultValue;
105+
private final List<AssignmentPolicy<T>> policies = new ArrayList<>();
106+
private String description;
107+
private Consumer<List<T>> callback;
108+
109+
private Builder(String name, PropertyType<T> type, Supplier<List<T>> defaultValue) {
110+
this.name = name;
111+
this.type = type;
112+
this.defaultValue = defaultValue;
113+
}
114+
115+
public Builder<T> addPolicy(AssignmentPolicy<T> policy) {
116+
this.policies.add(policy);
117+
return this;
118+
}
119+
120+
public Builder<T> addDescription(String description) {
121+
this.description = description;
122+
return this;
123+
}
124+
125+
public Builder<T> onChange(Consumer<List<T>> callback) {
126+
this.callback = callback;
127+
return this;
128+
}
129+
130+
public ListProperty<T> done() {
131+
return new ListProperty<>(type, defaultValue, name, description, callback, policies);
132+
}
133+
}
106134
}

src/main/java/net/azalealibrary/configuration/property/Property.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,15 @@
55
import org.bukkit.configuration.ConfigurationSection;
66

77
import javax.annotation.Nonnull;
8+
import java.util.ArrayList;
9+
import java.util.List;
810
import java.util.Optional;
911
import java.util.function.Consumer;
1012
import java.util.function.Supplier;
1113

1214
public final class Property<T> extends ConfigurableProperty<T, T> {
1315

14-
@SafeVarargs
15-
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, AssignmentPolicy<T>... policies) {
16-
this(type, defaultValue, name, name, v -> {}, policies);
17-
}
18-
19-
@SafeVarargs
20-
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, Consumer<T> callback, AssignmentPolicy<T>... policies) {
21-
this(type, defaultValue, name, name, callback, policies);
22-
}
23-
24-
@SafeVarargs
25-
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, String description, Consumer<T> callback, AssignmentPolicy<T>... policies) {
16+
private Property(PropertyType<T> type, Supplier<T> defaultValue, String name, String description, Consumer<T> callback, List<AssignmentPolicy<T>> policies) {
2617
super(type, defaultValue, name, description, callback, policies);
2718
}
2819

@@ -54,4 +45,43 @@ public boolean equals(Object object) {
5445
public String toString() {
5546
return isSet() ? getType().print(get()) : "<empty>";
5647
}
48+
49+
public static <T> Builder<T> create(String name, PropertyType<T> type, Supplier<T> defaultValue) {
50+
return new Builder<>(name, type, defaultValue);
51+
}
52+
53+
public static final class Builder<T> {
54+
55+
private final String name;
56+
private final PropertyType<T> type;
57+
private final Supplier<T> defaultValue;
58+
private final List<AssignmentPolicy<T>> policies = new ArrayList<>();
59+
private String description;
60+
private Consumer<T> callback;
61+
62+
private Builder(String name, PropertyType<T> type, Supplier<T> defaultValue) {
63+
this.name = name;
64+
this.type = type;
65+
this.defaultValue = defaultValue;
66+
}
67+
68+
public Builder<T> addPolicy(AssignmentPolicy<T> policy) {
69+
this.policies.add(policy);
70+
return this;
71+
}
72+
73+
public Builder<T> addDescription(String description) {
74+
this.description = description;
75+
return this;
76+
}
77+
78+
public Builder<T> onChange(Consumer<T> callback) {
79+
this.callback = callback;
80+
return this;
81+
}
82+
83+
public Property<T> done() {
84+
return new Property<>(type, defaultValue, name, description, callback, policies);
85+
}
86+
}
5787
}

0 commit comments

Comments
 (0)