-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractStorage.java
More file actions
81 lines (62 loc) · 2.25 KB
/
AbstractStorage.java
File metadata and controls
81 lines (62 loc) · 2.25 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
package ru.javaops.webapp.storage;
import ru.javaops.webapp.exception.ExistStorageException;
import ru.javaops.webapp.exception.NotExistStorageException;
import ru.javaops.webapp.model.Resume;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;
public abstract class AbstractStorage<SK> implements Storage {
protected final static Comparator<Resume> RESUME_COMPARATOR = Comparator.comparing(Resume::getFullName)
.thenComparing(Resume::getUuid);
private static final Logger LOG = Logger.getLogger(AbstractStorage.class.getName());
@Override
public final void update(Resume r) {
LOG.info("Update " + r);
doUpdate(getExistKey(r.getUuid()), r);
}
@Override
public final void save(Resume r) {
LOG.info("Save " + r);
doSave(getNotExistKey(r.getUuid()), r);
}
@Override
public final Resume get(String uuid) {
LOG.info("Get " + uuid);
return doGet(getExistKey(uuid));
}
@Override
public final void delete(String uuid) {
LOG.info("Delete " + uuid);
doDelete(getExistKey(uuid));
}
@Override
public List<Resume> getAllSorted() {
LOG.info("getAllSorted");
List<Resume> allResumes = doCopyAll();
allResumes.sort(RESUME_COMPARATOR);
return allResumes;
}
protected abstract SK getSearchKey(String uuid);
protected abstract void doSave(SK searchKey, Resume r);
protected abstract void doDelete(SK searchKey);
protected abstract void doUpdate(SK searchKey, Resume r);
protected abstract Resume doGet(SK searchKey);
protected abstract List<Resume> doCopyAll();
protected SK getNotExistKey(String uuid) {
SK searchKey = getSearchKey(uuid);
if (isExist(searchKey)) {
LOG.warning("Resume " + uuid + " already exist");
throw new ExistStorageException(uuid);
}
return searchKey;
}
protected SK getExistKey(String uuid) {
SK searchKey = getSearchKey(uuid);
if (!isExist(searchKey)) {
LOG.warning("Resume " + uuid + " not exist");
throw new NotExistStorageException(uuid);
}
return searchKey;
}
protected abstract boolean isExist(SK searchKey);
}