L2FastMap.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2004-2014 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.util;
  20. import java.util.Map;
  21. import javolution.util.FastMap;
  22. import com.l2jserver.gameserver.model.interfaces.IL2EntryProcedure;
  23. import com.l2jserver.gameserver.model.interfaces.IProcedure;
  24. /**
  25. * A custom version of FastMap with extension for iterating without using temporary collection<br>
  26. * It's provide synchronization lock when iterating if needed<br>
  27. * @author Julian
  28. * @version 1.0.1 (2008-02-07)<br>
  29. * Changes:<br>
  30. * 1.0.0 - Initial version.<br>
  31. * 1.0.1 - Made forEachP() final.<br>
  32. * @author UnAfraid
  33. * @version 1.0.2 (2012-08-19)<br>
  34. * 1.0.2 - Using IL2Procedure instead of I2ForEachKey/Value<br>
  35. * @param <K>
  36. * @param <V>
  37. */
  38. public class L2FastMap<K, V> extends FastMap<K, V>
  39. {
  40. private static final long serialVersionUID = 8503855490858805336L;
  41. public L2FastMap()
  42. {
  43. this(false);
  44. }
  45. public L2FastMap(Map<? extends K, ? extends V> map)
  46. {
  47. this(map, false);
  48. }
  49. public L2FastMap(int initialCapacity)
  50. {
  51. this(initialCapacity, false);
  52. }
  53. public L2FastMap(boolean shared)
  54. {
  55. super();
  56. if (shared)
  57. {
  58. shared();
  59. }
  60. }
  61. public L2FastMap(Map<? extends K, ? extends V> map, boolean shared)
  62. {
  63. super(map);
  64. if (shared)
  65. {
  66. shared();
  67. }
  68. }
  69. public L2FastMap(int initialCapacity, boolean shared)
  70. {
  71. super(initialCapacity);
  72. if (shared)
  73. {
  74. shared();
  75. }
  76. }
  77. /**
  78. * Public method that iterate entire collection.<br>
  79. * <br>
  80. * @param proc - a class method that must be executed on every element of collection.<br>
  81. * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
  82. * check method (IL2EntryProcedure.execute())<br>
  83. */
  84. public boolean executeForEachEntry(IL2EntryProcedure<K, V> proc)
  85. {
  86. for (Map.Entry<K, V> e : entrySet())
  87. {
  88. if (!proc.execute(e.getKey(), e.getValue()))
  89. {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. public boolean executeForEachKey(IProcedure<K, Boolean> proc)
  96. {
  97. for (K k : keySet())
  98. {
  99. if (!proc.execute(k))
  100. {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. public boolean executeForEachValue(IProcedure<V, Boolean> proc)
  107. {
  108. for (V v : values())
  109. {
  110. if (!proc.execute(v))
  111. {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. }