AdminCommandHandler.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 AdminCommandHandler implements IHandler<IAdminCommandHandler, String>
  22. {
  23. private final Map<String, IAdminCommandHandler> _datatable;
  24. protected AdminCommandHandler()
  25. {
  26. _datatable = new HashMap<>();
  27. }
  28. @Override
  29. public void registerHandler(IAdminCommandHandler handler)
  30. {
  31. String[] ids = handler.getAdminCommandList();
  32. for (String id : ids)
  33. {
  34. _datatable.put(id, handler);
  35. }
  36. }
  37. @Override
  38. public synchronized void removeHandler(IAdminCommandHandler handler)
  39. {
  40. String[] ids = handler.getAdminCommandList();
  41. for (String id : ids)
  42. {
  43. _datatable.remove(id);
  44. }
  45. }
  46. @Override
  47. public IAdminCommandHandler getHandler(String adminCommand)
  48. {
  49. String command = adminCommand;
  50. if (adminCommand.contains(" "))
  51. {
  52. command = adminCommand.substring(0, adminCommand.indexOf(" "));
  53. }
  54. return _datatable.get(command);
  55. }
  56. @Override
  57. public int size()
  58. {
  59. return _datatable.size();
  60. }
  61. public static AdminCommandHandler getInstance()
  62. {
  63. return SingletonHolder._instance;
  64. }
  65. private static class SingletonHolder
  66. {
  67. protected static final AdminCommandHandler _instance = new AdminCommandHandler();
  68. }
  69. }