|
| 1 | +package ru.javawebinar.basejava.storage; |
| 2 | + |
| 3 | +import ru.javawebinar.basejava.exception.StorageException; |
| 4 | +import ru.javawebinar.basejava.model.Resume; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
| 10 | + |
| 11 | +/** |
| 12 | + * gkislin |
| 13 | + * 22.07.2016 |
| 14 | + */ |
| 15 | +public abstract class AbstractFileStorage extends AbstractStorage<File> { |
| 16 | + private File directory; |
| 17 | + |
| 18 | + protected AbstractFileStorage(File directory) { |
| 19 | + Objects.requireNonNull(directory, "directory must not be null"); |
| 20 | + if (!directory.isDirectory()) { |
| 21 | + throw new IllegalArgumentException(directory.getAbsolutePath() + " is not directory"); |
| 22 | + } |
| 23 | + if (!directory.canRead() || !directory.canWrite()) { |
| 24 | + throw new IllegalArgumentException(directory.getAbsolutePath() + " is not readable/writable"); |
| 25 | + } |
| 26 | + this.directory = directory; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void clear() { |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public int size() { |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected File getSearchKey(String uuid) { |
| 41 | + return new File(directory, uuid); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void doUpdate(Resume r, File file) { |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected boolean isExist(File file) { |
| 51 | + return file.exists(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void doSave(Resume r, File file) { |
| 56 | + try { |
| 57 | + file.createNewFile(); |
| 58 | + doWrite(r, file); |
| 59 | + } catch (IOException e) { |
| 60 | + throw new StorageException("IO error", file.getName(), e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected abstract void doWrite(Resume r, File file) throws IOException; |
| 65 | + |
| 66 | + @Override |
| 67 | + protected Resume doGet(File file) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void doDelete(File file) { |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected List<Resume> doCopyAll() { |
| 78 | + return null; |
| 79 | + } |
| 80 | +} |
0 commit comments