-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHelper.java
More file actions
126 lines (89 loc) · 2.07 KB
/
ArrayHelper.java
File metadata and controls
126 lines (89 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package dev.lofiz.arial.event;
import java.util.Iterator;
public class ArrayHelper<T> implements Iterable<T> {
private T[] elements;
public ArrayHelper(final T[] array) {
this.elements = array;
}
@SuppressWarnings("unchecked")
public ArrayHelper() {
this.elements = (T[]) new Object[0];
}
@SuppressWarnings("unchecked")
public void add(final T t) {
if (t != null) {
final Object[] array = new Object[this.size() + 1];
for (int i = 0; i < array.length; i++) {
if (i < this.size()) {
array[i] = this.get(i);
} else {
array[i] = t;
}
}
this.set((T[]) array);
}
}
@SuppressWarnings("unchecked")
public boolean contains(final T t) {
Object[] array;
for (int lenght = (array = this.array()).length, i = 0; i < lenght; i++) {
final T entry = (T) array[i];
if (entry.equals(t)) {
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public void remove(final T t) {
if (this.contains(t)) {
final Object[] array = new Object[this.size() - 1];
boolean b = true;
for (int i = 0; i < this.size(); i++) {
if (b && this.get(i).equals(t)) {
b = false;
} else {
array[b ? i : (i - 1)] = this.get(i);
}
}
this.set((T[]) array);
}
}
public T[] array() {
return (T[]) this.elements;
}
public int size() {
return this.array().length;
}
public void set(final T[] array) {
this.elements = array;
}
public T get(final int index) {
return this.array()[index];
}
@SuppressWarnings("unchecked")
public void clear() {
this.elements = (T[]) new Object[0];
}
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int index = 0;
@Override
public boolean hasNext() {
return this.index < ArrayHelper.this.size() && ArrayHelper.this.get(this.index) != null;
}
@Override
public T next() {
return ArrayHelper.this.get(this.index++);
}
@Override
public void remove() {
ArrayHelper.this.remove(ArrayHelper.this.get(this.index));
}
};
}
}