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

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