Skip to content
9 changes: 4 additions & 5 deletions src/com/urise/webapp/MainArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
* Interactive test for ArrayStorage implementation
Expand Down Expand Up @@ -61,14 +62,12 @@ public static void main(String[] args) throws IOException {
}

static void printAll() {
Resume[] all = ARRAY_STORAGE.getAll();
List<Resume> allResumes = ARRAY_STORAGE.getAllSorted();
System.out.println("----------------------------");
if (all.length == 0) {
if (allResumes.size() == 0) {
System.out.println("Empty");
} else {
for (Resume r : all) {
System.out.println(r);
}
allResumes.forEach(System.out::println);
}
System.out.println("----------------------------");
}
Expand Down
10 changes: 3 additions & 7 deletions src/com/urise/webapp/MainTestArrayStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ public static void main(String[] args) {
resultOfSearch = ARRAY_STORAGE.get("dummy");
System.out.println("Result of get(\"dummy\"): " + resultOfSearch);

System.out.println("\n*** Test of getAll() method***");
for (Resume r : ARRAY_STORAGE.getAll()) {
System.out.println(r);
}
System.out.println("\n*** Test of getAllSorted() method***");
ARRAY_STORAGE.getAllSorted().forEach(System.out::println);

System.out.println("\n*** Testing the delete(\"uuid5\") method***");
ARRAY_STORAGE.delete("uuid5");
Expand All @@ -107,8 +105,6 @@ public static void main(String[] args) {

static void printAll() {
System.out.println("Print All");
for (Resume r : ARRAY_STORAGE.getAll()) {
System.out.println(r);
}
ARRAY_STORAGE.getAllSorted().forEach(System.out::println);
}
}
8 changes: 2 additions & 6 deletions src/com/urise/webapp/MainTestSortedArrayStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ public static void main(String[] args) {
System.out.println("Result of get(\"dummy\"): " + resultOfSearch);

System.out.println("\n*** Test of getAll() method***");
for (Resume r : ARRAY_STORAGE.getAll()) {
System.out.println(r);
}
ARRAY_STORAGE.getAllSorted().forEach(System.out::println);

System.out.println("\n*** Testing the delete(\"uuid5\") method***");
ARRAY_STORAGE.delete("uuid5");
Expand All @@ -107,8 +105,6 @@ public static void main(String[] args) {

static void printAll() {
System.out.println("Print All");
for (Resume r : ARRAY_STORAGE.getAll()) {
System.out.println(r);
}
ARRAY_STORAGE.getAllSorted().forEach(System.out::println);
}
}
23 changes: 18 additions & 5 deletions src/com/urise/webapp/model/Resume.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package com.urise.webapp.model;

import java.util.Objects;
import java.util.UUID;

/**
* Initial resume class
* 1 * Initial resume class
*/
public class Resume implements Comparable<Resume> {

private final String uuid;
private String fullName;

public Resume() {
this(UUID.randomUUID().toString());
public Resume(String fullName) {
this(UUID.randomUUID().toString(), fullName);
}

public Resume(String uuid) {
public Resume(String uuid, String fullName) {
Objects.requireNonNull(uuid, "uuid must not be null");
Objects.requireNonNull(fullName, "fullName must not be null");
this.uuid = uuid;
this.fullName = fullName;
}

public String getUuid() {
return uuid;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -38,7 +51,7 @@ public int hashCode() {

@Override
public String toString() {
return uuid;
return String.format("UUID: %s FullName: %s", uuid, fullName);
}

@Override
Expand Down
70 changes: 26 additions & 44 deletions src/com/urise/webapp/storage/AbstractArrayStorage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.urise.webapp.storage;

import com.urise.webapp.exception.ExistStorageException;
import com.urise.webapp.exception.NotExistStorageException;
import com.urise.webapp.exception.StorageException;
import com.urise.webapp.model.Resume;

Expand All @@ -10,81 +8,65 @@
/**
* Array based storage for Resumes
*/
public abstract class AbstractArrayStorage implements Storage {
public abstract class AbstractArrayStorage extends AbstractStorage<Integer> {
protected static final int STORAGE_LIMIT = 10_000;
protected final Resume[] storage = new Resume[STORAGE_LIMIT];
protected int size = 0;

@Override
public void clear() {
public void doClear() {
Arrays.fill(storage, 0, size, null);
size = 0;
}

@Override
public final void delete(String uuid) {
int index = getIndex(uuid);
if (index < 0) {
System.out.println("Ошибка! В базе отсутствует резюме с uuid = " + uuid);
throw new NotExistStorageException(uuid);
} else {
deleteResume(index);
size--;
}
public final void doDelete(Integer searchKey) {
deleteResume(searchKey);
}

@Override
public final Resume get(String uuid) {
int index = getIndex(uuid);
if (index < 0) {
System.out.println("Ошибка! В базе отсутствует резюме с uuid = " + uuid);
throw new NotExistStorageException(uuid);
}
public final Resume doGet(Integer index) {

return storage[index];
}

@Override
public Resume[] getAll() {
public Resume[] doGetAll() {
return Arrays.copyOf(storage, size);
}

@Override
public final void save(Resume resume) {
String uuid = resume.getUuid();
int index = getIndex(uuid);
public final void doSave(Integer searchKey, Resume r) {
if (size >= STORAGE_LIMIT) {
System.out.println("Ошибка сохранения! Закончилось место в хранилище.");
throw new StorageException("The storage overflow!", uuid);
} else if (index >= 0) {
System.out.println("Ошибка сохранения! Резюме с uuid = '" + uuid + "' уже присутствует в хранилище.");
throw new ExistStorageException(uuid);
} else {
index = -1 - index;
saveResume(index, resume);
size++;
throw new StorageException("The storage is overflow!", r.getUuid());
}
saveResume(searchKey, r);
}

@Override
public int size() {
public int doSize() {
return size;
}

@Override
public final void update(Resume resume) {
String uuid = resume.getUuid();
int index = getIndex(uuid);
if (index < 0) {
System.out.println("Ошибка обновления резюме! В базе не найдено резюме с uuid = " + uuid);
throw new NotExistStorageException(uuid);
} else {
storage[index] = resume;
}
protected void doUpdate(Integer searchKey, Resume r) {
storage[searchKey] = r;
}

@Override
protected Integer getSearchKey(String uuid) {
return getIndex(uuid);
}

@Override
protected boolean isExist(Integer searchKey) {
return searchKey > -1;
}

protected abstract void deleteResume(int index);
protected abstract void deleteResume(Integer searchKey);

public abstract int getIndex(String uuid);
protected abstract Integer getIndex(String uuid);

protected abstract void saveResume(int index, Resume resume);
protected abstract void saveResume(Integer index, Resume resume);
}
91 changes: 91 additions & 0 deletions src/com/urise/webapp/storage/AbstractStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractStorage<SK> implements Storage {
@Override
public void clear() {
doClear();
}

@Override
public void delete(String uuid) {
doDelete(getExistSearchKey(uuid));
}

@Override
public Resume get(String uuid) {
return doGet(getExistSearchKey(uuid));
}

@Override
public Resume[] getAll() {
return doGetAll();
}

@Override
public List<Resume> getAllSorted() {
return Arrays.stream(getAll())
.sorted((r1, r2) -> {
int c = r1.getFullName().compareTo(r2.getFullName());
return (c != 0) ? c : r1.getUuid().compareTo(r2.getUuid());
})
.collect(Collectors.toList());
}

@Override
public void save(Resume r) {
doSave(getNotExistSearchKey(r.getUuid()), r);
}

@Override
public int size() {
return doSize();
}

@Override
public void update(Resume r) {
SK searchKey = getExistSearchKey(r.getUuid());
doUpdate(searchKey, r);
}

private SK getExistSearchKey(String uuid) {
SK searchKey = getSearchKey(uuid);
if (!isExist(searchKey)) {
throw new NotExistStorageException(uuid);
}
return searchKey;
}

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

protected abstract void doClear();

protected abstract void doDelete(SK searchKey);

protected abstract Resume doGet(SK searchKey);

protected abstract Resume[] doGetAll();

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

protected abstract int doSize();

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

protected abstract SK getSearchKey(String uuid);

protected abstract boolean isExist(SK searchKey);
}
12 changes: 7 additions & 5 deletions src/com/urise/webapp/storage/ArrayStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
public class ArrayStorage extends AbstractArrayStorage {

@Override
protected void deleteResume(int index) {
storage[index] = storage[size - 1];
storage[size - 1] = null;
protected void deleteResume(Integer searchKey) {
size--;
storage[searchKey] = storage[size];
storage[size] = null;
}

@Override
public int getIndex(String uuid) {
protected Integer getIndex(String uuid) {
for (int i = 0; i < size; i++) {
if (storage[i].getUuid().equals(uuid)) {
return i;
Expand All @@ -24,7 +25,8 @@ public int getIndex(String uuid) {
}

@Override
protected void saveResume(int index, Resume resume) {
protected void saveResume(Integer index, Resume resume) {
storage[size] = resume;
size++;
}
}
Loading