L2HashMap.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.HashMap;
  17. import java.util.Map;
  18. import com.l2jserver.gameserver.model.IL2EntryProcedure;
  19. import com.l2jserver.gameserver.model.IL2Procedure;
  20. /**
  21. * A custom version of HashMap: Extension for iterating without using temporary collection<br>
  22. * Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the
  23. * map. If no such object exists, the map should be "wrapped" using the {@link L2FastMap}. This is best done at creation time, to prevent accidental unsynchronized access.
  24. * @author UnAfraid
  25. * @param <K>
  26. * @param <V>
  27. */
  28. public class L2HashMap<K, V> extends HashMap<K, V>
  29. {
  30. private static final long serialVersionUID = 8503855490858805336L;
  31. private static final float DEFAULT_LOAD_FACTOR = 0.75f;
  32. public L2HashMap()
  33. {
  34. super();
  35. }
  36. public L2HashMap(Map<? extends K, ? extends V> map)
  37. {
  38. super(map);
  39. }
  40. public L2HashMap(int initialCapacity)
  41. {
  42. this(initialCapacity, DEFAULT_LOAD_FACTOR);
  43. }
  44. public L2HashMap(int initialCapacity, float loadFactor)
  45. {
  46. super(initialCapacity, loadFactor);
  47. }
  48. /**
  49. * Public method that iterate entire collection.<br>
  50. * @param proc - a class method that must be executed on every element of collection.<br>
  51. * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
  52. * check method (IL2EntryProcedure.execute())<br>
  53. */
  54. public boolean executeForEachEntry(IL2EntryProcedure<K, V> proc)
  55. {
  56. for (Map.Entry<K, V> e : entrySet())
  57. {
  58. if (!proc.execute(e.getKey(), e.getValue()))
  59. {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. public boolean executeForEachKey(IL2Procedure<K> proc)
  66. {
  67. for (K k : keySet())
  68. {
  69. if (!proc.execute(k))
  70. {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. public boolean executeForEachValue(IL2Procedure<V> proc)
  77. {
  78. for (V v : values())
  79. {
  80. if (!proc.execute(v))
  81. {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. }