ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LiveArrayList.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package live;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import listeners.ListenerHandle;
  6. import listeners.ListenerList;
  7. public final class LiveArrayList<T> implements MutableLiveList<T>, AutoCloseable {
  8. private final List<T> values = new ArrayList<T>();
  9. private final ListenerList<LiveList.Listener<T>> listeners = new ListenerList<LiveList.Listener<T>>();
  10. @Override
  11. public void add(T value) {
  12. int index = values.size();
  13. values.add(value);
  14. listeners.accept(listener ->
  15. listener.onInserted(index, value));
  16. }
  17. @Override
  18. public void insert(int index, T value) {
  19. values.add(index, value);
  20. listeners.accept(listener ->
  21. listener.onInserted(index, value));
  22. }
  23. @Override
  24. public void setAt(int index, T value) {
  25. T oldValue = values.get(index);
  26. values.set(index, value);
  27. listeners.accept(listener ->
  28. listener.onChanged(index, oldValue, value));
  29. }
  30. @Override
  31. public void remove(int index) {
  32. T oldValue = values.remove(index);
  33. listeners.accept(listener ->
  34. listener.onRemoved(index, oldValue));
  35. }
  36. @Override
  37. public void remove(T value) {
  38. int index = this.indexOf(value);
  39. if (index < 0)
  40. return;
  41. this.remove(index);
  42. }
  43. @Override
  44. public void clear() {
  45. int i = getLength();
  46. while (i > 0) {
  47. i--;
  48. this.remove(i);
  49. }
  50. }
  51. @Override
  52. public Iterator<T> iterator() {
  53. return values.iterator();
  54. }
  55. @Override
  56. public int indexOf(T value) {
  57. return values.indexOf(value);
  58. }
  59. @Override
  60. public int getLength() {
  61. return values.size();
  62. }
  63. @Override
  64. public T getAt(int index) {
  65. return values.get(index);
  66. }
  67. @Override
  68. public ListenerHandle<LiveList.Listener<T>> addListener(LiveList.Listener<T> listener) {
  69. return listeners.add(listener);
  70. }
  71. @Override
  72. public void close() {
  73. }
  74. }