Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
72 lines (56 loc) · 2.28 KB
/
Copy pathSolution.java
File metadata and controls
72 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.javarush.task.task33.task3310;
import com.javarush.task.task33.task3310.strategy.*;
import static com.javarush.task.task33.task3310.Helper.printMessage;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Solution {
public static Set<Long> getIds(Shortener shortener, Set<String> strings){
Set<Long> result = new HashSet<>();
for(String str : strings){
result.add(shortener.getId(str));
}
return result;
}
public static Set<String> getStrings(Shortener shortener, Set<Long> keys){
Set<String> result = new HashSet<>();
for(Long id : keys){
result.add(shortener.getString(id));
}
return result;
}
public static void testStrategy(StorageStrategy strategy, long elementsNumber){
printMessage(strategy.getClass().getSimpleName());
Set<String> set = new HashSet<>();
for(int i = 0; i < elementsNumber; i++) {
set.add(Helper.generateRandomString());
}
Shortener shortener = new Shortener(strategy);
Date date1 = new Date();
Set<Long> setLongs = getIds(shortener, set);
Date date2 = new Date();
long tRes = date2.getTime() - date1.getTime();
Helper.printMessage(String.valueOf(tRes));
date1 = new Date();
Set<String> setStrings = getStrings(shortener, setLongs);
date2 = new Date();
tRes = date2.getTime() - date1.getTime();
Helper.printMessage(String.valueOf(tRes));
if(set.equals(setStrings))
Helper.printMessage("Тест пройден.");
else
Helper.printMessage("Тест не пройден.");
}
public static void main(String[] args) {
StorageStrategy strategy1 = new HashMapStorageStrategy();
testStrategy(strategy1, 1000);
StorageStrategy strategy2 = new OurHashMapStorageStrategy();
testStrategy(strategy2, 1000);
StorageStrategy strategy3 = new FileStorageStrategy();
testStrategy(strategy3, 100);
StorageStrategy strategy4 = new OurHashBiMapStorageStrategy();
testStrategy(strategy4, 1000);
StorageStrategy strategy5 = new DualHashBidiMapStorageStrategy();
testStrategy(strategy5, 1000);
}
}