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.

AliasFactory.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.aliases;
  18. import com.dmdirc.commandparser.CommandType;
  19. import com.dmdirc.interfaces.CommandController;
  20. import com.google.common.base.CharMatcher;
  21. import com.google.common.base.Joiner;
  22. import com.google.common.base.Splitter;
  23. import com.google.common.base.Strings;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. import javax.inject.Inject;
  28. import javax.inject.Singleton;
  29. import static com.google.common.base.Preconditions.checkArgument;
  30. import static com.google.common.base.Preconditions.checkNotNull;
  31. /**
  32. * Handles the creation of new aliases.
  33. */
  34. @Singleton
  35. public class AliasFactory {
  36. /** The controller to use to trim command chars. */
  37. private final CommandController commandController;
  38. @Inject
  39. public AliasFactory(final CommandController commandController) {
  40. this.commandController = commandController;
  41. }
  42. /**
  43. * Creates a new alias.
  44. *
  45. * @param name The name of the alias to be created.
  46. * @param minArguments The minimum number of arguments the alias requires.
  47. * @param substitution The command to substitute when the alias is executed.
  48. *
  49. * @return A new alias with the given attributes.
  50. */
  51. public Alias createAlias(final String name, final int minArguments, final String substitution) {
  52. checkNotNull(name);
  53. checkArgument(!Strings.isNullOrEmpty(name));
  54. checkArgument(minArguments >= 0);
  55. checkNotNull(substitution);
  56. // TODO: Infer command type from the substituted command
  57. return new Alias(CommandType.TYPE_GLOBAL, removeCommandChar(name), minArguments,
  58. removeCommandChar(substitution));
  59. }
  60. private String removeCommandChar(final String input) {
  61. // TODO: This could be moved into the command controller, or a utility class.
  62. // CommandArguments does something similar.
  63. final List<String> lines = Splitter.on(CharMatcher.anyOf("\r\n"))
  64. .omitEmptyStrings()
  65. .splitToList(input);
  66. final List<String> trimmedLines = new ArrayList<>(lines.size());
  67. trimmedLines.addAll(lines.stream()
  68. .map(line -> line.charAt(0) == commandController.getCommandChar() ?
  69. line.length() > 1 && line.charAt(1) == commandController.getSilenceChar() ?
  70. line.substring(2) : line.substring(1) : line)
  71. .collect(Collectors.toList()));
  72. return Joiner.on("\r\n").join(trimmedLines);
  73. }
  74. }