After tons of attempts and on the edge of desperation I decided to use the answer provided by CodeGym.
First of all, my answer is literally the same as theirs
But this is not everything, it doesn't even pass the provided answer.
How is that even possible ?
Please let me know what you think.
package com.codegym.task.task33.task3310;
import com.codegym.task.task33.task3310.strategy.HashMapStorageStrategy;
import com.codegym.task.task33.task3310.strategy.StorageStrategy;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Solution {
public static void main(String[] args) {
long num = 10000;
testStrategy(new HashMapStorageStrategy(), num);
}
public static Set<Long> getIds(Shortener shortener, Set<String> strings) {
Set<Long> keys = new HashSet<>();
for (String s : strings) {
keys.add(shortener.getId(s));
}
return keys;
}
public static Set<String> getStrings(Shortener shortener, Set<Long> keys){
Set<String> result = new HashSet<>();
for (Long key: keys) {
result.add(shortener.getString(key));
}
return result;
}
public static void testStrategy(StorageStrategy strategy, long elementsNumber){
Helper.printMessage(strategy.getClass().getSimpleName());
Set<String> testSet = new HashSet<>();
for (int i = 0; i <elementsNumber ; i++) {
testSet.add(Helper.generateRandomString());
}
Shortener shortener = new Shortener(strategy);
Date start = new Date();
Set<Long> testIds = getIds(shortener, testSet);
Date end = new Date();
long time = end.getTime() - start.getTime();
Helper.printMessage("Time IDs : " + time);
start = new Date();
Set<String> strings2 = getStrings(shortener, testIds);
end = new Date();
time = end.getTime() - start.getTime();
Helper.printMessage("Time strings : " + time);
if(testSet.equals(strings2))
Helper.printMessage("The test passed.");
else
Helper.printMessage("The test failed.");
Helper.printMessage("");
}
}