ItemHandler.java 2.4 KB

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