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.

Alias.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2017 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.aliases;
  23. import com.dmdirc.commandparser.CommandInfo;
  24. import com.dmdirc.commandparser.CommandType;
  25. import com.google.common.base.MoreObjects;
  26. import com.google.common.base.Objects;
  27. /**
  28. * Describes a user-defined command alias.
  29. */
  30. public class Alias implements CommandInfo {
  31. /** The type of this alias. */
  32. private final CommandType type;
  33. /** The name of the alias. */
  34. private final String name;
  35. /** The minimum number of arguments this alias requires. */
  36. private final int minArguments;
  37. /** The command string to substitute arguments into. */
  38. private final String substitution;
  39. public Alias(
  40. final CommandType type,
  41. final String name,
  42. final int minArguments,
  43. final String substitution) {
  44. this.type = type;
  45. this.name = name;
  46. this.minArguments = minArguments;
  47. this.substitution = substitution;
  48. }
  49. @Override
  50. public String getName() {
  51. return name;
  52. }
  53. @Override
  54. public String getHelp() {
  55. return name + " - alias for " + substitution.split(" ", 2)[0];
  56. }
  57. public int getMinArguments() {
  58. return minArguments;
  59. }
  60. public String getSubstitution() {
  61. return substitution;
  62. }
  63. @Override
  64. public CommandType getType() {
  65. return type;
  66. }
  67. @Override
  68. public boolean equals(final Object object) {
  69. if (!(object instanceof Alias)) {
  70. return false;
  71. }
  72. final Alias alias = (Alias) object;
  73. return Objects.equal(getName(), alias.getName())
  74. && Objects.equal(getMinArguments(), alias.getMinArguments())
  75. && Objects.equal(getSubstitution(), alias.getSubstitution())
  76. && Objects.equal(getType(), alias.getType());
  77. }
  78. @Override
  79. public int hashCode() {
  80. return Objects.hashCode(name, minArguments, substitution, type);
  81. }
  82. @Override
  83. public String toString() {
  84. return MoreObjects.toStringHelper(this)
  85. .add("name", name)
  86. .add("minargs", minArguments)
  87. .add("substitution", substitution)
  88. .add("type", type)
  89. .toString();
  90. }
  91. }