Skip to content

Commit cefe864

Browse files
committed
default methods for collections
1 parent 0e5ee13 commit cefe864

File tree

7 files changed

+46
-63
lines changed

7 files changed

+46
-63
lines changed

core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<parent>

core/src/main/java/io/github/projectunified/unidialog/core/dialog/Dialog.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,41 @@ public interface Dialog<I, BB extends DialogBodyBuilder<I>, IB extends DialogInp
6767
T body(Consumer<BB> bodyBuilder);
6868

6969
/**
70-
* Set the body of the dialog using a collection of body builders
70+
* Set the input for the dialog
7171
*
72-
* @param bodyBuilders the collection of body builders for the dialog
72+
* @param key the key for the input
73+
* @param inputBuilder the input builder for the dialog
7374
* @return the dialog itself for method chaining
7475
*/
75-
T body(Collection<Consumer<BB>> bodyBuilders);
76+
T input(String key, Consumer<IB> inputBuilder);
7677

7778
/**
78-
* Set the input for the dialog
79+
* Set the body of the dialog using a collection of body builders
7980
*
80-
* @param key the key for the input
81-
* @param inputBuilder the input builder for the dialog
81+
* @param bodyBuilders the collection of body builders for the dialog
8282
* @return the dialog itself for method chaining
8383
*/
84-
T input(String key, Consumer<IB> inputBuilder);
84+
default T body(Collection<Consumer<BB>> bodyBuilders) {
85+
for (Consumer<BB> bodyBuilder : bodyBuilders) {
86+
body(bodyBuilder);
87+
}
88+
//noinspection unchecked
89+
return (T) this;
90+
}
8591

8692
/**
8793
* Set the input for the dialog using a map of input builders
8894
*
8995
* @param inputBuilders the map of input builders for the dialog
9096
* @return the dialog itself for method chaining
9197
*/
92-
T input(Map<String, Consumer<IB>> inputBuilders);
98+
default T input(Map<String, Consumer<IB>> inputBuilders) {
99+
for (Map.Entry<String, Consumer<IB>> entry : inputBuilders.entrySet()) {
100+
input(entry.getKey(), entry.getValue());
101+
}
102+
//noinspection unchecked
103+
return (T) this;
104+
}
93105

94106
/**
95107
* Create an opener for the dialog

core/src/main/java/io/github/projectunified/unidialog/core/dialog/MultiActionDialog.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,24 @@ public interface MultiActionDialog<I, BB extends DialogBodyBuilder<I>, IB extend
3535
T action(Consumer<AB> action);
3636

3737
/**
38-
* Add multiple actions to the dialog
38+
* Set the exit action for the dialog
3939
*
40-
* @param actions a collection of actions to be performed
40+
* @param action the action to be performed when exiting the dialog
4141
* @return the dialog itself for method chaining
4242
*/
43-
T action(Collection<Consumer<AB>> actions);
43+
T exitAction(@Nullable Consumer<AB> action);
4444

4545
/**
46-
* Set the exit action for the dialog
46+
* Add multiple actions to the dialog
4747
*
48-
* @param action the action to be performed when exiting the dialog
48+
* @param actions a collection of actions to be performed
4949
* @return the dialog itself for method chaining
5050
*/
51-
T exitAction(@Nullable Consumer<AB> action);
51+
default T action(Collection<Consumer<AB>> actions) {
52+
for (Consumer<AB> action : actions) {
53+
action(action);
54+
}
55+
//noinspection unchecked
56+
return (T) this;
57+
}
5258
}

packetevents/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<parent>

packetevents/src/main/java/io/github/projectunified/unidialog/packetevents/dialog/PEDialog.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import net.kyori.adventure.text.Component;
1818
import org.jetbrains.annotations.Nullable;
1919

20-
import java.util.*;
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.UUID;
2124
import java.util.function.Consumer;
2225
import java.util.function.Function;
2326

@@ -101,19 +104,6 @@ public T body(Consumer<PEDialogBodyBuilder> bodyBuilder) {
101104
return (T) this;
102105
}
103106

104-
@Override
105-
public T body(Collection<Consumer<PEDialogBodyBuilder>> bodyBuilders) {
106-
if (bodies == null) {
107-
bodies = new ArrayList<>();
108-
}
109-
for (Consumer<PEDialogBodyBuilder> bodyBuilder : bodyBuilders) {
110-
PEDialogBodyBuilder builder = new PEDialogBodyBuilder();
111-
bodyBuilder.accept(builder);
112-
bodies.add(builder.getDialogBody());
113-
}
114-
return (T) this;
115-
}
116-
117107
@Override
118108
public T input(String key, Consumer<PEDialogInputBuilder> inputBuilder) {
119109
if (inputs == null) {
@@ -126,22 +116,6 @@ public T input(String key, Consumer<PEDialogInputBuilder> inputBuilder) {
126116
return (T) this;
127117
}
128118

129-
@Override
130-
public T input(Map<String, Consumer<PEDialogInputBuilder>> inputBuilders) {
131-
if (inputs == null) {
132-
inputs = new ArrayList<>();
133-
}
134-
for (Map.Entry<String, Consumer<PEDialogInputBuilder>> entry : inputBuilders.entrySet()) {
135-
String key = entry.getKey();
136-
Consumer<PEDialogInputBuilder> inputBuilder = entry.getValue();
137-
PEDialogInputBuilder builder = new PEDialogInputBuilder();
138-
inputBuilder.accept(builder);
139-
InputControl inputControl = builder.getInput();
140-
inputs.add(new Input(key, inputControl));
141-
}
142-
return (T) this;
143-
}
144-
145119
protected ActionButton getAction(Consumer<PEDialogActionBuilder> action) {
146120
PEDialogActionBuilder actionBuilder = new PEDialogActionBuilder(defaultNamespace);
147121
action.accept(actionBuilder);

packetevents/src/main/java/io/github/projectunified/unidialog/packetevents/dialog/PEMultiActionDialog.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import io.github.projectunified.unidialog.packetevents.input.PEDialogInputBuilder;
1111
import org.jetbrains.annotations.Nullable;
1212

13-
import java.util.*;
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.List;
16+
import java.util.UUID;
1417
import java.util.function.Consumer;
1518
import java.util.function.Function;
1619

@@ -39,18 +42,6 @@ public PEMultiActionDialog action(Consumer<PEDialogActionBuilder> action) {
3942
return this;
4043
}
4144

42-
@Override
43-
public PEMultiActionDialog action(Collection<Consumer<PEDialogActionBuilder>> actions) {
44-
if (this.actions == null) {
45-
this.actions = new ArrayList<>();
46-
}
47-
for (Consumer<PEDialogActionBuilder> action : actions) {
48-
ActionButton actionButton = getAction(action);
49-
this.actions.add(actionButton);
50-
}
51-
return this;
52-
}
53-
5445
@Override
5546
public PEMultiActionDialog exitAction(@Nullable Consumer<PEDialogActionBuilder> action) {
5647
this.exitAction = action == null ? null : getAction(action);

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

0 commit comments

Comments
 (0)