|
| 1 | +package xyz.srnyx.javautilities.objects; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | + |
| 6 | +import xyz.srnyx.javautilities.parents.Stringable; |
| 7 | + |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.StringJoiner; |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * A utility class for managing arguments |
| 14 | + */ |
| 15 | +public class Arguments extends Stringable { |
| 16 | + /** |
| 17 | + * The arguments |
| 18 | + */ |
| 19 | + @Nullable public final String[] args; |
| 20 | + |
| 21 | + /** |
| 22 | + * Construct a new {@link Arguments} with the specified arguments |
| 23 | + * |
| 24 | + * @param args the arguments |
| 25 | + */ |
| 26 | + public Arguments(@Nullable String[] args) { |
| 27 | + this.args = args; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns if the specified {@link #args} index is equal to <b>any</b> of the specified strings (case-insensitive) |
| 32 | + * |
| 33 | + * @param index the argument index |
| 34 | + * @param strings the strings to compare to |
| 35 | + * |
| 36 | + * @return {@code true} if the specified {@link #args} index is equal to <b>any</b> of the specified strings (case-insensitive) |
| 37 | + */ |
| 38 | + public boolean argEquals(int index, @Nullable String... strings) { |
| 39 | + if (args == null || args.length <= index) return false; |
| 40 | + final String arg = args[index]; |
| 41 | + if (arg == null) return false; |
| 42 | + for (final String string : strings) if (arg.equalsIgnoreCase(string)) return true; |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the argument at the specified index |
| 48 | + * |
| 49 | + * @param index the argument index |
| 50 | + * |
| 51 | + * @return the argument at the specified index |
| 52 | + */ |
| 53 | + @Nullable |
| 54 | + public String getArgument(int index) { |
| 55 | + return args == null || args.length <= index ? null : args[index]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Gets the argument at the specified index as an {@link Optional} |
| 60 | + * |
| 61 | + * @param index the argument index |
| 62 | + * |
| 63 | + * @return the argument at the specified index as an {@link Optional} |
| 64 | + */ |
| 65 | + @NotNull |
| 66 | + public Optional<String> getArgumentOptional(int index) { |
| 67 | + return Optional.ofNullable(getArgument(index)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets multiple arguments joined together starting from the specified index and ending at the specified index (if too high, it will stop at the last argument) |
| 72 | + * <br>If no arguments are found, it returns an empty string |
| 73 | + * |
| 74 | + * @param start the starting index |
| 75 | + * @param end the ending index |
| 76 | + * |
| 77 | + * @return the arguments joined together |
| 78 | + */ |
| 79 | + @NotNull |
| 80 | + public String getArgumentsJoined(int start, int end) { |
| 81 | + if (args == null || args.length <= start) return ""; |
| 82 | + final StringJoiner joiner = new StringJoiner(" "); |
| 83 | + for (int i = start; i < args.length && i < end; i++) joiner.add(args[i]); |
| 84 | + return joiner.toString(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets multiple arguments joined together starting from the specified index |
| 89 | + * <br>If no arguments are found, it returns an empty string |
| 90 | + * |
| 91 | + * @param start the starting index |
| 92 | + * |
| 93 | + * @return the arguments joined together |
| 94 | + */ |
| 95 | + @NotNull |
| 96 | + public String getArgumentsJoined(int start) { |
| 97 | + return args == null ? "" : getArgumentsJoined(start, args.length); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets all arguments joined together |
| 102 | + * <br>If no arguments are found, it returns an empty string |
| 103 | + * |
| 104 | + * @return all arguments joined together |
| 105 | + */ |
| 106 | + @NotNull |
| 107 | + public String getArgumentsJoined() { |
| 108 | + return args == null ? "" : String.join(" ", args); |
| 109 | + } |
| 110 | +} |
0 commit comments