TelnetHandler.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.handler;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. * @author UnAfraid
  20. */
  21. public class TelnetHandler implements IHandler<ITelnetHandler, String>
  22. {
  23. private final Map<String, ITelnetHandler> _telnetHandlers;
  24. protected TelnetHandler()
  25. {
  26. _telnetHandlers = new HashMap<>();
  27. }
  28. @Override
  29. public void registerHandler(ITelnetHandler handler)
  30. {
  31. for (String element : handler.getCommandList())
  32. {
  33. _telnetHandlers.put(element.toLowerCase(), handler);
  34. }
  35. }
  36. @Override
  37. public synchronized void removeHandler(ITelnetHandler handler)
  38. {
  39. for (String element : handler.getCommandList())
  40. {
  41. _telnetHandlers.remove(element.toLowerCase());
  42. }
  43. }
  44. @Override
  45. public ITelnetHandler getHandler(String command)
  46. {
  47. if (command.contains(" "))
  48. {
  49. command = command.substring(0, command.indexOf(" "));
  50. }
  51. return _telnetHandlers.get(command.toLowerCase());
  52. }
  53. @Override
  54. public int size()
  55. {
  56. return _telnetHandlers.size();
  57. }
  58. public static TelnetHandler getInstance()
  59. {
  60. return SingletonHolder._instance;
  61. }
  62. private static class SingletonHolder
  63. {
  64. protected static final TelnetHandler _instance = new TelnetHandler();
  65. }
  66. }