forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractStorage.java
More file actions
69 lines (48 loc) · 1.89 KB
/
AbstractStorage.java
File metadata and controls
69 lines (48 loc) · 1.89 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
package com.urise.webapp.storage;
import com.urise.webapp.exception.ExistStorageException;
import com.urise.webapp.exception.NotExistStorageException;
import com.urise.webapp.model.Resume;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public abstract class AbstractStorage implements Storage {
protected final Comparator<Resume> RESUME_COMPARATOR_FULLNAME = Comparator.comparing(Resume::getFullName).thenComparing(Resume::getUuid);
protected abstract void doUpdate(Resume resume, Object searchKey);
protected abstract boolean isExist(Object searchKey);
protected abstract void doSave(Resume resume, Object searchKey);
protected abstract Resume doGet(Object searchKey);
protected abstract void doDelete(Object searchKey);
protected abstract Object getSearchKey(String uuid);
protected abstract List<Resume> doCopyAll();
public List<Resume> getAllSorted() {
List<Resume> resumes = doCopyAll();
resumes.sort(RESUME_COMPARATOR_FULLNAME);
return resumes;
}
public void save(Resume resume) {
doSave(resume, getNotExistedSearchKey(resume.getUuid()));
}
public void delete(String uuid) {
doDelete(getExistedSearchKey(uuid));
}
public void update(Resume resume) {
doUpdate(resume, getExistedSearchKey(resume.getUuid()));
}
public Resume get(String uuid) {
return doGet(getExistedSearchKey(uuid));
}
private Object getExistedSearchKey(String uuid) {
Object searchKey = getSearchKey(uuid);
if (!isExist(searchKey)) {
throw new NotExistStorageException(uuid);
}
return searchKey;
}
private Object getNotExistedSearchKey(String uuid) {
Object searchKey = getSearchKey(uuid);
if (isExist(searchKey)) {
throw new ExistStorageException(uuid);
}
return searchKey;
}
}