source: trunk/src/org/expeditee/gui/ItemsList.java@ 1463

Last change on this file since 1463 was 1463, checked in by bnemhaus, 4 years ago

underlying() no longer leaks! It instead returns a clone. This was in fact how it was being used anyways.

File size: 1.9 KB
Line 
1package org.expeditee.gui;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.Iterator;
7import java.util.List;
8import java.util.function.Predicate;
9
10import org.expeditee.items.Item;
11
12/**
13 * Extends a ArrayList of Items through composition in order to keep it sorted.
14 */
15public class ItemsList implements Iterable<Item> {
16 private List<Item> items = new ArrayList<Item>();
17 private boolean sorted = true;
18
19 public ItemsList() { }
20
21 public ItemsList(ItemsList initialList) {
22 items.addAll(initialList.items);
23 }
24
25 public ItemsList(Collection<Item> unsorted) {
26 items.addAll(unsorted);
27 sort();
28 }
29
30 public void add(Item item) {
31 items.add(item);
32 sorted = false;
33 }
34
35 public void addAll(Collection<Item> toAdd) {
36 items.addAll(toAdd);
37 }
38
39 public void sort() {
40 if (!sorted) {
41 items.removeIf(item -> item == null);
42 }
43 Collections.sort(items);
44 sorted = true;
45 }
46
47 @Override
48 public Iterator<Item> iterator() {
49 //sort();
50 return items.iterator();
51 }
52
53 public boolean contains(Item item) {
54 return items.contains(item);
55 }
56
57 public int size() {
58 return items.size();
59 }
60
61 public void invalidateSorted() {
62 sorted = false;
63 }
64
65 public boolean remove(Item item) {
66 return items.remove(item);
67 }
68
69 public void removeAll(ItemsList newBody) {
70 items.removeAll(newBody.items);
71 }
72
73 public void retainAll(ItemsList historyItems) {
74 items.retainAll(historyItems.items);
75 }
76
77 public int indexOf(Item item) {
78 return items.indexOf(item);
79 }
80
81 public void set(int index, Item item) {
82 items.set(index, item);
83 }
84
85 public boolean removeIf(Predicate<Item> condition) {
86 return items.removeIf(condition);
87 }
88
89 public List<Item> cloneList() {
90 List<Item> ret = new ArrayList<Item>(items);
91 return ret;
92 }
93
94 public boolean isEmpty() {
95 return items.isEmpty();
96 }
97
98 public void clear() {
99 items.clear();
100 }
101}
Note: See TracBrowser for help on using the repository browser.