PunishmentHolder.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2004-2013 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.model.holders;
  20. import java.util.Map;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import com.l2jserver.gameserver.model.punishment.PunishmentTask;
  23. import com.l2jserver.gameserver.model.punishment.PunishmentType;
  24. /**
  25. * @author UnAfraid
  26. */
  27. public final class PunishmentHolder
  28. {
  29. private final Map<String, Map<PunishmentType, PunishmentTask>> _holder = new ConcurrentHashMap<>();
  30. /**
  31. * Stores the punishment task in the Map.
  32. * @param task
  33. */
  34. public void addPunishment(PunishmentTask task)
  35. {
  36. if (!task.isExpired())
  37. {
  38. String key = String.valueOf(task.getKey());
  39. if (!_holder.containsKey(key))
  40. {
  41. _holder.put(key, new ConcurrentHashMap<PunishmentType, PunishmentTask>());
  42. }
  43. _holder.get(key).put(task.getType(), task);
  44. }
  45. }
  46. /**
  47. * Removes previously stopped task from the Map.
  48. * @param task
  49. */
  50. public void stopPunishment(PunishmentTask task)
  51. {
  52. String key = String.valueOf(task.getKey());
  53. if (_holder.containsKey(key))
  54. {
  55. task.stopPunishment();
  56. final Map<PunishmentType, PunishmentTask> punishments = _holder.get(key);
  57. punishments.remove(task.getType());
  58. if (punishments.isEmpty())
  59. {
  60. _holder.remove(key);
  61. }
  62. }
  63. }
  64. /**
  65. * @param key
  66. * @param type
  67. * @return {@code true} if Map contains the current key and type, {@code false} otherwise.
  68. */
  69. public boolean hasPunishment(String key, PunishmentType type)
  70. {
  71. return getPunishment(key, type) != null;
  72. }
  73. /**
  74. * @param key
  75. * @param type
  76. * @return {@link PunishmentTask} by specified key and type if exists, null otherwise.
  77. */
  78. public PunishmentTask getPunishment(String key, PunishmentType type)
  79. {
  80. if (_holder.containsKey(key))
  81. {
  82. return _holder.get(key).get(type);
  83. }
  84. return null;
  85. }
  86. }