Repository navigation
Expand file tree
/
Copy pathAbstractArrayStorage.java
More file actions
75 lines (61 loc) · 1.87 KB
/
Copy pathAbstractArrayStorage.java
File metadata and controls
75 lines (61 loc) · 1.87 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
package storage;
import exception.ExistStorageException;
import exception.NotExistStorageException;
import exception.StorageException;
import model.Resume;
import java.util.Arrays;
public abstract class AbstractArrayStorage implements Storage {
protected static final int STORAGE_LIMIT = 10_000;
protected Resume[] storage = new Resume[STORAGE_LIMIT];
protected int size = 0;
public int size() {
return size;
}
public Resume get(String uuid) {
int index = getIndexById(uuid);
if (index < 0) {
throw new NotExistStorageException(uuid);
} else {
return storage[index];
}
}
public Resume[] getAll() {
return Arrays.copyOfRange(storage, 0, size);
}
public void clear() {
Arrays.fill(storage, 0, size, null);
size = 0;
}
public void update(Resume r) {
int index = getIndexById(r.getUuid());
if (index < 0) {
throw new NotExistStorageException(r.getUuid());
} else {
storage[index] = r;
}
}
public void save(Resume r) {
int index = getIndexById(r.getUuid());
if (size == STORAGE_LIMIT) {
throw new StorageException("Storage overflow", r.getUuid());
} else if (index >= 0) {
throw new ExistStorageException(r.getUuid());
} else {
saveByIndex(r, index);
size++;
}
}
public void delete(String uuid) {
int index = getIndexById(uuid);
if (index < 0) {
throw new NotExistStorageException(uuid);
} else {
deleteByIndex(index);
storage[size - 1] = null;
size--;
}
}
protected abstract void saveByIndex(Resume r, int index);
protected abstract void deleteByIndex(int index);
protected abstract int getIndexById(String uuid);
}