L2FastMap.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. *
  34. * @author Julian
  35. * @param <K>
  36. * @param <V>
  37. */
  38. public interface I2ForEach<K,V> {
  39. public boolean forEach(K key, V val);
  40. }
  41. public interface I2ForEachKey<K> {
  42. public boolean forEach(K key);
  43. }
  44. public interface I2ForEachValue<V> {
  45. public boolean forEach(V val);
  46. }
  47. /**
  48. * Public method that iterate entire collection.<br>
  49. * <br>
  50. * @param func - 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 (I2ForEach.forEach())<br>
  53. */
  54. public boolean ForEach(I2ForEach<K,V> func) {
  55. for (Map.Entry<K,V> e: this.entrySet())
  56. if (!func.forEach(e.getKey(),e.getValue())) return false;
  57. return true;
  58. }
  59. public boolean ForEachKey(I2ForEachKey<K> func) {
  60. for (K k: this.keySet())
  61. if (!func.forEach(k)) return false;
  62. return true;
  63. }
  64. public boolean ForEachValue(I2ForEachValue<V> func) {
  65. for (V v: this.values())
  66. if (!func.forEach(v)) return false;
  67. return true;
  68. }
  69. }