Skip to content

Commit 8274858

Browse files
author
Tanechka
committed
Lesson06 HW05
1 parent 6def59a commit 8274858

11 files changed

Lines changed: 223 additions & 40 deletions

File tree

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ru.javawebinar.basejava.storage;
22

3-
import ru.javawebinar.basejava.exception.ExistStorageException;
4-
import ru.javawebinar.basejava.exception.NotExistStorageException;
53
import ru.javawebinar.basejava.exception.StorageException;
64
import ru.javawebinar.basejava.model.Resume;
75

@@ -10,7 +8,7 @@
108
/**
119
* Array based storage for Resumes
1210
*/
13-
public abstract class AbstractArrayStorage implements Storage {
11+
public abstract class AbstractArrayStorage extends AbstractStorage {
1412
protected static final int STORAGE_LIMIT = 10000;
1513

1614
protected Resume[] storage = new Resume[STORAGE_LIMIT];
@@ -25,13 +23,9 @@ public void clear() {
2523
size = 0;
2624
}
2725

28-
public void update(Resume r) {
29-
int index = getIndex(r.getUuid());
30-
if (index < 0) {
31-
throw new NotExistStorageException(r.getUuid());
32-
} else {
33-
storage[index] = r;
34-
}
26+
@Override
27+
protected void doUpdate(Resume r, Object index) {
28+
storage[(Integer) index] = r;
3529
}
3630

3731
/**
@@ -41,40 +35,35 @@ public Resume[] getAll() {
4135
return Arrays.copyOfRange(storage, 0, size);
4236
}
4337

44-
public void save(Resume r) {
45-
int index = getIndex(r.getUuid());
46-
if (index >= 0) {
47-
throw new ExistStorageException(r.getUuid());
48-
} else if (size == STORAGE_LIMIT) {
38+
@Override
39+
protected void doSave(Resume r, Object index) {
40+
if (size == STORAGE_LIMIT) {
4941
throw new StorageException("Storage overflow", r.getUuid());
5042
} else {
51-
insertElement(r, index);
43+
insertElement(r, (Integer) index);
5244
size++;
5345
}
5446
}
5547

56-
public void delete(String uuid) {
57-
int index = getIndex(uuid);
58-
if (index < 0) {
59-
throw new NotExistStorageException(uuid);
60-
} else {
61-
fillDeletedElement(index);
62-
storage[size - 1] = null;
63-
size--;
64-
}
48+
@Override
49+
public void doDelete(Object index) {
50+
fillDeletedElement((Integer) index);
51+
storage[size - 1] = null;
52+
size--;
6553
}
6654

67-
public Resume get(String uuid) {
68-
int index = getIndex(uuid);
69-
if (index < 0) {
70-
throw new NotExistStorageException(uuid);
71-
}
72-
return storage[index];
55+
public Resume doGet(Object index) {
56+
return storage[(Integer) index];
57+
}
58+
59+
@Override
60+
protected boolean isExist(Object index) {
61+
return (Integer) index >= 0;
7362
}
7463

7564
protected abstract void fillDeletedElement(int index);
7665

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

79-
protected abstract int getIndex(String uuid);
68+
protected abstract Integer getSearchKey(String uuid);
8069
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.exception.ExistStorageException;
4+
import ru.javawebinar.basejava.exception.NotExistStorageException;
5+
import ru.javawebinar.basejava.model.Resume;
6+
7+
public abstract class AbstractStorage implements Storage {
8+
9+
protected abstract Object getSearchKey(String uuid);
10+
11+
protected abstract void doUpdate(Resume r, Object searchKey);
12+
13+
protected abstract boolean isExist(Object searchKey);
14+
15+
protected abstract void doSave(Resume r, Object searchKey);
16+
17+
protected abstract Resume doGet(Object searchKey);
18+
19+
protected abstract void doDelete(Object searchKey);
20+
21+
public void update(Resume r) {
22+
Object searchKey = getExistedSearchKey(r.getUuid());
23+
doUpdate(r, searchKey);
24+
}
25+
26+
public void save(Resume r) {
27+
Object searchKey = getNotExistedSearchKey(r.getUuid());
28+
doSave(r, searchKey);
29+
}
30+
31+
public void delete(String uuid) {
32+
Object searchKey = getExistedSearchKey(uuid);
33+
doDelete(searchKey);
34+
}
35+
36+
public Resume get(String uuid) {
37+
Object searchKey = getExistedSearchKey(uuid);
38+
return doGet(searchKey);
39+
}
40+
41+
private Object getExistedSearchKey(String uuid) {
42+
Object searchKey = getSearchKey(uuid);
43+
if (!isExist(searchKey)) {
44+
throw new NotExistStorageException(uuid);
45+
}
46+
return searchKey;
47+
}
48+
49+
private Object getNotExistedSearchKey(String uuid) {
50+
Object searchKey = getSearchKey(uuid);
51+
if (isExist(searchKey)) {
52+
throw new ExistStorageException(uuid);
53+
}
54+
return searchKey;
55+
}
56+
}

‎src/ru/javawebinar/basejava/storage/ArrayStorage.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ protected void insertElement(Resume r, int index) {
1717
storage[size] = r;
1818
}
1919

20-
@Override
21-
protected int getIndex(String uuid) {
20+
protected Integer getSearchKey(String uuid) {
2221
for (int i = 0; i < size; i++) {
2322
if (uuid.equals(storage[i].getUuid())) {
2423
return i;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class ListStorage extends AbstractStorage {
9+
private List<Resume> list = new ArrayList<>();
10+
11+
@Override
12+
protected Integer getSearchKey(String uuid) {
13+
for (int i = 0; i < list.size(); i++) {
14+
if (list.get(i).getUuid().equals(uuid)) {
15+
return i;
16+
}
17+
}
18+
return null;
19+
}
20+
21+
@Override
22+
protected boolean isExist(Object searchKey) {
23+
return searchKey != null;
24+
}
25+
26+
@Override
27+
protected void doUpdate(Resume r, Object searchKey) {
28+
list.set((Integer) searchKey, r);
29+
}
30+
31+
@Override
32+
protected void doSave(Resume r, Object searchKey) {
33+
list.add(r);
34+
}
35+
36+
@Override
37+
protected Resume doGet(Object searchKey) {
38+
return list.get((Integer) searchKey);
39+
}
40+
41+
@Override
42+
protected void doDelete(Object searchKey) {
43+
list.remove(((Integer) searchKey).intValue());
44+
}
45+
46+
@Override
47+
public void clear() {
48+
list.clear();
49+
}
50+
51+
@Override
52+
public Resume[] getAll() {
53+
return list.toArray(new Resume[list.size()]);
54+
}
55+
56+
@Override
57+
public int size() {
58+
return list.size();
59+
}
60+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
public class MainUtil {
4+
public static void main(String[] args) {
5+
System.out.println(Integer.valueOf(-1) == Integer.valueOf(-1));
6+
System.out.println(Integer.valueOf(-1) == new Integer(-1));
7+
int result = getInt();
8+
System.out.println(result);
9+
}
10+
11+
private static Integer getInt() {
12+
return null;
13+
}
14+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
// TODO implement
9+
// TODO create new MapStorage with search key not uuid
10+
public class MapUuidStorage extends AbstractStorage {
11+
private Map<String, Resume> map = new HashMap<>();
12+
13+
@Override
14+
protected String getSearchKey(String uuid) {
15+
return uuid;
16+
}
17+
18+
@Override
19+
protected void doUpdate(Resume r, Object searchKey) {
20+
}
21+
22+
@Override
23+
protected boolean isExist(Object searchKey) {
24+
return false;
25+
}
26+
27+
@Override
28+
protected void doSave(Resume r, Object searchKey) {
29+
30+
}
31+
32+
@Override
33+
protected Resume doGet(Object searchKey) {
34+
return null;
35+
}
36+
37+
@Override
38+
protected void doDelete(Object searchKey) {
39+
40+
}
41+
42+
@Override
43+
public void clear() {
44+
45+
}
46+
47+
@Override
48+
public Resume[] getAll() {
49+
return new Resume[0];
50+
}
51+
52+
@Override
53+
public int size() {
54+
return 0;
55+
}
56+
}

‎src/ru/javawebinar/basejava/storage/SortedArrayStorage.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected void insertElement(Resume r, int index) {
2323
}
2424

2525
@Override
26-
protected int getIndex(String uuid) {
26+
protected Integer getSearchKey(String uuid) {
2727
Resume searchKey = new Resume(uuid);
2828
return Arrays.binarySearch(storage, 0, size, searchKey);
2929
}

test/ru/javawebinar/basejava/storage/AbstractArrayStorageTest.java renamed to test/ru/javawebinar/basejava/storage/AbstractStorageTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import static org.junit.Assert.assertEquals;
1212
import static org.junit.Assert.assertTrue;
1313

14-
public abstract class AbstractArrayStorageTest {
14+
public abstract class AbstractStorageTest {
1515
private Storage storage;
1616

1717
private static final String UUID_1 = "uuid1";
@@ -31,7 +31,7 @@ public abstract class AbstractArrayStorageTest {
3131
RESUME_4 = new Resume(UUID_4);
3232
}
3333

34-
protected AbstractArrayStorageTest(Storage storage) {
34+
protected AbstractStorageTest(Storage storage) {
3535
this.storage = storage;
3636
}
3737

@@ -87,10 +87,11 @@ public void saveExist() throws Exception {
8787
storage.save(RESUME_1);
8888
}
8989

90+
// TODO remain only for Arrays implementations
9091
@Test(expected = StorageException.class)
9192
public void saveOverflow() throws Exception {
9293
try {
93-
for (int i = 4; i <= AbstractArrayStorage.STORAGE_LIMIT+1; i++) {
94+
for (int i = 4; i <= AbstractArrayStorage.STORAGE_LIMIT; i++) {
9495
storage.save(new Resume());
9596
}
9697
} catch (StorageException e) {

‎test/ru/javawebinar/basejava/storage/ArrayStorageTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ru.javawebinar.basejava.storage;
22

3-
public class ArrayStorageTest extends AbstractArrayStorageTest {
3+
public class ArrayStorageTest extends AbstractStorageTest {
44

55
public ArrayStorageTest() {
66
super(new ArrayStorage());
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
public class ListStorageTest extends AbstractStorageTest {
4+
5+
public ListStorageTest() {
6+
super(new ListStorage());
7+
}
8+
}

0 commit comments

Comments
 (0)