ChatHandler.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  23. * This class handles all chat handlers
  24. * @author durgus, UnAfraid
  25. */
  26. public class ChatHandler implements IHandler<IChatHandler, Integer>
  27. {
  28. private final Map<Integer, IChatHandler> _datatable;
  29. /**
  30. * Singleton constructor
  31. */
  32. protected ChatHandler()
  33. {
  34. _datatable = new HashMap<>();
  35. }
  36. /**
  37. * Register a new chat handler
  38. * @param handler
  39. */
  40. @Override
  41. public void registerHandler(IChatHandler handler)
  42. {
  43. int[] ids = handler.getChatTypeList();
  44. for (int id : ids)
  45. {
  46. _datatable.put(id, handler);
  47. }
  48. }
  49. @Override
  50. public synchronized void removeHandler(IChatHandler handler)
  51. {
  52. int[] ids = handler.getChatTypeList();
  53. for (int id : ids)
  54. {
  55. _datatable.remove(id);
  56. }
  57. }
  58. /**
  59. * Get the chat handler for the given chat type
  60. * @param chatType
  61. * @return
  62. */
  63. @Override
  64. public IChatHandler getHandler(Integer chatType)
  65. {
  66. return _datatable.get(chatType);
  67. }
  68. /**
  69. * Returns the size
  70. * @return
  71. */
  72. @Override
  73. public int size()
  74. {
  75. return _datatable.size();
  76. }
  77. public static ChatHandler getInstance()
  78. {
  79. return SingletonHolder._instance;
  80. }
  81. private static class SingletonHolder
  82. {
  83. protected static final ChatHandler _instance = new ChatHandler();
  84. }
  85. }