Skip to content

Commit 25ecc3b

Browse files
committed
lesson 6. Исправление ошибок
1 parent 8df83af commit 25ecc3b

14 files changed

+84
-108
lines changed
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
package com.urise.webapp;
22

33
import com.urise.webapp.model.Resume;
4-
import com.urise.webapp.storage.SortedArrayStorage;
54

65
import java.lang.reflect.InvocationTargetException;
76
import java.lang.reflect.Method;
8-
import java.util.Arrays;
97

108
public class MainReflection {
11-
12-
private static final String UUID_1 = "UUID_1";
13-
private static final String UUID_2 = "UUID_2";
14-
private static final String UUID_3 = "UUID_3";
15-
private static final String UUID_4 = "UUID_4";
16-
private static final Resume R_1 = new Resume(UUID_1,"FIO_1");
17-
private static final Resume R_2 = new Resume(UUID_2,"FIO_2");
18-
private static final Resume R_3 = new Resume(UUID_3,"FIO_3");
19-
private static final Resume R_4 = new Resume(UUID_4,"FIO_4");
20-
219
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
2210
Resume test = new Resume("test");
11+
System.out.println(test);
2312
Method method = test.getClass().getMethod("toString");
2413
System.out.println(method.invoke(test));
25-
SortedArrayStorage storage = new SortedArrayStorage();
26-
storage.getStorage()[0]=R_1;
27-
storage.getStorage()[1]=R_2;
28-
storage.getStorage()[2]=R_3;
29-
30-
storage.save(R_4);
3114

32-
System.out.println(storage.getStorage().length);
33-
System.out.println(Arrays.binarySearch(storage.getStorage(),0,9, R_2));
3415
}
3516

36-
37-
3817
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.urise.webapp.model;
22

3-
import java.util.Comparator;
3+
import java.util.Objects;
44
import java.util.UUID;
55

