L2FastMap.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. *
  20. * A custom version of HashMap with extension for iterating without using temporary collection<br>
  21. * <br>
  22. * @author Julian Version 1.0.1 (2008-02-07)<br>
  23. * Changes:<br>
  24. * 1.0.0 - Initial version.<br>
  25. * 1.0.1 - Made forEachP() final.<br>
  26. */
  27. public class L2FastMap<K extends Object, V extends Object> extends HashMap<K,V>
  28. {
  29. static final long serialVersionUID = 1L;
  30. /**
  31. * Public inner interface used by ForEach iterations<br>
  32. *
  33. * @author Julian
  34. */
  35. public interface I2ForEach<K,V> {
  36. public boolean forEach(K key, V val);
  37. }
  38. public interface I2ForEachKey<K> {
  39. public boolean forEach(K key);
  40. }
  41. public interface I2ForEachValue<V> {
  42. public boolean forEach(V val);
  43. }
  44. /**
  45. * Public method that iterate entire collection.<br>
  46. * <br>
  47. * @param func - a class method that must be executed on every element of collection.<br>
  48. * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
  49. * check method (I2ForEach.forEach())<br>
  50. */
  51. public boolean ForEach(I2ForEach<K,V> func) {
  52. for (Map.Entry<K,V> e: this.entrySet())
  53. if (!func.forEach(e.getKey(),e.getValue())) return false;
  54. return true;
  55. }
  56. public boolean ForEachKey(I2ForEachKey<K> func) {
  57. for (K k: this.keySet())
  58. if (!func.forEach(k)) return false;
  59. return true;
  60. }
  61. public boolean ForEachValue(I2ForEachValue<V> func) {
  62. for (V v: this.values())
  63. if (!func.forEach(v)) return false;
  64. return true;
  65. }
  66. }