/
ActionRow.java
167 lines (154 loc) · 4.86 KB
/
ActionRow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.interactions.components;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.interactions.ButtonImpl;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;
/**
* One row of interactive message components.
*
* @see Component
*/
public class ActionRow implements ComponentLayout, Iterable<Component>
{
private final List<Component> components = new ArrayList<>();
private ActionRow() {}
/**
* Load ActionRow from serialized representation.
* <br>Inverse of {@link #toData()}.
*
* @param data
* Serialized version of an action row
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided data is not a valid action row
* @throws IllegalArgumentException
* If the data is null or the type is not 1
*
* @return ActionRow instance
*/
@Nonnull
public static ActionRow fromData(@Nonnull DataObject data)
{
Checks.notNull(data, "Data");
ActionRow row = new ActionRow();
if (data.getInt("type", 0) != 1)
throw new IllegalArgumentException("Data has incorrect type. Expected: 1 Found: " + data.getInt("type"));
data.getArray("components")
.stream(DataArray::getObject)
.map(obj -> {
switch (Component.Type.fromKey(obj.getInt("type")))
{
case BUTTON:
return new ButtonImpl(obj);
default:
return null;
}
})
.filter(Objects::nonNull)
.forEach(row.components::add);
return row;
}
/**
* Create one row of up to 5 interactive message {@link Component components}.
*
* @param components
* The components for this action row
*
* @throws IllegalArgumentException
* If anything is null, empty, or more than 5 components are provided
*
* @return The action row
*/
@Nonnull
public static ActionRow of(@Nonnull Collection<? extends Component> components)
{
Checks.noneNull(components, "Components");
return of(components.toArray(new Component[0]));
}
/**
* Create one row of up to 5 interactive message {@link Component components}.
*
* @param components
* The components for this action row
*
* @throws IllegalArgumentException
* If anything is null, empty, or more than 5 components are provided
*
* @return The action row
*/
@Nonnull
public static ActionRow of(@Nonnull Component... components)
{
Checks.noneNull(components, "Components");
Checks.check(components.length <= 5, "Can only have 5 components per action row!");
Checks.check(components.length > 0, "Cannot have empty row!");
ActionRow row = new ActionRow();
Collections.addAll(row.components, components);
return row;
}
/**
* Mutable list of components in this ActionRow.
* <br>ActionRows should not be empty and are limited to 5 buttons.
*
* @return The list of components
*/
@Nonnull
@Override
public List<Component> getComponents()
{
return components;
}
/**
* Immutable list of buttons in this ActionRow.
*
* @return Immutable list of buttons
*/
@Nonnull
@Override
public List<Button> getButtons()
{
return Collections.unmodifiableList(
getComponents().stream()
.filter(Button.class::isInstance)
.map(Button.class::cast)
.collect(Collectors.toList()));
}
@Nonnull
@Override
public Type getType()
{
return Type.ACTION_ROW;
}
@Nonnull
@Override
public DataObject toData()
{
return DataObject.empty()
.put("type", 1)
.put("components", DataArray.fromCollection(components));
}
@Nonnull
@Override
public Iterator<Component> iterator()
{
return components.iterator();
}
}