forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractArrayStorage.java
More file actions
85 lines (71 loc) · 2.54 KB
/
AbstractArrayStorage.java
File metadata and controls
85 lines (71 loc) · 2.54 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
package com.urise.webapp.storage;
import com.urise.webapp.model.Resume;
import java.util.Arrays;
public abstract class AbstractArrayStorage implements Storage {
protected Resume[] storage = new Resume[MAX_SIZE];
protected int resumeCounter = 0;
private static final int MAX_SIZE = 10_0000;
public void clear() {
Arrays.fill(storage, 0, resumeCounter, null);
resumeCounter = 0;
}
public void save(Resume resume) {
int index = getIndex(resume.getUuid());
if (index >= 0) {
System.out.println("ERROR: resume with UUID = " + resume.getUuid() + " already exist!. Resume can't be saved.");
return;
}
if (resumeCounter >= MAX_SIZE) {
System.out.println("ERROR: storage is overfilled. Resume with UUID = " + resume.getUuid() + " can't be saved.");
return;
}
insertResume(index,resume);
resumeCounter++;
}
public Resume get(String uuid) {
int index = getIndex(uuid);
if (index < 0) {
System.out.println("ERROR: resume with UUID = " + uuid + " not founded.");
return null;
}
return storage[index];
}
public void delete(String uuid) {
int index = getIndex(uuid);
if (index < 0) {
System.out.println("ERROR: resume with UUID = " + uuid + " didn't found. Resume can't be deleted.");
return;
}
deleteResume(index);
storage[resumeCounter - 1] = null;
resumeCounter--;
}
/**
* @return array, contains only Resumes in storage (without null)
*/
public Resume[] getAll() {
return Arrays.copyOf(storage, resumeCounter);
}
public int size() {
return resumeCounter;
}
public void update(Resume resume) {
int index = getIndex(resume.getUuid());
if (index < 0) {
System.out.println("ERROR: resume with UUID = " + resume.getUuid() + " didn't found. Resume can't be updated.");
return;
}
storage[index] = resume;
}
/*
method returns:
1. Positive Index in storage, including zero, for existing element
2. Negative index for absent element. Result equals |index| of element, wich is next to absent element.
*/
protected abstract int getIndex(String uuid);
/*
* parametr index equals index of element in storage, wich is next to inserting element
* */
protected abstract void insertResume(int index, Resume resume);
protected abstract void deleteResume(int index);
}