001//
002// MIT License
003//
004// Copyright (c) 2021 Alexander Söderberg & Contributors
005//
006// Permission is hereby granted, free of charge, to any person obtaining a copy
007// of this software and associated documentation files (the "Software"), to deal
008// in the Software without restriction, including without limitation the rights
009// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010// copies of the Software, and to permit persons to whom the Software is
011// furnished to do so, subject to the following conditions:
012//
013// The above copyright notice and this permission notice shall be included in all
014// copies or substantial portions of the Software.
015//
016// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022// SOFTWARE.
023//
024package cloud.commandframework.arguments.standard;
025
026import cloud.commandframework.ArgumentDescription;
027import cloud.commandframework.arguments.CommandArgument;
028import cloud.commandframework.arguments.parser.ArgumentParseResult;
029import cloud.commandframework.arguments.parser.ArgumentParser;
030import cloud.commandframework.context.CommandContext;
031import io.leangen.geantyref.TypeToken;
032import org.checkerframework.checker.nullness.qual.NonNull;
033import org.checkerframework.checker.nullness.qual.Nullable;
034
035import java.util.List;
036import java.util.Queue;
037import java.util.function.BiFunction;
038
039/**
040 * This is a command argument type that essentially mimics {@link StringArgument#greedy(String)},
041 * but then splits the input string into a string array. The input string will be split at
042 * every blank space.
043 *
044 * @param <C> Command sender type
045 */
046public final class StringArrayArgument<C> extends CommandArgument<C, String[]> {
047
048    private StringArrayArgument(
049            final boolean required,
050            final @NonNull String name,
051            final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
052            final @NonNull ArgumentDescription defaultDescription
053    ) {
054        super(
055                required,
056                name,
057                new StringArrayParser<>(),
058                "",
059                TypeToken.get(String[].class),
060                suggestionsProvider,
061                defaultDescription
062        );
063    }
064
065    /**
066     * Create a new required string array argument
067     *
068     * @param name                Argument name
069     * @param suggestionsProvider Suggestions provider
070     * @param <C>                 Command sender type
071     * @return Created argument
072     */
073    public static <C> @NonNull StringArrayArgument<C> of(
074            final @NonNull String name,
075            final @NonNull BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
076    ) {
077        return new StringArrayArgument<>(
078                true,
079                name,
080                suggestionsProvider,
081                ArgumentDescription.empty()
082        );
083    }
084
085    /**
086     * Create a new optional string array argument
087     *
088     * @param name                Argument name
089     * @param suggestionsProvider Suggestions provider
090     * @param <C>                 Command sender type
091     * @return Created argument
092     */
093    public static <C> @NonNull StringArrayArgument<C> optional(
094            final @NonNull String name,
095            final @NonNull BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
096    ) {
097        return new StringArrayArgument<>(
098                false,
099                name,
100                suggestionsProvider,
101                ArgumentDescription.empty()
102        );
103    }
104
105
106    /**
107     * Parser that parses input into a string array
108     *
109     * @param <C> Command sender type
110     */
111    public static final class StringArrayParser<C> implements ArgumentParser<C, String[]> {
112
113        @Override
114        public @NonNull ArgumentParseResult<String @NonNull []> parse(
115                final @NonNull CommandContext<@NonNull C> commandContext,
116                final @NonNull Queue<@NonNull String> inputQueue
117        ) {
118            final String[] result = new String[inputQueue.size()];
119            for (int i = 0; i < result.length; i++) {
120                result[i] = inputQueue.remove();
121            }
122            return ArgumentParseResult.success(result);
123        }
124
125    }
126
127}