diff --git a/src/com/urise/webapp/model/Resume.java b/src/com/urise/webapp/model/Resume.java index 45140d19..47c2f62d 100644 --- a/src/com/urise/webapp/model/Resume.java +++ b/src/com/urise/webapp/model/Resume.java @@ -5,7 +5,7 @@ /** * Initial resume class */ -public class Resume { +public class Resume implements Comparable { // Unique identifier private String uuid; @@ -39,4 +39,9 @@ public int hashCode() { public String toString() { return uuid; } + + @Override + public int compareTo(Resume o) { + return this.uuid.compareTo(o.uuid); + } } diff --git a/src/com/urise/webapp/storage/AbstractArrayStorage.java b/src/com/urise/webapp/storage/AbstractArrayStorage.java new file mode 100644 index 00000000..310f7ebb --- /dev/null +++ b/src/com/urise/webapp/storage/AbstractArrayStorage.java @@ -0,0 +1,26 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; +import java.util.Arrays; + +public abstract class AbstractArrayStorage implements Storage { + protected static final int STORAGE_LIMIT = 10000; + protected Resume[] storage = new Resume[STORAGE_LIMIT]; + protected int storageSize; + + public int size() { + return storageSize; + } + + public Resume get(String uuid) { + int index = getIndex(uuid); + if (index >= 0) { + return storage[index]; + } else { + System.out.println("Resume " + uuid + " isn't exists!"); + return null; + } + } + + protected abstract int getIndex(String uuid); +} diff --git a/src/com/urise/webapp/storage/ArrayStorage.java b/src/com/urise/webapp/storage/ArrayStorage.java index 169cca78..d9a72e67 100644 --- a/src/com/urise/webapp/storage/ArrayStorage.java +++ b/src/com/urise/webapp/storage/ArrayStorage.java @@ -6,11 +6,7 @@ /** * Array based storage for Resumes */ -public class ArrayStorage implements Storage { - - private static final int STORAGE_LIMIT = 10000; - private Resume[] storage = new Resume[STORAGE_LIMIT]; - private int storageSize; +public class ArrayStorage extends AbstractArrayStorage { public void clear() { Arrays.fill(storage,0, storageSize, null); @@ -68,11 +64,7 @@ public Resume[] getAll() { return Arrays.copyOf(storage, storageSize); } - public int size() { - return storageSize; - } - - private int getIndex(String uuid) { + protected int getIndex(String uuid) { for (int i = 0; i < storageSize; i++) { if (uuid.equals(storage[i].getUuid())) { return i; diff --git a/src/com/urise/webapp/storage/SortedArrayStorage.java b/src/com/urise/webapp/storage/SortedArrayStorage.java new file mode 100644 index 00000000..83015f10 --- /dev/null +++ b/src/com/urise/webapp/storage/SortedArrayStorage.java @@ -0,0 +1,39 @@ +package com.urise.webapp.storage; + +import com.urise.webapp.model.Resume; +import java.util.Arrays; + +public class SortedArrayStorage extends AbstractArrayStorage { + + @Override + public void clear() { + + } + + @Override + public void update(Resume r) { + + } + + @Override + public void save(Resume r) { + + } + + @Override + public void delete(String uuid) { + + } + + @Override + public Resume[] getAll() { + return new Resume[0]; + } + + @Override + protected int getIndex(String uuid) { + Resume searchKey = new Resume(); + searchKey.setUuid(uuid); + return Arrays.binarySearch(storage, 0, storageSize, searchKey); + } +}