1616
1717package net .dv8tion .jda .api .components ;
1818
19+ import net .dv8tion .jda .api .components .thumbnail .Thumbnail ;
1920import net .dv8tion .jda .api .components .tree .ComponentTree ;
20- import net .dv8tion .jda .api .components .tree .MessageComponentTree ;
21- import net .dv8tion .jda .api .components .tree .ModalComponentTree ;
21+ import net .dv8tion .jda .api .components .utils .ComponentDeserializer ;
22+ import net .dv8tion .jda .api .components .utils .ComponentSerializer ;
23+ import net .dv8tion .jda .api .utils .FileUpload ;
2224import net .dv8tion .jda .api .utils .data .DataArray ;
2325import net .dv8tion .jda .api .utils .data .DataObject ;
24- import net .dv8tion .jda .internal .components .UnknownComponentImpl ;
25- import net .dv8tion .jda .internal .components .actionrow .ActionRowImpl ;
26- import net .dv8tion .jda .internal .components .buttons .ButtonImpl ;
27- import net .dv8tion .jda .internal .components .container .ContainerImpl ;
28- import net .dv8tion .jda .internal .components .filedisplay .FileDisplayImpl ;
29- import net .dv8tion .jda .internal .components .label .LabelImpl ;
30- import net .dv8tion .jda .internal .components .mediagallery .MediaGalleryImpl ;
31- import net .dv8tion .jda .internal .components .section .SectionImpl ;
32- import net .dv8tion .jda .internal .components .selections .EntitySelectMenuImpl ;
33- import net .dv8tion .jda .internal .components .selections .StringSelectMenuImpl ;
34- import net .dv8tion .jda .internal .components .separator .SeparatorImpl ;
35- import net .dv8tion .jda .internal .components .textdisplay .TextDisplayImpl ;
36- import net .dv8tion .jda .internal .components .textinput .TextInputImpl ;
37- import net .dv8tion .jda .internal .components .thumbnail .ThumbnailImpl ;
38- import net .dv8tion .jda .internal .components .utils .ComponentsUtil ;
39- import net .dv8tion .jda .internal .utils .Checks ;
4026
4127import javax .annotation .Nonnull ;
4228import java .util .List ;
43- import java .util .function .Function ;
4429import java .util .stream .Collectors ;
4530
31+ import static net .dv8tion .jda .internal .entities .EntityBuilder .DEFAULT_COMPONENT_DESERIALIZER ;
32+
4633/**
4734 * Utility class for components.
35+ *
36+ * <p>This utility builds on {@link ComponentDeserializer} to deserialize components.
37+ * The implementation provided by {@link ComponentDeserializer} allows to deserialize components with implicit {@link FileUpload FileUploads} like {@link Thumbnail}.
38+ *
39+ * @see ComponentDeserializer
40+ * @see ComponentSerializer
4841 */
4942public class Components
5043{
@@ -55,58 +48,17 @@ public class Components
5548 * @param data
5649 * The {@link DataArray} to create the component tree from
5750 *
58- * @return A {@link ComponentTree} representing the provided data
59- *
6051 * @throws IllegalArgumentException
6152 * If the provided data is {@code null}, or the component is not of type {@link T}
53+ *
54+ * @return A {@link ComponentTree} representing the provided data
55+ *
56+ * @see ComponentDeserializer#deserializeAs(Class, DataObject)
6257 */
6358 @ Nonnull
6459 public static <T extends Component > T parseComponent (@ Nonnull Class <T > componentType , @ Nonnull DataObject data )
6560 {
66- Checks .notNull (componentType , "Component type" );
67- Checks .notNull (data , "Data" );
68-
69- final IComponentUnion component = parseComponent (data );
70- return ComponentsUtil .safeUnionCastWithUnknownType ("component" , component , componentType );
71- }
72-
73- @ Nonnull
74- private static IComponentUnion parseComponent (@ Nonnull DataObject data )
75- {
76- switch (Component .Type .fromKey (data .getInt ("type" )))
77- {
78- case ACTION_ROW :
79- return new ActionRowImpl (data );
80- case BUTTON :
81- return new ButtonImpl (data );
82- case STRING_SELECT :
83- return new StringSelectMenuImpl (data );
84- case TEXT_INPUT :
85- return new TextInputImpl (data );
86- case USER_SELECT :
87- case ROLE_SELECT :
88- case MENTIONABLE_SELECT :
89- case CHANNEL_SELECT :
90- return new EntitySelectMenuImpl (data );
91- case SECTION :
92- return new SectionImpl (data );
93- case TEXT_DISPLAY :
94- return new TextDisplayImpl (data );
95- case THUMBNAIL :
96- return new ThumbnailImpl (data );
97- case MEDIA_GALLERY :
98- return new MediaGalleryImpl (data );
99- case FILE_DISPLAY :
100- return new FileDisplayImpl (data );
101- case SEPARATOR :
102- return new SeparatorImpl (data );
103- case CONTAINER :
104- return new ContainerImpl (data );
105- case LABEL :
106- return new LabelImpl (data );
107- default :
108- return new UnknownComponentImpl (data );
109- }
61+ return DEFAULT_COMPONENT_DESERIALIZER .deserializeAs (componentType , data );
11062 }
11163
11264 /**
@@ -116,18 +68,17 @@ private static IComponentUnion parseComponent(@Nonnull DataObject data)
11668 * @param data
11769 * The {@link DataArray} to create the components from
11870 *
119- * @return A {@link List} of {@link T} representing the provided data
120- *
12171 * @throws IllegalArgumentException
12272 * If the provided data is {@code null}, or one of the components is not of type {@link T}
73+ *
74+ * @return A {@link List} of {@link T} representing the provided data
75+ *
76+ * @see ComponentDeserializer#deserializeAs(Class, DataArray)
12377 */
12478 @ Nonnull
12579 public static <T extends Component > List <T > parseComponents (@ Nonnull Class <T > componentType , @ Nonnull DataArray data )
12680 {
127- Checks .notNull (componentType , "Component type" );
128- Checks .notNull (data , "Data" );
129-
130- return parseTo (componentType , data , Function .identity ());
81+ return DEFAULT_COMPONENT_DESERIALIZER .deserializeAs (componentType , data ).collect (Collectors .toList ());
13182 }
13283
13384 /**
@@ -137,41 +88,18 @@ public static <T extends Component> List<T> parseComponents(@Nonnull Class<T> co
13788 * @param data
13889 * The {@link DataArray} to create the component tree from
13990 *
140- * @return A {@link ComponentTree} representing the provided data
141- *
14291 * @throws IllegalArgumentException
14392 * If the provided data is {@code null}, or one of the components is not compatible.
14493 * @throws UnsupportedOperationException
14594 * If the provided tree type is not supported
14695 *
96+ * @return A {@link ComponentTree} representing the provided data
97+ *
98+ * @see ComponentDeserializer#deserializeAsTree(Class, DataArray)
14799 */
148100 @ Nonnull
149- @ SuppressWarnings ("unchecked" )
150101 public static <T extends ComponentTree <?>> T parseTree (@ Nonnull Class <T > treeType , @ Nonnull DataArray data )
151102 {
152- Checks .notNull (treeType , "Tree type" );
153- Checks .notNull (data , "Data" );
154-
155- if (MessageComponentTree .class .isAssignableFrom (treeType ))
156- return (T ) parseTo (MessageTopLevelComponentUnion .class , data , MessageComponentTree ::of );
157- else if (ModalComponentTree .class .isAssignableFrom (treeType ))
158- return (T ) parseTo (ModalTopLevelComponentUnion .class , data , ModalComponentTree ::of );
159- else if (ComponentTree .class .isAssignableFrom (treeType ))
160- return (T ) parseTo (IComponentUnion .class , data , ComponentTree ::of );
161- else
162- throw new UnsupportedOperationException ("Cannot deserialize to tree of type " + treeType .getName ());
163- }
164-
165- @ Nonnull
166- private static <R , U extends Component > R parseTo (
167- @ Nonnull Class <U > topLevelComponentType ,
168- @ Nonnull DataArray data ,
169- @ Nonnull Function <List <U >, R > treeFactory
170- )
171- {
172- final List <U > components = data .stream (DataArray ::getObject )
173- .map (obj -> parseComponent (topLevelComponentType , obj ))
174- .collect (Collectors .toList ());
175- return treeFactory .apply (components );
103+ return DEFAULT_COMPONENT_DESERIALIZER .deserializeAsTree (treeType , data );
176104 }
177105}
0 commit comments