L2HashMap.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.HashMap;
  21. import java.util.Map;
  22. import com.l2jserver.gameserver.model.interfaces.IL2EntryProcedure;
  23. import com.l2jserver.gameserver.model.interfaces.IProcedure;
  24. /**
  25. * A custom version of HashMap: Extension for iterating without using temporary collection<br>
  26. * @author UnAfraid
  27. * @param <K>
  28. * @param <V>
  29. */
  30. public class L2HashMap<K, V> extends HashMap<K, V>
  31. {
  32. private static final long serialVersionUID = 8503855490858805336L;
  33. private static final float DEFAULT_LOAD_FACTOR = 0.75f;
  34. public L2HashMap()
  35. {
  36. super();
  37. }
  38. public L2HashMap(Map<? extends K, ? extends V> map)
  39. {
  40. super(map);
  41. }
  42. public L2HashMap(int initialCapacity)
  43. {
  44. this(initialCapacity, DEFAULT_LOAD_FACTOR);
  45. }
  46. public L2HashMap(int initialCapacity, float loadFactor)
  47. {
  48. super(initialCapacity, loadFactor);
  49. }
  50. /**
  51. * Public method that iterate entire collection.<br>
  52. * @param proc - a class method that must be executed on every element of collection.<br>
  53. * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
  54. * check method (IL2EntryProcedure.execute())<br>
  55. */
  56. public boolean executeForEachEntry(IL2EntryProcedure<K, V> proc)
  57. {
  58. for (Map.Entry<K, V> e : entrySet())
  59. {
  60. if (!proc.execute(e.getKey(), e.getValue()))
  61. {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. public boolean executeForEachKey(IProcedure<K, Boolean> proc)
  68. {
  69. for (K k : keySet())
  70. {
  71. if (!proc.execute(k))
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. public boolean executeForEachValue(IProcedure<V, Boolean> proc)
  79. {
  80. for (V v : values())
  81. {
  82. if (!proc.execute(v))
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. }