L2FastMap.java 2.9 KB

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