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
79 lines (60 loc) · 2.21 KB
/
AbstractStorage.java
File metadata and controls
79 lines (60 loc) · 2.21 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
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.Collections;
import java.util.List;
import java.util.logging.Logger;
public abstract class AbstractStorage<SK> implements IStorage{
private static final Logger LOG = Logger.getLogger(AbstractStorage.class.getName());
protected abstract void doSave(Resume resume, SK searchKey);
protected abstract SK getSearchKey(String uuid);
protected abstract Resume doGet(SK searchKey);
protected abstract void doUpdate(Resume resume, SK searchKey);
protected abstract void doDelete(SK searchKey);
protected abstract boolean isExist(SK searchKey);
protected abstract List<Resume> doCopyAll();
public void save(Resume resume) {
LOG.info("Save " + resume);
SK searchKey = getNotExistSearchKey(resume.getUuid());
doSave(resume, searchKey);
}
public Resume get(String uuid) {
LOG.info("Get " + uuid);
SK searchKey = getExistSearchKey(uuid);
return doGet(searchKey);
}
public void update(Resume resume) {
LOG.info("Update " + resume);
SK searchKey = getExistSearchKey(resume.getUuid());
doUpdate(resume, searchKey);
}
public void delete(String uuid) {
LOG.info("Delete " + uuid);
SK searchKey = getExistSearchKey(uuid);
doDelete(searchKey);
}
protected SK getExistSearchKey(String uuid){
SK searchKey = getSearchKey(uuid);
if (!isExist(searchKey)){
LOG.warning("Resume " + uuid + " not exist.");
throw new NotExistStorageException(uuid);
}
return searchKey;
}
@Override
public List<Resume> getAllSorted() {
LOG.info("getAllSorted");
List<Resume> list = doCopyAll();
Collections.sort(list);
return list;
}
protected SK getNotExistSearchKey(String uuid){
SK searchKey = getSearchKey(uuid);
if (isExist(searchKey)){
LOG.warning("Resume " + uuid + " is exist.");
throw new ExistStorageException(uuid);
}
return searchKey;
}
}