-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbstractIntArray.java
More file actions
150 lines (130 loc) · 3.13 KB
/
AbstractIntArray.java
File metadata and controls
150 lines (130 loc) · 3.13 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package javasabr.rlib.collections.array.impl;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javasabr.rlib.collections.array.IntArray;
import javasabr.rlib.collections.array.UnsafeIntArray;
import javasabr.rlib.common.util.ArrayUtils;
import lombok.AccessLevel;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
@Accessors(fluent = true)
@FieldDefaults(level = AccessLevel.PROTECTED)
public abstract class AbstractIntArray implements UnsafeIntArray {
@Override
public int first() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return unsafeGet(0);
}
@Override
public int last() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return unsafeGet(size() - 1);
}
@Override
public int get(int index) {
checkIndex(index);
return unsafeGet(index);
}
@Override
public int unsafeGet(int index) {
return wrapped()[index];
}
protected void checkIndex(int index) {
if (index < 0 || index >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
}
@Override
public boolean contains(int value) {
int[] wrapped = wrapped();
for (int i = 0, length = size(); i < length; i++) {
if (value == wrapped[i]) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(IntArray array) {
if (array.isEmpty()) {
return true;
}
int[] wrapped = array.asUnsafe().wrapped();
for (int i = 0, length = array.size(); i < length; i++) {
if (!contains(wrapped[i])) {
return false;
}
}
return true;
}
@Override
public boolean containsAll(int[] array) {
if (array.length < 1) {
return true;
}
for (int value : array) {
if (!contains(value)) {
return false;
}
}
return true;
}
@Override
public int indexOf(int value) {
int[] wrapped = wrapped();
for (int i = 0, length = size(); i < length; i++) {
if (value == wrapped[i]) {
return i;
}
}
return -1;
}
@Override
public int lastIndexOf(int value) {
int[] wrapped = wrapped();
for (int i = size() - 1; i >= 0; i--) {
if (value == wrapped[i]) {
return i;
}
}
return -1;
}
@Override
public Iterator<Integer> iterator() {
return new DefaultIntArrayIterator(this);
}
@Override
public int[] toArray() {
return Arrays.copyOf(wrapped(), size());
}
@Override
public boolean equals(Object another) {
if (!(another instanceof IntArray array)) {
return false;
}
int[] wrapped = array.asUnsafe().wrapped();
return Arrays.equals(wrapped(), 0, size(), wrapped, 0, array.size());
}
@Override
public int hashCode() {
int[] wrapped = wrapped();
int result = 1;
for (int i = 0, wrappedLength = size(); i < wrappedLength; i++) {
result = 31 * result + wrapped[i];
}
return result;
}
@Override
public String toString() {
return ArrayUtils.toString(wrapped(), 0, size(), ",", false, true);
}
@Override
public UnsafeIntArray asUnsafe() {
return this;
}
}