66
/**
77
* Initial resume class
88
*/
9-
public class Resume implements Comparator<Resume>, Comparable<Resume> {
9+
public class Resume implements Comparable<Resume> {
1010

1111
// Unique identifier
1212
private final String uuid;
@@ -17,6 +17,8 @@ public Resume(String fullName) {
1717
}
1818

1919
public Resume(String uuid, String fullName) {
20+
Objects.requireNonNull(fullName, "fullName must not be null");
21+
Objects.requireNonNull(uuid, "uuid must not be null");
2022
this.uuid = uuid;
2123
this.fullName = fullName;
2224
}
@@ -31,34 +33,28 @@ public String getFullName() {
3133

3234
@Override
3335
public String toString() {
34-
return uuid;
36+
return uuid + "(" + fullName + ")";
3537
}
3638

37-
@Override
38-
public int compare(Resume o1, Resume o2) {
39-
return o1.getUuid().equals(o2.getUuid())?
40-
o1.getFullName().compareTo(o2.getFullName()):
41-
o1.getUuid().compareTo(o2.getUuid());
42-
}
43-
44-
4539
@Override
4640
public boolean equals(Object o) {
4741
if (this == o) return true;
4842
if (o == null || getClass() != o.getClass()) return false;
49-
5043
Resume resume = (Resume) o;
51-
44+
if (!uuid.equals(resume.uuid)) return false;
5245
return uuid.equals(resume.uuid);
5346
}
5447

5548
@Override
5649
public int hashCode() {
57-
return uuid.hashCode();
50+
int result = uuid.hashCode();
51+
result = 31 * result + fullName.hashCode();
52+
return result;
5853
}
5954

6055
@Override
6156
public int compareTo(Resume o) {
62-
return this.getFullName().equals(o.fullName)?this.getUuid().compareTo(o.getUuid()):this.getFullName().compareTo(o.getFullName());
57+
int cmp = fullName.compareTo(o.fullName);
58+
return cmp != 0 ? cmp : uuid.compareTo(o.getUuid());
6359
}
6460
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import java.util.stream.Stream;
1010

1111
public abstract class AbstractArrayStorage extends AbstractStorage {
12-
protected static final int MAX_SIZE = 10_0000;
13-
14-
public Resume[] getStorage() {
15-
return storage;
16-
}
12+
protected static final int MAX_SIZE = 10_000;
1713

1814
protected Resume[] storage = new Resume[MAX_SIZE];
1915
protected int resumeCounter = 0;
@@ -24,8 +20,8 @@ public void clear() {
2420
}
2521

2622
@Override
27-
protected Resume doGet(Object existedSearchKey) {
28-
return storage[(Integer) existedSearchKey];
23+
protected Resume doGet(Object index) {
24+
return storage[(Integer) index];
2925
}
3026

3127
@Override
@@ -49,13 +45,13 @@ protected void doSave(Resume resume, Object index) {
4945
if (resumeCounter >= MAX_SIZE) {
5046
throw new StorageException("Storage overflow", resume.getUuid());
5147
}
52-
insertResume((Integer) index, resume);
48+
insertResume(resume,(Integer) index);
5349
resumeCounter++;
5450
}
5551

5652
@Override
57-
protected Stream<Resume> getStream() {
58-
return Arrays.stream(storage,0,resumeCounter);
53+
protected List<Resume> doCopyAll() {
54+
return Arrays.asList(Arrays.copyOfRange(storage,0,resumeCounter));
5955
}
6056

6157
public int size() {
@@ -67,7 +63,7 @@ protected boolean isExist(Object index) {
6763
return (Integer) index >= 0;
6864
}
6965

70-
protected abstract void insertResume(int index, Resume resume);
66+
protected abstract void insertResume(Resume resume, int index);
7167

7268
protected abstract void deleteResume(int index);
7369
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,37 @@
33
import com.urise.webapp.exception.ExistStorageException;
44
import com.urise.webapp.exception.NotExistStorageException;
55
import com.urise.webapp.model.Resume;
6+
7+
import java.util.Collections;
8+
import java.util.Comparator;
69
import java.util.List;
7-
import java.util.stream.Collectors;
8-
import java.util.stream.Stream;
10+
911

1012
public abstract class AbstractStorage implements Storage {
13+
Comparator<Resume> RESUME_COMAPATOR= Comparator.comparing(Resume::getUuid);
1114

1215
protected abstract void doUpdate(Resume resume, Object searchKey);
1316

1417
protected abstract boolean isExist(Object searchKey);
1518

1619
protected abstract void doSave(Resume resume, Object searchKey);
1720

18-
protected abstract Resume doGet(Object existedSearchKey);
21+
protected abstract Resume doGet(Object searchKey);
1922

2023
protected abstract void doDelete(Object searchKey);
2124

2225
protected abstract Object getSearchKey(String uuid);
2326

24-
protected abstract Stream<Resume> getStream();
27+
protected abstract List<Resume> doCopyAll();
2528

2629
public List<Resume> getAllSorted(){
27-
return getStream().sorted().collect(Collectors.toList());
30+
List<Resume> list =doCopyAll();
31+
Collections.sort(list);
32+
return list;
2833
}
2934

35+
36+
3037
public void save(Resume resume) {
3138
doSave(resume, getNotExistedSearchKey(resume.getUuid()));
3239
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected Integer getSearchKey(String uuid) {
1919

2020

2121
@Override
22-
protected void insertResume(int index, Resume resume) {
22+
protected void insertResume(Resume resume,int index) {
2323
storage[resumeCounter] = resume;
2424
}
2525

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public void clear() {
5151
}
5252

5353
@Override
54-
protected Stream<Resume> getStream() {
55-
return list.stream();
54+
protected List<Resume> doCopyAll() {
55+
return new ArrayList<>(list);
5656
}
5757

5858
@Override

src/com/urise/webapp/storage/MapStoragePairedKey.java renamed to src/com/urise/webapp/storage/MapResumeStorage.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
import com.urise.webapp.model.Resume;
44

5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
5+
import java.lang.reflect.Array;
6+
import java.util.*;
87
import java.util.stream.Collectors;
98
import java.util.stream.Stream;
109

11-
public class MapStoragePairedKey extends AbstractStorage {
10+
public class MapResumeStorage extends AbstractStorage {
1211
private Map<String, Resume> mapStorage = new HashMap<>();
1312

1413
@Override
1514
protected void doUpdate(Resume resume, Object searchKey) {
15+
1616
mapStorage.put(((Resume)searchKey).getUuid(), resume);
1717
}
1818

1919
@Override
2020
protected boolean isExist(Object searchKey) {
21-
return searchKey==null?false:true;
21+
return searchKey != null;
2222
}
2323

2424
@Override
@@ -28,27 +28,29 @@ protected void doSave(Resume resume, Object searchKey) {
2828

2929
@Override
3030
protected Resume doGet(Object searchKey) {
31+
3132
return (Resume)searchKey;
3233
}
3334

3435
@Override
3536
protected void doDelete(Object searchKey) {
37+
3638
mapStorage.remove(((Resume)searchKey).getUuid());
3739
}
3840

3941
@Override
40-
protected Object getSearchKey(String uuid) {
42+
protected Resume getSearchKey(String uuid) {
4143
return mapStorage.get(uuid);
4244
}
4345

4446
@Override
45-
public void clear() {
46-
mapStorage.clear();
47+
protected List<Resume> doCopyAll() {
48+
return new ArrayList(mapStorage.values());
4749
}
4850

4951
@Override
50-
public Stream<Resume> getStream() {
51-
return mapStorage.values().stream();
52+
public void clear() {
53+
mapStorage.clear();
5254
}
5355

5456
@Override

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import com.urise.webapp.model.Resume;
44

5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
import java.util.stream.Collectors;
9-
import java.util.stream.Stream;
5+
import java.util.*;
6+
107

118
public class MapStorage extends AbstractStorage {
12-
private Map<String, Resume> mapStorage = new HashMap();
9+
private Map<String, Resume> mapStorage = new HashMap<>();
1310

1411
@Override
1512
protected void doUpdate(Resume resume, Object searchKey) {
@@ -47,8 +44,8 @@ public void clear() {
4744
}
4845

4946
@Override
50-
protected Stream<Resume> getStream() {
51-
return mapStorage.values().stream();
47+
protected List<Resume> doCopyAll() {
48+
return new ArrayList<>(mapStorage.values());
5249
}
5350

5451
@Override

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
public class SortedArrayStorage extends AbstractArrayStorage {
1313
@Override
1414
protected Integer getSearchKey(String uuid) {
15-
Resume resume = new Resume(uuid,"FIO_1");
16-
if (resumeCounter==0) return -1;
17-
String[] uuids = Arrays.stream(storage).limit(resumeCounter).map(r->r.getUuid()).toArray(String[]::new);
15+
Resume resume = new Resume(uuid,"defaultFullName");
16+
// if (resumeCounter==0) return -1;
17+
// String[] uuids = Arrays.stream(storage).limit(resumeCounter).map(r->r.getUuid()).toArray(String[]::new);
1818
// Arrays.stream(storage).filter(r->r.getUuid().equals(uuid)).count();
1919
//return Arrays.binarySearch(storage, 0, resumeCounter, resume);
2020
/* Arrays.stream(storage)
2121
.sorted(Comparator.comparing(Resume::getUuid))
2222
.toArray();*/
23-
return Arrays.binarySearch(uuids,uuid);
23+
//return Arrays.binarySearch(uuids,uuid);
24+
return Arrays.binarySearch(storage,0,resumeCounter,resume,RESUME_COMAPATOR);
2425
}
2526

2627
@Override
27-
protected void insertResume(int index, Resume resume) {
28+
protected void insertResume(Resume resume, int index) {
2829
int indexResume = -index - 1;
2930
System.arraycopy(storage, indexResume, storage, indexResume + 1, resumeCounter - indexResume);
3031
storage[indexResume] = resume;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import com.urise.webapp.model.Resume;
55
import org.junit.Test;
66

7-
87
import static com.urise.webapp.storage.AbstractArrayStorage.MAX_SIZE;
98
import static org.junit.Assert.fail;
109

11-
public abstract class AbstractArrayStorageTest extends AbstractStorageTest{
10+
public abstract class AbstractArrayStorageTest extends AbstractStorageTest {
1211

1312
public AbstractArrayStorageTest(AbstractArrayStorage storage) {
1413
super(storage);
1514
}
15+
1616
@Test(expected = StorageException.class)
1717
public void sizeOverLoaded() {
1818
try {
@@ -23,6 +23,6 @@ public void sizeOverLoaded() {
2323
} catch (StorageException e) {
2424
fail();
2525
}
26-
storage.save(new Resume("FIO_" + MAX_SIZE+1));
26+
storage.save(new Resume("FIO_" + MAX_SIZE + 1));
2727
}
2828
}

0 commit comments

Comments
 (0)