Java IRC bot
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.

CalcCommand.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  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.md87.charliebravo.commands;
  23. import com.dmdirc.addons.calc.Evaluator;
  24. import com.dmdirc.addons.calc.Lexer;
  25. import com.dmdirc.addons.calc.Parser;
  26. import com.md87.charliebravo.Command;
  27. import com.md87.charliebravo.Followup;
  28. import com.md87.charliebravo.InputHandler;
  29. import com.md87.charliebravo.Response;
  30. /**
  31. *
  32. * @author chris
  33. */
  34. public class CalcCommand implements Command {
  35. public void execute(InputHandler handler, Response response, String line) throws Exception {
  36. final Lexer lexer = new Lexer(line);
  37. final Parser parser = new Parser(lexer);
  38. final Evaluator evaluator = new Evaluator(parser.parse());
  39. final Number result = evaluator.evaluate();
  40. response.sendMessage(line + " = " + result);
  41. response.addFollowup(new AddFollowup(result.toString()));
  42. response.addFollowup(new SubtractFollowup(result.toString()));
  43. response.addFollowup(new MultiplyByFollowup(result.toString()));
  44. response.addFollowup(new DivideByFollowup(result.toString()));
  45. }
  46. protected static class AddFollowup implements Followup {
  47. private final String result;
  48. public AddFollowup(String result) {
  49. this.result = result;
  50. }
  51. public boolean matches(String line) {
  52. return line.startsWith("add ");
  53. }
  54. public void execute(InputHandler handler, Response response, String line) throws Exception {
  55. new CalcCommand().execute(handler, response, result + " + " + line.substring(4));
  56. }
  57. }
  58. protected static class SubtractFollowup implements Followup {
  59. private final String result;
  60. public SubtractFollowup(String result) {
  61. this.result = result;
  62. }
  63. public boolean matches(String line) {
  64. return line.startsWith("subtract ");
  65. }
  66. public void execute(InputHandler handler, Response response, String line) throws Exception {
  67. new CalcCommand().execute(handler, response, result + " - " + line.substring(9));
  68. }
  69. }
  70. protected static class MultiplyByFollowup implements Followup {
  71. private final String result;
  72. public MultiplyByFollowup(String result) {
  73. this.result = result;
  74. }
  75. public boolean matches(String line) {
  76. return line.startsWith("multiply by ");
  77. }
  78. public void execute(InputHandler handler, Response response, String line) throws Exception {
  79. new CalcCommand().execute(handler, response, result + " * " + line.substring(13));
  80. }
  81. }
  82. protected static class DivideByFollowup implements Followup {
  83. private final String result;
  84. public DivideByFollowup(String result) {
  85. this.result = result;
  86. }
  87. public boolean matches(String line) {
  88. return line.startsWith("divide by ");
  89. }
  90. public void execute(InputHandler handler, Response response, String line) throws Exception {
  91. new CalcCommand().execute(handler, response, result + " / " + line.substring(10));
  92. }
  93. }
  94. }