L2FastMap.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  19. * A custom version of HashMap with extension for iterating without using temporary collection<br>
  20. * <br>
  21. * @author Julian Version 1.0.1 (2008-02-07)<br>
  22. * Changes:<br>
  23. * 1.0.0 - Initial version.<br>
  24. * 1.0.1 - Made forEachP() final.<br>
  25. * @param <K>
  26. * @param <V>
  27. */
  28. public class L2FastMap<K extends Object, V extends Object> extends HashMap<K, V>
  29. {
  30. static final long serialVersionUID = 1L;
  31. /**
  32. * Public inner interface used by ForEach iterations<br>
  33. * @author Julian
  34. * @param <K>
  35. * @param <V>
  36. */
  37. public interface I2ForEach<K, V>
  38. {
  39. public boolean forEach(K key, V val);
  40. }
  41. public interface I2ForEachKey<K>
  42. {
  43. public boolean forEach(K key);
  44. }
  45. public interface I2ForEachValue<V>
  46. {
  47. public boolean forEach(V val);
  48. }
  49. /**
  50. * Public method that iterate entire collection.<br>
  51. * <br>
  52. * @param func - 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 (I2ForEach.forEach())<br>
  55. */
  56. public boolean ForEach(I2ForEach<K, V> func)
  57. {
  58. for (Map.Entry<K, V> e : entrySet())
  59. {
  60. if (!func.forEach(e.getKey(), e.getValue()))
  61. {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. public boolean ForEachKey(I2ForEachKey<K> func)
  68. {
  69. for (K k : keySet())
  70. {
  71. if (!func.forEach(k))
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. public boolean ForEachValue(I2ForEachValue<V> func)
  79. {
  80. for (V v : values())
  81. {
  82. if (!func.forEach(v))
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. }