-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayList.java
175 lines (152 loc) · 4.59 KB
/
ArrayList.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Implementation of a List as an ArrayList
*
* @author Laurent Mignot
*/
public class ArrayList implements List {
private final static int MIN_STORAGE_CAPACITY = 100;
private int size;
private Object[] storage;
public ArrayList () {
this.size = 0;
this.storage = new Object[MIN_STORAGE_CAPACITY];
}
/**
* @see List#isEmpty()
*/
@Override
public boolean isEmpty () {
return this.size == 0;
}
/**
* @see List#size()
*/
@Override
public int size () {
return this.size;
}
/**
* @see List#get()
*/
@Override
public ReturnObject get (int index) {
if (this.isEmpty()) {
return new ReturnObjectImpl(ErrorMessage.EMPTY_STRUCTURE);
}
if (this.isOutOfBounds(index)) {
return new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
}
return new ReturnObjectImpl(this.storage[index]);
}
/**
* I've chosen to not check for isEmpty in this scenario as
* the isOutOfBounds check would return true for an empty structure
* empty size: (0 - 1 = -1 = < 0)
* @see List#remove()
*/
@Override
public ReturnObject remove (int index) {
if (this.isOutOfBounds(index)) {
return new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
}
ReturnObject obj = new ReturnObjectImpl(this.storage[index]);
for (int i = index; i < this.size; i++) {
Object tmp = this.storage[i + 1];
this.storage[i] = tmp;
}
if (this.canDecreaseStorage()) {
this.decreaseStorage();
}
this.size--;
return obj;
}
/**
* @see List#add(int, Object)
*/
@Override
public ReturnObject add (int index, Object item) {
if (this.isOutOfBounds(index)) {
return new ReturnObjectImpl(ErrorMessage.INDEX_OUT_OF_BOUNDS);
}
if (item == null) {
return new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
}
if (this.shouldIncreaseStorage()) {
this.increaseStorage();
}
Object current = this.storage[index];
this.storage[index] = item;
for (int i = index + 1; i <= this.size; i++) {
Object tmp = this.storage[i];
this.storage[i] = current;
current = tmp;
}
this.size++;
return new ReturnObjectImpl(null);
}
/**
* @see List#add(Object)
*/
@Override
public ReturnObject add (Object item) {
if (item == null) {
return new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
}
if (this.shouldIncreaseStorage()) {
this.increaseStorage();
}
this.storage[this.size] = item;
this.size++;
return new ReturnObjectImpl(null);
}
/**
* Checks whether the given index is out of bounds of the data structure
* @param index the index to check
* @return true if out of bounds, false otherwise
*/
private boolean isOutOfBounds (int index) {
return (index < 0 || index > (this.size - 1));
}
/**
* Checks if we need to increase data structure capacity
* @return true if we should increase capacity, false otherwise
*/
private boolean shouldIncreaseStorage () {
return this.size >= this.storage.length;
}
/**
* Checks if we can reduce data structure capacity
* @return true if we can decrease capacity, false otherwise
*/
private boolean canDecreaseStorage () {
return (this.storage.length / 2) >= this.size;
}
/**
* Increases the capacity of data structure, maintaining any data in storage
*/
private void increaseStorage () {
int oldCapacity = this.storage.length;
int newCapacity = oldCapacity * 2;
Object[] tmp = new Object[newCapacity];
for (int i = 0; i < this.size; i++) {
tmp[i] = this.storage[i];
}
this.storage = tmp;
}
/**
* Reduces the capacity of data structure, maintaining any data in storage
* Reduced capacity will never be less than {@MIN_STORAGE_SIZE}
*/
private void decreaseStorage () {
int oldCapacity = this.storage.length;
int newCapacity = oldCapacity / 2;
if (newCapacity < this.MIN_STORAGE_CAPACITY) {
newCapacity = this.MIN_STORAGE_CAPACITY;
}
Object[] tmp = new Object[newCapacity];
for (int i = 0; i < this.size; i++) {
tmp[i] = this.storage[i];
}
this.storage = tmp;
}
}