ChatHandler.java 2.0 KB

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