-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStorage.java
More file actions
85 lines (70 loc) · 2.04 KB
/
Copy pathArrayStorage.java
File metadata and controls
85 lines (70 loc) · 2.04 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
import java.util.Arrays;
import java.util.Objects;
/**
* Array based storage for Resumes
*/
public class ArrayStorage {
private Resume[] storage = new Resume[10_000];
private int size;
public void clear() {
Arrays.fill(storage, 0, size, null);
size = 0;
}
public void update(Resume resume) {
int index = getIndex(resume.getUuid());
if (index >= 0) {
storage[index] = resume;
} else {
System.out.println("ERROR in method 'update' :" + resume.getUuid());
}
}
public void save(Resume resume) {
int index = getIndex(resume.getUuid());
if (index >= 0) {
System.out.println("ERROR in method 'save' :" + resume.getUuid());
} else {
if (size < storage.length) {
storage[size] = resume;
size++;
} else {
System.out.println("ERROR in method 'save' :" + resume.getUuid() + " (storage backing array boundary has been reached)");
}
}
}
public Resume get(String uuid) {
int index = getIndex(uuid);
if (index >= 0) {
return storage[index];
} else {
System.out.println("Invalid uuid in method 'get': " + uuid);
return null;
}
}
public void delete(String uuid) {
int index = getIndex(uuid);
if (index >= 0) {
storage[index] = storage[size - 1];
storage[size - 1] = null;
size--;
} else {
System.out.println("Invalid uuid in method 'delete': " + uuid);
}
}
/**
* @return array, contains only Resumes in storage (without null)
*/
public Resume[] getAll() {
return Arrays.copyOf(storage, size);
}
public int size() {
return size;
}
private int getIndex(String uuid) {
for (int i = 0; i < size; i++) {
if (Objects.equals(storage[i].getUuid(), uuid)) {
return i;
}
}
return -1;
}
}