[1415] | 1 | package org.expeditee.gui;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.Collections;
|
---|
| 6 | import java.util.Iterator;
|
---|
| 7 | import java.util.List;
|
---|
| 8 | import java.util.function.Predicate;
|
---|
| 9 |
|
---|
| 10 | import org.expeditee.items.Item;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Extends a ArrayList of Items through composition in order to keep it sorted.
|
---|
| 14 | */
|
---|
| 15 | public 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 |
|
---|
[1434] | 73 | public void retainAll(ItemsList historyItems) {
|
---|
[1415] | 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 |
|
---|
[1463] | 89 | public List<Item> cloneList() {
|
---|
| 90 | List<Item> ret = new ArrayList<Item>(items);
|
---|
| 91 | return ret;
|
---|
[1415] | 92 | }
|
---|
| 93 |
|
---|
| 94 | public boolean isEmpty() {
|
---|
| 95 | return items.isEmpty();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public void clear() {
|
---|
| 99 | items.clear();
|
---|
| 100 | }
|
---|
[1502] | 101 |
|
---|
| 102 | @Override
|
---|
| 103 | public String toString() {
|
---|
| 104 | StringBuilder sb = new StringBuilder("{");
|
---|
| 105 |
|
---|
| 106 | if (!items.isEmpty()) {
|
---|
| 107 | for (Item i : items) {
|
---|
| 108 | sb.append(i + ", ");
|
---|
| 109 | }
|
---|
| 110 | sb.deleteCharAt(sb.length() - 1);
|
---|
| 111 | sb.deleteCharAt(sb.length() - 1);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | sb.append("}");
|
---|
| 115 | return sb.toString();
|
---|
| 116 | }
|
---|
[1415] | 117 | }
|
---|