ActionHandler.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. import com.l2jserver.gameserver.model.L2Object.InstanceType;
  19. /**
  20. * @author UnAfraid
  21. */
  22. public class ActionHandler implements IHandler<IActionHandler, InstanceType>
  23. {
  24. private final Map<InstanceType, IActionHandler> _actions;
  25. public static ActionHandler getInstance()
  26. {
  27. return SingletonHolder._instance;
  28. }
  29. protected ActionHandler()
  30. {
  31. _actions = new HashMap<>();
  32. }
  33. @Override
  34. public void registerHandler(IActionHandler handler)
  35. {
  36. _actions.put(handler.getInstanceType(), handler);
  37. }
  38. @Override
  39. public synchronized void removeHandler(IActionHandler handler)
  40. {
  41. _actions.remove(handler.getInstanceType());
  42. }
  43. @Override
  44. public IActionHandler getHandler(InstanceType iType)
  45. {
  46. IActionHandler result = null;
  47. for (InstanceType t = iType; t != null; t = t.getParent())
  48. {
  49. result = _actions.get(t);
  50. if (result != null)
  51. {
  52. break;
  53. }
  54. }
  55. return result;
  56. }
  57. @Override
  58. public int size()
  59. {
  60. return _actions.size();
  61. }
  62. private static class SingletonHolder
  63. {
  64. protected static final ActionHandler _instance = new ActionHandler();
  65. }
  66. }