You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandFlag.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.commands.flags;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. /**
  24. * Describes a flag that may be used in a command. Each flag may be enabled or disabled initially,
  25. * can specify a number of immediate arguments (which must follow the flag directly), a number of
  26. * delayed arguments (which are specified after any other flags), and a list of other flags that the
  27. * use of this one causes to be enabled or disabled.
  28. *
  29. * @since 0.6.5
  30. * @see CommandFlagHandler
  31. */
  32. public class CommandFlag {
  33. /** The name of the flag. */
  34. private final String name;
  35. /** The list of flags that become enabled if this one is used. */
  36. private final List<CommandFlag> enables = new LinkedList<>();
  37. /** The list of flags that become disabled if this one is used. */
  38. private final List<CommandFlag> disables = new LinkedList<>();
  39. /** The number of args expected following this flag. */
  40. private final int immediateArgs;
  41. /** The number of args expected after flags are finished. */
  42. private final int delayedArgs;
  43. /** The initial state of this flag. */
  44. private final boolean enabled;
  45. /**
  46. * Creates a new enabled flag with the specified name and no arguments.
  47. *
  48. * @param name The name of this flag
  49. */
  50. public CommandFlag(final String name) {
  51. this(name, true);
  52. }
  53. /**
  54. * Creates a new flag with the specified name and no arguments.
  55. *
  56. * @param name The name of this flag
  57. * @param enabled Whether or not this flag is initially enabled
  58. */
  59. public CommandFlag(final String name, final boolean enabled) {
  60. this(name, enabled, 0, 0);
  61. }
  62. /**
  63. * Creates a new flag with the specified name and the specified number of arguments. Flags may
  64. * only use immediate OR delayed arguments, not both.
  65. *
  66. * @param name The name of this flag
  67. * @param enabled Whether or not this flag is initially enabled
  68. * @param immediateArgs The number of immediate arguments
  69. * @param delayedArgs The number of delayed arguments
  70. */
  71. public CommandFlag(final String name, final boolean enabled,
  72. final int immediateArgs, final int delayedArgs) {
  73. this.name = name;
  74. this.immediateArgs = immediateArgs;
  75. this.delayedArgs = delayedArgs;
  76. this.enabled = enabled;
  77. if (delayedArgs > 0 && immediateArgs > 0) {
  78. throw new IllegalArgumentException("May not use both delayed and immediate arguments");
  79. }
  80. }
  81. /**
  82. * Indicates that the specified flags will be enabled if this flag is used.
  83. *
  84. * @param enabled The flags which will be enabled
  85. */
  86. public void addEnabled(final CommandFlag... enabled) {
  87. enables.addAll(Arrays.asList(enabled));
  88. }
  89. /**
  90. * Indicates that the specified flags will be disabled if this flag is used.
  91. *
  92. * @param disabled The flags which will be disabled
  93. */
  94. public void addDisabled(final CommandFlag... disabled) {
  95. disables.addAll(Arrays.asList(disabled));
  96. }
  97. /**
  98. * Retrieves the number of delayed arguments required by this flag.
  99. *
  100. * @return The number of delayed arguments
  101. */
  102. public int getDelayedArgs() {
  103. return delayedArgs;
  104. }
  105. /**
  106. * Retrieves the set of flags which become disabled if this one is used.
  107. *
  108. * @return A set of flags disabled by the use of this one
  109. */
  110. public Collection<CommandFlag> getDisables() {
  111. return Collections.unmodifiableCollection(disables);
  112. }
  113. /**
  114. * Determines whether or not this flag is enabled initially.
  115. *
  116. * @return The initial enabled/disabled state of this action
  117. */
  118. public boolean isEnabled() {
  119. return enabled;
  120. }
  121. /**
  122. * Retrieves the set of flags which become enabled if this one is used.
  123. *
  124. * @return A set of flags enabled by the use of this one
  125. */
  126. public Collection<CommandFlag> getEnables() {
  127. return Collections.unmodifiableCollection(enables);
  128. }
  129. /**
  130. * Retrieves the number of immediate arguments required by this flag.
  131. *
  132. * @return The number of immediate arguments
  133. */
  134. public int getImmediateArgs() {
  135. return immediateArgs;
  136. }
  137. /**
  138. * Retrieves the name of this flag.
  139. *
  140. * @return This flag's name
  141. */
  142. public String getName() {
  143. return name;
  144. }
  145. }