ItemHandler.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.items.L2EtcItem;
  19. /**
  20. * This class manages handlers of items
  21. * @author UnAfraid
  22. */
  23. public class ItemHandler implements IHandler<IItemHandler, L2EtcItem>
  24. {
  25. private final Map<String, IItemHandler> _datatable;
  26. /**
  27. * Constructor of ItemHandler
  28. */
  29. protected ItemHandler()
  30. {
  31. _datatable = new HashMap<>();
  32. }
  33. /**
  34. * Adds handler of item type in <I>datatable</I>.<BR>
  35. * <BR>
  36. * <B><I>Concept :</I></U><BR>
  37. * This handler is put in <I>datatable</I> Map &lt;String ; IItemHandler &gt; for each ID corresponding to an item type (existing in classes of package itemhandlers) sets as key of the Map.
  38. * @param handler (IItemHandler)
  39. */
  40. @Override
  41. public void registerHandler(IItemHandler handler)
  42. {
  43. _datatable.put(handler.getClass().getSimpleName(), handler);
  44. }
  45. @Override
  46. public synchronized void removeHandler(IItemHandler handler)
  47. {
  48. _datatable.remove(handler.getClass().getSimpleName());
  49. }
  50. /**
  51. * Returns the handler of the item
  52. * @param item
  53. * @return IItemHandler
  54. */
  55. @Override
  56. public IItemHandler getHandler(L2EtcItem item)
  57. {
  58. if ((item == null) || (item.getHandlerName() == null))
  59. {
  60. return null;
  61. }
  62. return _datatable.get(item.getHandlerName());
  63. }
  64. /**
  65. * Returns the number of elements contained in datatable
  66. * @return int : Size of the datatable
  67. */
  68. @Override
  69. public int size()
  70. {
  71. return _datatable.size();
  72. }
  73. /**
  74. * Create ItemHandler if doesn't exist and returns ItemHandler
  75. * @return ItemHandler
  76. */
  77. public static ItemHandler getInstance()
  78. {
  79. return SingletonHolder._instance;
  80. }
  81. private static class SingletonHolder
  82. {
  83. protected static final ItemHandler _instance = new ItemHandler();
  84. }
  85. }