Skip to content

Commit 4cc54f0

Browse files
author
Alexander Söderberg
committed
feature: introduce CommandFactory
1 parent 62c2686 commit 4cc54f0

3 files changed

Lines changed: 64 additions & 9 deletions

File tree

cloud-core/src/main/java/cloud/commandframework/CommandBean.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import cloud.commandframework.context.CommandContext;
2727
import cloud.commandframework.execution.CommandExecutionHandler;
2828
import cloud.commandframework.meta.CommandMeta;
29+
import java.util.Collections;
30+
import java.util.List;
2931
import org.apiguardian.api.API;
3032
import org.checkerframework.checker.nullness.qual.NonNull;
3133

@@ -37,14 +39,15 @@
3739
* to implement your command handler. You may also provide custom handler when {@link #configure(Command.Builder)} is invoked.
3840
* <p>
3941
* Information about the command, such as aliases, should be provided by {@link #properties()}. The command bean may be
40-
* registered to the command manager by using {@link CommandManager#command(CommandBean)}. This will invoke {@link #configure(Command.Builder)}
42+
* registered to the command manager by using {@link CommandManager#command(CommandFactory)}. This will invoke
43+
* {@link #configure(Command.Builder)}
4144
* where you may configure the command. The command meta may be configured by overriding {@link #meta()}.
4245
*
4346
* @param <C> the command sender type
4447
* @since 2.0.0
4548
*/
4649
@API(status = API.Status.STABLE, since = "2.0.0")
47-
public abstract class CommandBean<C> implements CommandExecutionHandler<C> {
50+
public abstract class CommandBean<C> implements CommandExecutionHandler<C>, CommandFactory<C> {
4851

4952
protected CommandBean() {
5053
}
@@ -57,14 +60,15 @@ protected CommandBean() {
5760
* @param commandManager the command manager
5861
* @return the constructed command
5962
*/
60-
public @NonNull Command<C> constructCommand(final @NonNull CommandManager<C> commandManager) {
63+
@Override
64+
public @NonNull List<@NonNull Command<C>> createCommand(final @NonNull CommandManager<C> commandManager) {
6165
final Command.Builder<C> builder = commandManager.commandBuilder(
6266
this.properties().name(),
6367
this.properties().aliases(),
6468
this.meta()
6569
).handler(this);
6670
this.configure(builder);
67-
return builder.build();
71+
return Collections.singletonList(builder.build());
6872
}
6973

7074
/**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2022 Alexander Söderberg & Contributors
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
package cloud.commandframework;
25+
26+
import java.util.List;
27+
import org.apiguardian.api.API;
28+
import org.checkerframework.checker.nullness.qual.NonNull;
29+
30+
/**
31+
* Factory producing command instances
32+
*
33+
* @param <C> the command sender type
34+
*/
35+
@API(status = API.Status.STABLE, since = "2.0.0")
36+
@FunctionalInterface
37+
public interface CommandFactory<C> {
38+
39+
/**
40+
* Creates commands using the given {@code commandManager}. Each invocation produces unique instances of the commands.
41+
* <p>
42+
* This method has no side effects, meaning that the created commands will not be automatically registered to the
43+
* {@link CommandManager command manager}.
44+
*
45+
* @param commandManager the command manager
46+
* @return the created commands
47+
*/
48+
@NonNull List<@NonNull Command<C>> createCommand(@NonNull CommandManager<C> commandManager);
49+
}

cloud-core/src/main/java/cloud/commandframework/CommandManager.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,23 @@ protected CommandManager(
246246
}
247247

248248
/**
249-
* Register a new command to the command manager and insert it into the underlying command tree. The command will be
250-
* forwarded to the {@link CommandRegistrationHandler} and will, depending on the platform, be forwarded to the platform.
249+
* Creates a command using the given {@code commandFactory} and inserts it into the underlying command tree. The command
250+
* will be forwarded to the {@link CommandRegistrationHandler} and will, depending on the platform, be forwarded to the
251+
* platform.
251252
* <p>
252253
* Different command manager implementations have different requirements for the command registration. It is possible
253254
* that a command manager may only allow registration during certain stages of the application lifetime. Read the platform
254255
* command manager documentation to find out more about your particular platform
255256
*
256-
* @param commandBean the command bean to register
257+
* @param commandFactory the command factory to register
257258
* @return The command manager instance. This is returned so that these method calls may be chained. This will always
258259
* return {@code this}
259260
* @since 2.0.0
260261
*/
261262
@API(status = API.Status.STABLE, since = "2.0.0")
262-
public @NonNull @This CommandManager<C> command(final @NonNull CommandBean<C> commandBean) {
263-
return this.command(commandBean.constructCommand(this));
263+
public @NonNull @This CommandManager<C> command(final @NonNull CommandFactory<C> commandFactory) {
264+
commandFactory.createCommand(this).forEach(this::command);
265+
return this;
264266
}
265267

266268
/**

0 commit comments

Comments
 (0)