3636import cloud .commandframework .keys .CloudKeyHolder ;
3737import cloud .commandframework .keys .SimpleCloudKey ;
3838import cloud .commandframework .permission .CommandPermission ;
39+ import cloud .commandframework .types .tuples .Pair ;
3940import java .util .Collections ;
4041import java .util .HashMap ;
4142import java .util .LinkedList ;
43+ import java .util .List ;
4244import java .util .Map ;
45+ import java .util .NoSuchElementException ;
4346import java .util .Optional ;
4447import java .util .function .Function ;
4548import java .util .function .Supplier ;
49+ import java .util .stream .Collectors ;
4650import org .apiguardian .api .API ;
4751import org .checkerframework .checker .nullness .qual .NonNull ;
4852import org .checkerframework .checker .nullness .qual .Nullable ;
5660public class CommandContext <C > {
5761
5862 private final CaptionVariableReplacementHandler captionVariableReplacementHandler ;
59- private final Map < CommandArgument <C , ?>, ArgumentTiming > argumentTimings = new HashMap <>();
63+ private final List < ArgumentContext <C , ?>> argumentContexts = new LinkedList <>();
6064 private final FlagContext flagContext = FlagContext .create ();
6165 private final Map <CloudKey <?>, Object > internalStorage = new HashMap <>();
6266 private final C commandSender ;
@@ -603,20 +607,103 @@ public <T> T computeIfAbsent(
603607 *
604608 * @param argument Argument
605609 * @return Created timing instance
610+ *
611+ * @deprecated This has been replaced by {@link #createArgumentContext(CommandArgument)}
606612 */
613+ @ API (status = API .Status .DEPRECATED , since = "1.9.0" )
614+ @ Deprecated
607615 public @ NonNull ArgumentTiming createTiming (final @ NonNull CommandArgument <C , ?> argument ) {
608- final ArgumentTiming argumentTiming = new ArgumentTiming ();
609- this .argumentTimings .put (argument , argumentTiming );
610- return argumentTiming ;
616+ return new ArgumentTiming ();
611617 }
612618
613619 /**
614620 * Get an immutable view of the argument timings map
615621 *
616622 * @return Argument timings
623+ * @deprecated Replaced with {@link #argumentContexts()}
617624 */
625+ @ API (status = API .Status .DEPRECATED , since = "1.9.0" )
626+ @ Deprecated
618627 public @ NonNull Map <CommandArgument <@ NonNull C , @ NonNull ?>, ArgumentTiming > getArgumentTimings () {
619- return Collections .unmodifiableMap (this .argumentTimings );
628+ return this .argumentContexts .stream ()
629+ .map (context -> Pair .of (
630+ context .argument (),
631+ new ArgumentTiming (
632+ context .startTime (),
633+ context .endTime (),
634+ context .success ()
635+ )
636+ )
637+ ).collect (Collectors .toMap (Pair ::getFirst , Pair ::getSecond ));
638+ }
639+
640+ /**
641+ * Create an argument context instance for the given argument
642+ *
643+ * @param argument the argument
644+ * @return the created context
645+ * @param <T> the type of the argument
646+ * @since 1.9.0
647+ */
648+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
649+ public <T > @ NonNull ArgumentContext <C , T > createArgumentContext (final @ NonNull CommandArgument <C , T > argument ) {
650+ final ArgumentContext <C , T > argumentContext = new ArgumentContext <>(argument );
651+ this .argumentContexts .add (argumentContext );
652+ return argumentContext ;
653+ }
654+
655+ /**
656+ * Returns the context for the given argument
657+ *
658+ * @param argument the argument
659+ * @return the context
660+ * @param <T> the type of the argument
661+ * @since 1.9.0
662+ */
663+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
664+ @ SuppressWarnings ("unchecked" )
665+ public <T > @ NonNull ArgumentContext <C , T > argumentContext (final @ NonNull CommandArgument <C , T > argument ) {
666+ return this .argumentContexts .stream ().filter (context -> context .argument ().equals (argument ))
667+ .findFirst ()
668+ .map (context -> (ArgumentContext <C , T >) context )
669+ .orElseThrow (NoSuchElementException ::new );
670+ }
671+
672+ /**
673+ * Returns the context for the argument at the given position
674+ *
675+ * @param position the position
676+ * @return the context
677+ * @since 1.9.0
678+ */
679+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
680+ public @ NonNull ArgumentContext <C , ?> argumentContext (final int position ) {
681+ return this .argumentContexts .get (position );
682+ }
683+
684+ /**
685+ * Return the context for the argument with the given name.
686+ *
687+ * @param name the name
688+ * @return the context
689+ * @since 1.9.0
690+ */
691+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
692+ public @ NonNull ArgumentContext <C , ?> argumentContext (final String name ) {
693+ return this .argumentContexts .stream ().filter (context -> context .argument ().getName ().equals (name ))
694+ .findFirst ()
695+ .orElseThrow (NoSuchElementException ::new );
696+ }
697+
698+ /**
699+ * Return an unmodifiable view of the stored argument contexts
700+ *
701+ * @return the contexts
702+ * @since 1.9.0
703+ */
704+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
705+ public @ NonNull List <@ NonNull ArgumentContext <@ NonNull C , @ NonNull ?>> argumentContexts () {
706+ return Collections .unmodifiableList (this .argumentContexts );
620707 }
621708
622709 /**
@@ -680,16 +767,19 @@ public void setCurrentArgument(final @Nullable CommandArgument<C, ?> argument) {
680767 * parsed.
681768 * <p>
682769 * The times are measured in nanoseconds.
770+ *
771+ * @deprecated Superseded by {@link ArgumentContext}
683772 */
684- @ API (status = API .Status .STABLE )
773+ @ Deprecated
774+ @ API (status = API .Status .DEPRECATED , since = "1.9.0" )
685775 public static final class ArgumentTiming {
686776
687777 private long start ;
688778 private long end ;
689779 private boolean success ;
690780
691781 /**
692- * Created a new argument timing instance
782+ * Creates a new argument timing instance
693783 *
694784 * @param start Start time (in nanoseconds)
695785 * @param end End time (in nanoseconds)
@@ -702,7 +792,7 @@ public ArgumentTiming(final long start, final long end, final boolean success) {
702792 }
703793
704794 /**
705- * Created a new argument timing instance without an end time
795+ * Creates a new argument timing instance without an end time
706796 *
707797 * @param start Start time (in nanoseconds)
708798 */
@@ -712,7 +802,7 @@ public ArgumentTiming(final long start) {
712802 }
713803
714804 /**
715- * Created a new argument timing instance
805+ * Creates a new argument timing instance
716806 */
717807 public ArgumentTiming () {
718808 this (-1 , -1 , false );
0 commit comments