L2FastMap.java 2.7 KB

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