L2FastList.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 net.sf.l2j.util;
  16. import java.util.LinkedList;
  17. /**
  18. *
  19. * A custom version of LinkedList with extension for iterating without using temporary collection<br>
  20. * It`s provide synchronization lock when iterating if needed<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 L2FastList<T extends Object> extends LinkedList<T>
  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<T> {
  36. public boolean ForEach(T obj);
  37. }
  38. /**
  39. * Public method that iterate entire collection.<br>
  40. * <br>
  41. * @param func - a class method that must be executed on every element of collection.<br>
  42. * @param sync - if set to true, will lock entire collection.<br>
  43. * @return - returns true if entire collection is iterated, false if it`s been interrupted by<br>
  44. * check method (I2ForEach.forEach())<br>
  45. */
  46. public final boolean forEach(I2ForEach<T> func, boolean sync) {
  47. if (sync)
  48. synchronized(this) { return forEachP(func); }
  49. else
  50. return forEachP(func);
  51. }
  52. // private method that implements forEach iteration
  53. private final boolean forEachP(I2ForEach<T> func) {
  54. for (T e: this)
  55. if (!func.ForEach(e)) return false;
  56. return true;
  57. }
  58. }