Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ru/javawebinar/basejava/MainCollections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.javawebinar.basejava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import ru.javawebinar.basejava.model.Resume;

public class MainCollections {

private static final String UUID_1 = "uuid1";
private static final Resume RESUME_1 = new Resume(UUID_1);
private static final String UUID_2 = "uuid2";
private static final Resume RESUME_2 = new Resume(UUID_2);
private static final String UUID_3 = "uuid3";
private static final Resume RESUME_3 = new Resume(UUID_3);
private static final String UUID_4 = "uuid4";
private static final Resume RESUME_4 = new Resume(UUID_4);

public static void main(String[] args) {
Collection<Resume> collection = new ArrayList<>();
collection.add(RESUME_1);
collection.add(RESUME_2);
collection.add(RESUME_3);

collection.removeIf(r -> Objects.equals(r.getUuid(), UUID_1));
// System.out.println(collection.);
}
}
60 changes: 25 additions & 35 deletions src/ru/javawebinar/basejava/storage/AbstractArrayStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,52 @@
import ru.javawebinar.basejava.exception.StorageException;
import ru.javawebinar.basejava.model.Resume;

public abstract class AbstractArrayStorage implements Storage {
public abstract class AbstractArrayStorage extends AbstractStorage {

protected static final int STORAGE_LIMIT = 10000;
protected Resume[] storage = new Resume[STORAGE_LIMIT];
protected int storageSize;

protected abstract int getIndex(String uuid);
@Override
protected boolean isExist(Object index) {
return (Integer) index >= 0;
}

@Override
protected abstract Integer getSearchKey(String uuid);

protected abstract void insertElement(Resume r, int index);

protected abstract void fillDeletedElement(int index);

public void update(Resume r) {
int index = getIndex(r.getUuid());
if (index >= 0) {
storage[index] = r;
} else {
throw new NotExistStorageException(r.getUuid());
}
@Override
protected void doUpdate(Resume r, Object index) {
storage[(Integer) index] = r;
}

public int size() {
return storageSize;
}

public void save(Resume r) {
if (storageSize >= STORAGE_LIMIT) {
throw new StorageException("Storage is full!", r.getUuid());
}
int index = getIndex(r.getUuid());
if (index < 0) {
insertElement(r, index);
storageSize++;
} else {
throw new ExistStorageException(r.getUuid());
@Override
protected void doSave(Resume r, Object index) {
if (storageSize == STORAGE_LIMIT) {
throw new StorageException("Storage is overflow!", r.getUuid());
}
insertElement(r, (Integer) index);
storageSize++;
}

public void delete(String uuid) {
int index = getIndex(uuid);
if (index >= 0) {
fillDeletedElement(index);
storage[storageSize] = null;
storageSize--;
} else {
throw new NotExistStorageException(uuid);
}
@Override
public void doDelete(Object index) {
fillDeletedElement((Integer) index);
storage[storageSize - 1] = null;
storageSize--;
}

public final Resume get(String uuid) {
int index = getIndex(uuid);
if (index >= 0) {
return storage[index];
} else {
throw new NotExistStorageException(uuid);
}
@Override
public Resume doGet(Object index) {
return storage[(Integer) index];
}

/**
Expand Down
56 changes: 56 additions & 0 deletions src/ru/javawebinar/basejava/storage/AbstractStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.javawebinar.basejava.storage;

import ru.javawebinar.basejava.exception.ExistStorageException;
import ru.javawebinar.basejava.exception.NotExistStorageException;
import ru.javawebinar.basejava.model.Resume;

public abstract class AbstractStorage implements Storage {

protected abstract Object getSearchKey(String uuid);

protected abstract void doUpdate(Resume r, Object searchKey);

protected abstract boolean isExist(Object searchKey);

protected abstract void doSave(Resume r, Object searchKey);

protected abstract void doDelete(Object searchKey);

protected abstract Resume doGet(Object searchKey);

public void update(Resume r) {
Object searchKey = getExistedSearchKey(r.getUuid());
doUpdate(r, searchKey);
}

public void save(Resume r) {
Object searchKey = getNotExistedSearchKey(r.getUuid());
doSave(r, searchKey);
}

public void delete(String uuid) {
Object searchKey = getExistedSearchKey(uuid);
doDelete(searchKey);
}

public Resume get(String uuid) {
Object searchKey = getExistedSearchKey(uuid);
return doGet(searchKey);
}

private Object getNotExistedSearchKey(String uuid) {
Object searchKey = getSearchKey(uuid);
if (isExist(searchKey)) {
throw new ExistStorageException(uuid);
}
return searchKey;
}

private Object getExistedSearchKey(String uuid) {
Object searchKey = getSearchKey(uuid);
if (!isExist(searchKey)) {
throw new NotExistStorageException(uuid);
}
return searchKey;
}
}
3 changes: 1 addition & 2 deletions src/ru/javawebinar/basejava/storage/ArrayStorage.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package ru.javawebinar.basejava.storage;

import ru.javawebinar.basejava.model.Resume;
import java.util.Arrays;

/**
* Array based storage for Resumes
*/
public class ArrayStorage extends AbstractArrayStorage {

@Override
protected int getIndex(String uuid) {
protected Integer getSearchKey(String uuid) {
for (int i = 0; i < storageSize; i++) {
if (uuid.equals(storage[i].getUuid())) {
return i;
Expand Down
74 changes: 74 additions & 0 deletions src/ru/javawebinar/basejava/storage/ListStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ru.javawebinar.basejava.storage;

import java.util.ArrayList;
import java.util.List;
import ru.javawebinar.basejava.exception.ExistStorageException;
import ru.javawebinar.basejava.exception.NotExistStorageException;
import ru.javawebinar.basejava.model.Resume;

public class ListStorage implements Storage {

List<Resume> storage = new ArrayList<>();

@Override
public void clear() {
storage.clear();
}

@Override
public void update(Resume r) {
int index = getIndex(r.getUuid());
if(index >= 0) {
storage.add(index, r);
} else {
throw new NotExistStorageException(r.getUuid());
}
}

private int getIndex(String uuid) {
for (int i = 0; i < storage.size(); i++) {
if(storage.get(i).getUuid().equals(uuid)) {
return i;
}
}
return -1;
}

@Override
public void save(Resume r) {
int index = getIndex(r.getUuid());
if(index >= 0) {
throw new ExistStorageException(r.getUuid());
} else {
storage.add(r);
}
}

@Override
public Resume get(String uuid) {
int index = getIndex(uuid);
if(index >= 0) {
return storage.get(index);
} else {
throw new NotExistStorageException(uuid);
}
}

@Override
public void delete(String uuid) {
boolean isDeleted = storage.remove(new Resume(uuid));
if(!isDeleted) {
throw new NotExistStorageException(uuid);
}
}

@Override
public Resume[] getAll() {
return storage.toArray(new Resume[storage.size()]);
}

@Override
public int size() {
return storage.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class SortedArrayStorage extends AbstractArrayStorage {

@Override
protected int getIndex(String uuid) {
protected Integer getSearchKey(String uuid) {
Resume searchKey = new Resume(uuid);
return Arrays.binarySearch(storage, 0, storageSize, searchKey);
}
Expand Down
22 changes: 11 additions & 11 deletions test/ru/javawebinar/basejava/storage/AbstractArrayStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,73 +27,73 @@ protected AbstractArrayStorageTest(Storage storage) {
private static final Resume RESUME_4 = new Resume(UUID_4);

@BeforeEach
public void setUp() throws Exception {
public void setUp() {
storage.clear();
storage.save(RESUME_1);
storage.save(RESUME_2);
storage.save(RESUME_3);
}

@Test
public void update() throws Exception {
public void update() {
Resume newResume = new Resume(UUID_1);
storage.update(newResume);
Assertions.assertSame(newResume, storage.get(UUID_1));
}

@Test
public void updateNotExist() throws Exception {
public void resumeNotExist() {
Assertions.assertThrows(NotExistStorageException.class,
() -> storage.get("dummy"));
}

@Test
public void size() throws Exception {
public void size() {
assertSize(3);
}

@Test
public void save() throws Exception {
public void save() {
storage.save(RESUME_4);
assertSize(4);
assertGet(RESUME_4);
}

@Test
public void saveExist() throws Exception {
public void saveExist() {
Assertions.assertThrows(ExistStorageException.class,
() -> storage.save(RESUME_1));
}

@Test
public void saveOverflow() throws Exception {
public void saveOverflow() {
try {
for (int i = 4; i <= AbstractArrayStorage.STORAGE_LIMIT; i++) {
storage.save(new Resume());
}
} catch (StorageException e) {
Assertions.fail();
Assertions.fail("Overflow was too early");
}
Assertions.assertThrows(StorageException.class,
() -> storage.save(new Resume()));
}

@Test
public void delete() throws Exception {
public void delete() {
storage.delete(UUID_1);
assertSize(2);
Assertions.assertThrows(NotExistStorageException.class,
() -> storage.delete(UUID_1));
}

@Test
public void deleteNotExist() throws Exception {
public void deleteNotExist() {
Assertions.assertThrows(NotExistStorageException.class,
() -> storage.delete("dummy"));
}

@Test
public void get() throws Exception {
public void get() {
assertGet(RESUME_1);
assertGet(RESUME_2);
assertGet(RESUME_3);
Expand Down
Loading