UserCommandHandler.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 UserCommandHandler implements IHandler<IUserCommandHandler, Integer>
  22. {
  23. private final Map<Integer, IUserCommandHandler> _datatable;
  24. protected UserCommandHandler()
  25. {
  26. _datatable = new HashMap<>();
  27. }
  28. @Override
  29. public void registerHandler(IUserCommandHandler handler)
  30. {
  31. int[] ids = handler.getUserCommandList();
  32. for (int id : ids)
  33. {
  34. _datatable.put(id, handler);
  35. }
  36. }
  37. @Override
  38. public synchronized void removeHandler(IUserCommandHandler handler)
  39. {
  40. int[] ids = handler.getUserCommandList();
  41. for (int id : ids)
  42. {
  43. _datatable.remove(id);
  44. }
  45. }
  46. @Override
  47. public IUserCommandHandler getHandler(Integer userCommand)
  48. {
  49. return _datatable.get(userCommand);
  50. }
  51. @Override
  52. public int size()
  53. {
  54. return _datatable.size();
  55. }
  56. public static UserCommandHandler getInstance()
  57. {
  58. return SingletonHolder._instance;
  59. }
  60. private static class SingletonHolder
  61. {
  62. protected static final UserCommandHandler _instance = new UserCommandHandler();
  63. }
  64. }