Skip to content

Commit 4d70a68

Browse files
authored
feature/lesson05
+ Storage, AbstractArrayStorage, AbstractArrayStorageTest - method lenght() deleted, constant STORAGE_LENGTH is used + new classes: ListStorage, AbstractStorage, ListStorageTest, AbstractStorageTest
1 parent 6aea2fd commit 4d70a68

9 files changed

Lines changed: 245 additions & 140 deletions

File tree

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ version '1.0'
88

99
repositories {
1010
mavenLocal()
11-
mavenCentral()
12-
// maven { url "http://artapp/artifactory/public" }
11+
// mavenCentral()
12+
maven { url "http://artapp/artifactory/public/" }
1313
}
1414

1515
sourceSets {
@@ -23,11 +23,11 @@ sourceSets {
2323
}
2424

2525
configurations{
26-
compile {
26+
implementation {
2727
transitive = true
2828
}
2929
}
3030

3131
dependencies {
32-
compile 'junit:junit:4.13.1'
32+
implementation 'junit:junit:4.13.1'
3333
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
4-
#distributionUrl=http\://artapp/artifactory/gradle-wrapper-remote-cache/distributions/gradle-6.7.1-bin.zip
3+
#distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
4+
distributionUrl=http\://artapp/artifactory/gradle-wrapper-remote-cache/distributions/gradle-6.7.1-bin.zip
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists
77
file.encoding=utf-8

src/ru/topjava/webapp/storage/AbstractArrayStorage.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
/**
1111
* Array based storage for Resumes
1212
*/
13-
public abstract class AbstractArrayStorage implements Storage {
13+
public abstract class AbstractArrayStorage extends AbstractStorage implements Storage {
1414
protected static final int STORAGE_LENGTH = 10000;
1515
protected final Resume[] storage = new Resume[STORAGE_LENGTH];
1616
protected int size = 0;
1717

18-
protected abstract int getIndex(String uuid);
19-
20-
protected abstract void saveByIndex(int resumeIndex, Resume resume);
21-
22-
protected abstract void deleteByIndex(int resumeIndex);
18+
public void clear() {
19+
Arrays.fill(storage, 0, size, null);
20+
size = 0;
21+
}
2322

2423
public void save(Resume resume) {
2524
int resumeIndex = getIndex(resume.getUuid());
@@ -33,28 +32,23 @@ public void save(Resume resume) {
3332
}
3433
}
3534

36-
public void delete(String uuid) {
35+
public Resume get(String uuid) {
3736
int resumeIndex = getIndex(uuid);
3837
if (resumeIndex < 0) {
3938
throw new NotExistStorageException(uuid);
4039
} else {
41-
deleteByIndex(resumeIndex);
42-
storage[size - 1] = null;
43-
size--;
40+
return storage[resumeIndex];
4441
}
4542
}
4643

47-
public void clear() {
48-
Arrays.fill(storage, 0, size, null);
49-
size = 0;
50-
}
51-
52-
public Resume get(String uuid) {
44+
public void delete(String uuid) {
5345
int resumeIndex = getIndex(uuid);
5446
if (resumeIndex < 0) {
5547
throw new NotExistStorageException(uuid);
5648
} else {
57-
return storage[resumeIndex];
49+
deleteByIndex(resumeIndex);
50+
storage[size - 1] = null;
51+
size--;
5852
}
5953
}
6054

@@ -77,9 +71,4 @@ public void update(Resume resume) {
7771
storage[resumeIndex] = resume;
7872
}
7973
}
80-
81-
public int length() {
82-
return STORAGE_LENGTH;
83-
}
84-
8574
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.topjava.webapp.storage;
2+
3+
import ru.topjava.webapp.model.Resume;
4+
5+
import java.util.ArrayList;
6+
7+
public abstract class AbstractStorage implements Storage {
8+
9+
protected abstract int getIndex(String uuid);
10+
11+
protected abstract void saveByIndex(int resumeIndex, Resume resume);
12+
13+
protected abstract void deleteByIndex(int resumeIndex);
14+
15+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package ru.topjava.webapp.storage;
2+
3+
import ru.topjava.webapp.exception.ExistStorageException;
4+
import ru.topjava.webapp.exception.NotExistStorageException;
5+
import ru.topjava.webapp.exception.StorageException;
6+
import ru.topjava.webapp.model.Resume;
7+
8+
import java.util.ArrayList;
9+
10+
public class ListStorage extends AbstractStorage implements Storage {
11+
protected final ArrayList<Resume> storage = new ArrayList<>();
12+
13+
@Override
14+
protected int getIndex(String uuid) {
15+
final Resume tempResume = new Resume(uuid);
16+
return storage.indexOf(tempResume);
17+
}
18+
19+
@Override
20+
protected void saveByIndex(int resumeIndex, Resume resume) {
21+
storage.add(resume);
22+
}
23+
24+
@Override
25+
protected void deleteByIndex(int resumeIndex) {
26+
storage.remove(resumeIndex);
27+
}
28+
29+
public void clear() {
30+
storage.clear();
31+
}
32+
33+
public void save(Resume resume) {
34+
int resumeIndex = getIndex(resume.getUuid());
35+
if (resumeIndex >= 0) {
36+
throw new ExistStorageException(resume.getUuid());
37+
} else {
38+
saveByIndex(resumeIndex, resume);
39+
}
40+
}
41+
42+
public Resume get(String uuid) {
43+
int resumeIndex = getIndex(uuid);
44+
if (resumeIndex < 0) {
45+
throw new NotExistStorageException(uuid);
46+
} else {
47+
return storage.get(resumeIndex);
48+
}
49+
}
50+
51+
public void delete(String uuid) {
52+
int resumeIndex = getIndex(uuid);
53+
if (resumeIndex < 0) {
54+
throw new NotExistStorageException(uuid);
55+
} else {
56+
deleteByIndex(resumeIndex);
57+
}
58+
}
59+
60+
public Resume[] getAll() {
61+
final Resume[] tempArray = new Resume[this.size()];
62+
return storage.toArray(tempArray);
63+
}
64+
65+
public int size() {
66+
return storage.size();
67+
}
68+
69+
public void update(Resume resume) {
70+
int resumeIndex = getIndex(resume.getUuid());
71+
if (resumeIndex < 0) {
72+
throw new NotExistStorageException(resume.getUuid());
73+
} else {
74+
storage.set(resumeIndex, resume);
75+
}
76+
}
77+
}

src/ru/topjava/webapp/storage/Storage.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@ public interface Storage {
2727
int size();
2828

2929
void update(Resume resume);
30-
31-
int length();
3230
}
Lines changed: 31 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,32 @@
1-
package ru.topjava.webapp.storage;
2-
3-
import org.junit.Assert;
4-
import org.junit.Before;
5-
import org.junit.Test;
6-
import ru.topjava.webapp.exception.ExistStorageException;
7-
import ru.topjava.webapp.exception.NotExistStorageException;
8-
import ru.topjava.webapp.exception.StorageException;
9-
import ru.topjava.webapp.model.Resume;
10-
11-
public abstract class AbstractArrayStorageTest {
12-
private final Storage storage;
13-
private final Resume resume1;
14-
private final Resume resume2;
15-
private final Resume resume3;
16-
private final Resume resume4;
17-
18-
public AbstractArrayStorageTest(Storage storage) {
19-
this.storage = storage;
20-
resume1 = new Resume("uuid1");
21-
resume2 = new Resume("uuid2");
22-
resume3 = new Resume("uuid3");
23-
resume4 = new Resume("uuid4");
24-
}
25-
26-
@Before
27-
public void setUp() {
28-
storage.clear();
29-
storage.save(resume1);
30-
storage.save(resume2);
31-
storage.save(resume3);
32-
}
33-
34-
@Test
35-
public void save() {
36-
storage.save(resume4);
37-
Assert.assertEquals(resume4, storage.get("uuid4"));
38-
Assert.assertEquals(4, storage.size());
39-
}
40-
41-
@Test(expected = ExistStorageException.class)
42-
public void saveExist() {
43-
storage.save(resume2);
44-
}
45-
46-
@Test(expected = NotExistStorageException.class)
47-
public void delete() {
48-
try {
49-
storage.delete(resume2.getUuid());
50-
Assert.assertEquals(2, storage.size());
51-
} catch (NotExistStorageException e) {
52-
Assert.fail("NotExistStorageException thrown earlier, than expected");
53-
}
54-
storage.get("uuid2");
55-
}
56-
57-
@Test(expected = NotExistStorageException.class)
58-
public void deleteNotExist() {
59-
storage.delete(resume4.getUuid());
60-
}
61-
62-
@Test
63-
public void update() {
64-
final Resume resume3Update = new Resume("uuid3");
65-
storage.update(resume3Update);
66-
Assert.assertEquals(resume3Update, storage.get("uuid3"));
67-
}
68-
69-
@Test(expected = NotExistStorageException.class)
70-
public void updateNotExist() {
71-
storage.update(resume4);
72-
}
73-
74-
@Test
75-
public void get() {
76-
Assert.assertEquals(resume1, storage.get("uuid1"));
77-
}
78-
79-
@Test(expected = NotExistStorageException.class)
80-
public void getNotExist() {
81-
storage.get(resume4.getUuid());
82-
}
83-
84-
@Test
85-
public void getAll() {
86-
Assert.assertArrayEquals(new Resume[] {resume1, resume2, resume3}, storage.getAll());
87-
}
88-
89-
@Test
90-
public void size() {
91-
Assert.assertEquals(3, storage.size());
92-
}
93-
94-
@Test
95-
public void clear() {
96-
storage.clear();
97-
}
98-
99-
@Test(expected = StorageException.class)
100-
public void saveStorageFull() {
101-
int initialSize = storage.size();
102-
try {
103-
for (int i = 0; i < storage.length() - initialSize; i++) {
104-
storage.save(new Resume());
105-
}
106-
} catch (StorageException e) {
107-
Assert.fail("StorageException thrown earlier, than expected");
108-
}
109-
storage.save(new Resume());
110-
}
1+
package ru.topjava.webapp.storage;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import ru.topjava.webapp.exception.ExistStorageException;
7+
import ru.topjava.webapp.exception.NotExistStorageException;
8+
import ru.topjava.webapp.exception.StorageException;
9+
import ru.topjava.webapp.model.Resume;
10+
11+
import static ru.topjava.webapp.storage.AbstractArrayStorage.STORAGE_LENGTH;
12+
13+
public abstract class AbstractArrayStorageTest extends AbstractStorageTest{
14+
15+
public AbstractArrayStorageTest(Storage storage) {
16+
super(storage);
17+
}
18+
19+
@Test(expected = StorageException.class)
20+
public void saveStorageFull() {
21+
int initialSize = storage.size();
22+
try {
23+
for (int i = 0; i < STORAGE_LENGTH - initialSize; i++) {
24+
storage.save(new Resume());
25+
}
26+
} catch (StorageException e) {
27+
Assert.fail("StorageException thrown earlier, than expected");
28+
}
29+
storage.save(new Resume());
30+
}
31+
11132
}

0 commit comments

Comments
 (0)