ItemHandler.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 gnu.trove.map.hash.TIntObjectHashMap;
  17. import com.l2jserver.gameserver.templates.item.L2EtcItem;
  18. /**
  19. * This class manages handlers of items
  20. *
  21. * @version $Revision: 1.1.4.3 $ $Date: 2005/03/27 15:30:09 $
  22. */
  23. public class ItemHandler
  24. {
  25. private TIntObjectHashMap<IItemHandler> _datatable;
  26. /**
  27. * Create ItemHandler if doesn't exist and returns ItemHandler
  28. * @return ItemHandler
  29. */
  30. public static ItemHandler getInstance()
  31. {
  32. return SingletonHolder._instance;
  33. }
  34. /**
  35. * Returns the number of elements contained in datatable
  36. * @return int : Size of the datatable
  37. */
  38. public int size()
  39. {
  40. return _datatable.size();
  41. }
  42. /**
  43. * Constructor of ItemHandler
  44. */
  45. private ItemHandler()
  46. {
  47. _datatable = new TIntObjectHashMap<IItemHandler>();
  48. }
  49. /**
  50. * Adds handler of item type in <I>datatable</I>.<BR><BR>
  51. * <B><I>Concept :</I></U><BR>
  52. * This handler is put in <I>datatable</I> Map &lt;String ; IItemHandler &gt; for each ID corresponding to an item type
  53. * (existing in classes of package itemhandlers) sets as key of the Map.
  54. * @param handler (IItemHandler)
  55. */
  56. public void registerItemHandler(IItemHandler handler)
  57. {
  58. _datatable.put(handler.getClass().getSimpleName().intern().hashCode(), handler);
  59. }
  60. /**
  61. * Returns the handler of the item
  62. * @param item
  63. * @return IItemHandler
  64. */
  65. public IItemHandler getItemHandler(L2EtcItem item)
  66. {
  67. if (item == null || item.getHandlerName() == null)
  68. return null;
  69. return _datatable.get(item.getHandlerName().hashCode());
  70. }
  71. @SuppressWarnings("synthetic-access")
  72. private static class SingletonHolder
  73. {
  74. protected static final ItemHandler _instance = new ItemHandler();
  75. }
  76. }