ItemHandler.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.handler;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import com.l2jserver.gameserver.model.items.L2EtcItem;
  23. /**
  24. * This class manages handlers of items
  25. * @author UnAfraid
  26. */
  27. public class ItemHandler implements IHandler<IItemHandler, L2EtcItem>
  28. {
  29. private final Map<String, IItemHandler> _datatable;
  30. /**
  31. * Constructor of ItemHandler
  32. */
  33. protected ItemHandler()
  34. {
  35. _datatable = new HashMap<>();
  36. }
  37. /**
  38. * Adds handler of item type in <I>datatable</I>.<BR>
  39. * <BR>
  40. * <B><I>Concept :</I></U><BR>
  41. * 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.
  42. * @param handler (IItemHandler)
  43. */
  44. @Override
  45. public void registerHandler(IItemHandler handler)
  46. {
  47. _datatable.put(handler.getClass().getSimpleName(), handler);
  48. }
  49. @Override
  50. public synchronized void removeHandler(IItemHandler handler)
  51. {
  52. _datatable.remove(handler.getClass().getSimpleName());
  53. }
  54. /**
  55. * Returns the handler of the item
  56. * @param item
  57. * @return IItemHandler
  58. */
  59. @Override
  60. public IItemHandler getHandler(L2EtcItem item)
  61. {
  62. if ((item == null) || (item.getHandlerName() == null))
  63. {
  64. return null;
  65. }
  66. return _datatable.get(item.getHandlerName());
  67. }
  68. /**
  69. * Returns the number of elements contained in datatable
  70. * @return int : Size of the datatable
  71. */
  72. @Override
  73. public int size()
  74. {
  75. return _datatable.size();
  76. }
  77. /**
  78. * Create ItemHandler if doesn't exist and returns ItemHandler
  79. * @return ItemHandler
  80. */
  81. public static ItemHandler getInstance()
  82. {
  83. return SingletonHolder._instance;
  84. }
  85. private static class SingletonHolder
  86. {
  87. protected static final ItemHandler _instance = new ItemHandler();
  88. }
  89. }