Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CommandFlag.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2012 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
  30. * or disabled initially, can specify a number of immediate arguments (which
  31. * must follow the flag directly), a number of delayed arguments (which
  32. * are specified after any other flags), and a list of other flags that
  33. * the use of this one causes to be enabled or disabled.
  34. *
  35. * @since 0.6.5
  36. * @see CommandFlagHandler
  37. */
  38. public class CommandFlag {
  39. /** The name of the flag. */
  40. private final String name;
  41. /** The list of flags that become enabled if this one is used. */
  42. private final List<CommandFlag> enables = new LinkedList<CommandFlag>();
  43. /** The list of flags that become disabled if this one is used. */
  44. private final List<CommandFlag> disables = new LinkedList<CommandFlag>();
  45. /** The number of args expected following this flag. */
  46. private final int immediateArgs;
  47. /** The number of args expected after flags are finished. */
  48. private final int delayedArgs;
  49. /** The initial state of this flag. */
  50. private final boolean enabled;
  51. /**
  52. * Creates a new enabled flag with the specified name and no arguments.
  53. *
  54. * @param name The name of this flag
  55. */
  56. public CommandFlag(final String name) {
  57. this(name, true);
  58. }
  59. /**
  60. * Creates a new flag with the specified name and no arguments.
  61. *
  62. * @param name The name of this flag
  63. * @param enabled Whether or not this flag is initially enabled
  64. */
  65. public CommandFlag(final String name, final boolean enabled) {
  66. this(name, enabled, 0, 0);
  67. }
  68. /**
  69. * Creates a new flag with the specified name and the specified number
  70. * of arguments. Flags may only use immediate OR delayed arguments, not
  71. * both.
  72. *
  73. * @param name The name of this flag
  74. * @param enabled Whether or not this flag is initially enabled
  75. * @param immediateArgs The number of immediate arguments
  76. * @param delayedArgs The number of delayed arguments
  77. */
  78. public CommandFlag(final String name, final boolean enabled,
  79. final int immediateArgs, final int delayedArgs) {
  80. this.name = name;
  81. this.immediateArgs = immediateArgs;
  82. this.delayedArgs = delayedArgs;
  83. this.enabled = enabled;
  84. if (delayedArgs > 0 && immediateArgs > 0) {
  85. throw new IllegalArgumentException("May not use both delayed and immediate arguments");
  86. }
  87. }
  88. /**
  89. * Indicates that the specified flags will be enabled if this flag
  90. * is used.
  91. *
  92. * @param enabled The flags which will be enabled
  93. */
  94. public void addEnabled(final CommandFlag ... enabled) {
  95. this.enables.addAll(Arrays.asList(enabled));
  96. }
  97. /**
  98. * Indicates that the specified flags will be disabled if this flag
  99. * is used.
  100. *
  101. * @param disabled The flags which will be disabled
  102. */
  103. public void addDisabled(final CommandFlag ... disabled) {
  104. this.disables.addAll(Arrays.asList(disabled));
  105. }
  106. /**
  107. * Retrieves the number of delayed arguments required by this flag.
  108. *
  109. * @return The number of delayed arguments
  110. */
  111. public int getDelayedArgs() {
  112. return delayedArgs;
  113. }
  114. /**
  115. * Retrieves the set of flags which become disabled if this one is used.
  116. *
  117. * @return A set of flags disabled by the use of this one
  118. */
  119. public Collection<CommandFlag> getDisables() {
  120. return Collections.unmodifiableCollection(disables);
  121. }
  122. /**
  123. * Determines whether or not this flag is enabled initially.
  124. *
  125. * @return The initial enabled/disabled state of this action
  126. */
  127. public boolean isEnabled() {
  128. return enabled;
  129. }
  130. /**
  131. * Retrieves the set of flags which become enabled if this one is used.
  132. *
  133. * @return A set of flags enabled by the use of this one
  134. */
  135. public Collection<CommandFlag> getEnables() {
  136. return Collections.unmodifiableCollection(enables);
  137. }
  138. /**
  139. * Retrieves the number of immediate arguments required by this flag.
  140. *
  141. * @return The number of immediate arguments
  142. */
  143. public int getImmediateArgs() {
  144. return immediateArgs;
  145. }
  146. /**
  147. * Retrieves the name of this flag.
  148. *
  149. * @return This flag's name
  150. */
  151. public String getName() {
  152. return name;
  153. }
  154. }