Skip to content

Commit 993e873

Browse files
committed
lesson 5
1 parent d009e7a commit 993e873

File tree

9 files changed

+282
-47
lines changed

9 files changed

+282
-47
lines changed

src/com/urise/webapp/storage/AbstractArrayStorage.java

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.urise.webapp.storage;
22

3-
import com.urise.webapp.exception.ExistStorageException;
4-
import com.urise.webapp.exception.NotExistStorageException;
53
import com.urise.webapp.exception.StorageException;
64
import com.urise.webapp.model.Resume;
75

86
import java.util.Arrays;
97

10-
public abstract class AbstractArrayStorage implements Storage {
8+
public abstract class AbstractArrayStorage extends AbstractStorage {
119
protected static final int MAX_SIZE = 10_0000;
1210
protected Resume[] storage = new Resume[MAX_SIZE];
1311
protected int resumeCounter = 0;
@@ -17,62 +15,51 @@ public void clear() {
1715
resumeCounter = 0;
1816
}
1917

20-
public void save(Resume resume) {
21-
int index = getIndex(resume.getUuid());
22-
if (index >= 0) {
23-
throw new ExistStorageException(resume.getUuid());
24-
}
25-
if (resumeCounter >= MAX_SIZE) {
26-
throw new StorageException("Storage overflow", resume.getUuid());
27-
}
28-
insertResume(index, resume);
29-
resumeCounter++;
18+
@Override
19+
protected Resume doGet(Object existedSearchKey) {
20+
return storage[(Integer) existedSearchKey];
3021
}
3122

32-
public Resume get(String uuid) {
33-
int index = getIndex(uuid);
34-
if (index < 0) {
35-
throw new NotExistStorageException(uuid);
36-
}
37-
return storage[index];
38-
}
39-
40-
public void delete(String uuid) {
41-
int index = getIndex(uuid);
42-
if (index < 0) {
43-
throw new NotExistStorageException(uuid);
44-
}
45-
deleteResume(index);
23+
@Override
24+
public void doDelete(Object index) {
25+
deleteResume((Integer) index);
4626
storage[resumeCounter - 1] = null;
4727
resumeCounter--;
4828
}
4929

50-
/**
51-
* @return array, contains only Resumes in storage (without null)
52-
*/
53-
5430
public Resume[] getAll() {
5531
return Arrays.copyOf(storage, resumeCounter);
5632
}
5733

34+
@Override
35+
protected void doUpdate(Resume resume, Object index) {
36+
storage[(Integer) index] = resume;
37+
}
38+
39+
@Override
40+
protected void doSave(Resume resume, Object index) {
41+
if (resumeCounter >= MAX_SIZE) {
42+
throw new StorageException("Storage overflow", resume.getUuid());
43+
}
44+
insertResume((Integer) index, resume);
45+
resumeCounter++;
46+
}
47+
5848
public int size() {
5949
return resumeCounter;
6050
}
6151

62-
public void update(Resume resume) {
63-
int index = getIndex(resume.getUuid());
64-
if (index < 0) {
65-
throw new NotExistStorageException(resume.getUuid());
66-
}
67-
storage[index] = resume;
52+
@Override
53+
protected boolean isExist(Object index) {
54+
return (Integer) index >= 0;
6855
}
69-
/*
70-
method returns:
71-
1. Positive Index in storage, including zero, for existing element
72-
2. Negative index for absent element. Result equals |index| of element, wich is next to absent element.
73-
*/
7456

75-
protected abstract int getIndex(String uuid);
57+
/*
58+
method returns:
59+
1. Positive Index in storage, including zero, for existing element
60+
2. Negative index for absent element. Result equals |index| of element, wich is next to absent element.
61+
*/
62+
protected abstract Integer getSearchKey(String uuid);
7663

7764
/*
7865
* parametr index equals index of element in storage, wich is next to inserting element
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.exception.ExistStorageException;
4+
import com.urise.webapp.exception.NotExistStorageException;
5+
import com.urise.webapp.model.Resume;
6+
7+
public abstract class AbstractStorage implements Storage {
8+
9+
protected abstract void doUpdate(Resume resume, Object searchKey);
10+
11+
protected abstract boolean isExist(Object searchKey);
12+
13+
protected abstract void doSave(Resume resume, Object searchKey);
14+
15+
protected abstract Resume doGet(Object existedSearchKey);
16+
17+
protected abstract void doDelete(Object searchKey);
18+
19+
protected abstract Object getSearchKey(String uuid);
20+
21+
public void save(Resume resume) {
22+
doSave(resume, getNotExistedSearchKey(resume.getUuid()));
23+
}
24+
25+
public void delete(String uuid) {
26+
doDelete(getExistedSearchKey(uuid));
27+
}
28+
29+
public void update(Resume resume) {
30+
doUpdate(resume, getExistedSearchKey(resume.getUuid()));
31+
}
32+
33+
public Resume get(String uuid) {
34+
return doGet(getExistedSearchKey(uuid));
35+
}
36+
37+
38+
private Object getExistedSearchKey(String uuid) {
39+
Object searchKey = getSearchKey(uuid);
40+
if (!isExist(searchKey)) {
41+
throw new NotExistStorageException(uuid);
42+
}
43+
return searchKey;
44+
}
45+
46+
private Object getNotExistedSearchKey(String uuid) {
47+
Object searchKey = getSearchKey(uuid);
48+
if (isExist(searchKey)) {
49+
throw new ExistStorageException(uuid);
50+
}
51+
return searchKey;
52+
}
53+
54+
/**
55+
* @return array, contains only Resumes in storage (without null)
56+
*/
57+
58+
59+
}

src/com/urise/webapp/storage/ArrayStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class ArrayStorage extends AbstractArrayStorage {
99

10-
protected int getIndex(String uuid) {
10+
protected Integer getSearchKey(String uuid) {
1111
for (int i = 0; i < resumeCounter; i++) {
1212
if (storage[i].getUuid().equals(uuid)) {
1313
return i;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.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 void doUpdate(Resume resume, Object searchKey) {
13+
list.set((Integer) searchKey, resume);
14+
}
15+
16+
@Override
17+
protected boolean isExist(Object searchKey) {
18+
return searchKey != null;
19+
}
20+
21+
@Override
22+
protected void doSave(Resume resume, Object searchKey) {
23+
list.add(resume);
24+
}
25+
26+
@Override
27+
protected Resume doGet(Object searchKey) {
28+
return list.get((Integer) searchKey);
29+
}
30+
31+
@Override
32+
protected void doDelete(Object searchKey) {
33+
list.remove(((Integer) searchKey).intValue());
34+
}
35+
36+
@Override
37+
protected Integer getSearchKey(String uuid) {
38+
for (int i=0;i<list.size();i++) {
39+
if (list.get(i).getUuid().equals(uuid)){
40+
return i;
41+
}
42+
}
43+
return null;
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+
}

src/com/urise/webapp/storage/SortedArrayStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class SortedArrayStorage extends AbstractArrayStorage {
88
@Override
9-
protected int getIndex(String uuid) {
9+
protected Integer getSearchKey(String uuid) {
1010
Resume resume = new Resume(uuid);
1111
return Arrays.binarySearch(storage, 0, resumeCounter, resume);
1212
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.exception.ExistStorageException;
4+
import com.urise.webapp.exception.NotExistStorageException;
5+
import com.urise.webapp.exception.StorageException;
6+
import com.urise.webapp.model.Resume;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
11+
import static com.urise.webapp.storage.AbstractArrayStorage.MAX_SIZE;
12+
import static org.junit.Assert.*;
13+
14+
public abstract class AbstractStorageTest {
15+
private static final String UUID_1 = "UUID_1";
16+
private static final String UUID_2 = "UUID_2";
17+
private static final String UUID_3 = "UUID_3";
18+
private static final String UUID_4 = "UUID_4";
19+
private static final Resume R_1 = new Resume(UUID_1);
20+
private static final Resume R_2 = new Resume(UUID_2);
21+
private static final Resume R_4 = new Resume(UUID_4);
22+
protected Storage storage;
23+
private static final Resume R_3 = new Resume(UUID_3);
24+
25+
protected AbstractStorageTest(Storage storage) {
26+
this.storage = storage;
27+
}
28+
29+
/*создается перед каждым методом*/
30+
@Before
31+
public void setUp() {
32+
storage.clear();
33+
storage.save(R_1);
34+
storage.save(R_2);
35+
storage.save(R_3);
36+
}
37+
38+
@Test
39+
public void clear() {
40+
storage.clear();
41+
assertEquals(0, storage.size());
42+
}
43+
44+
@Test(expected = ExistStorageException.class)
45+
public void saveExist() {
46+
storage.save(R_3);
47+
}
48+
49+
@Test
50+
public void saveNotExist() {
51+
int sizeBefore = storage.size();
52+
storage.save(R_4);
53+
assertEquals(storage.size(), sizeBefore + 1);
54+
assertEquals(R_4, storage.get(UUID_4));
55+
}
56+
57+
@Test
58+
public void getExist() {
59+
assertEquals(storage.get(UUID_1), R_1);
60+
assertEquals(storage.get(UUID_2), R_2);
61+
assertEquals(storage.get(UUID_3), R_3);
62+
}
63+
64+
@Test(expected = NotExistStorageException.class)
65+
public void getNotExist() {
66+
storage.get("notExistableUUID");
67+
}
68+
69+
@Test(expected = NotExistStorageException.class)
70+
public void deleteExist() {
71+
int sizeBefore = storage.size();
72+
storage.delete(UUID_1);
73+
assertEquals(storage.size(), sizeBefore - 1);
74+
storage.get(UUID_1);
75+
76+
}
77+
78+
@Test(expected = NotExistStorageException.class)
79+
public void deleteNotExist() {
80+
storage.delete("notExistableUUID");
81+
}
82+
83+
@Test
84+
public void getAll() {
85+
assertArrayEquals( new Resume[]{R_1, R_2, R_3},storage.getAll());
86+
}
87+
88+
@Test
89+
public void size() {
90+
assertEquals(storage.size(), 3);
91+
}
92+
93+
@Test(expected = AssertionError.class)
94+
public void sizeOverLoaded() {
95+
try {
96+
if (!(storage instanceof AbstractArrayStorage)) {
97+
throw new StorageException("Is not instance of AbstractArrayStorage",null );
98+
}
99+
for (int i = 4; i <= MAX_SIZE; i++) {
100+
storage.save(new Resume());
101+
}
102+
storage.save(new Resume());
103+
} catch (StorageException e) {
104+
fail();
105+
}
106+
107+
}
108+
109+
@Test
110+
public void updateExist() {
111+
Resume rBefore = storage.get(UUID_1);
112+
Resume rAfter = new Resume(UUID_1);
113+
storage.update(rAfter);
114+
assertNotSame(storage.get(UUID_1), rBefore);
115+
}
116+
117+
@Test(expected = NotExistStorageException.class)
118+
public void updateNotExist() {
119+
Resume rAfter = new Resume("test");
120+
storage.update(rAfter);
121+
}
122+
}

test/com/urise/webapp/storage/ArrayStorageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.urise.webapp.storage;
22

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

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

test/com/urise/webapp/storage/SortedArrayStorageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.urise.webapp.storage;
22

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

55
public SortedArrayStorageTest() {
66
super(new SortedArrayStorage());

0 commit comments

Comments
 (0)