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

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