Move ManifestCreatorCache tests to a separate class and remove override of equals and hashCode methods in ManifestCreatorCache

These methods don't need to be overriden, as they are not excepted to be used in collections.
Also improve the toString method of this class, which contains also now clearFactor and maximumSize attributes and for each operations.
This commit is contained in:
TiA4f8R 2022-04-03 20:54:35 +02:00
parent 2fb1a412a6
commit d64d7bbd01
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
3 changed files with 77 additions and 96 deletions

View File

@ -1,10 +1,10 @@
package org.schabi.newpipe.extractor.utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -225,50 +225,11 @@ public final class ManifestCreatorCache<K extends Serializable, V extends Serial
this.clearFactor = DEFAULT_CLEAR_FACTOR;
}
/**
* Reveals whether an object is equal to a {@code ManifestCreator} cache existing object.
*
* @param obj the object to compare with the current {@code ManifestCreatorCache} object
* @return whether the object compared is equal to the current {@code ManifestCreatorCache}
* object
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final ManifestCreatorCache<?, ?> manifestCreatorCache =
(ManifestCreatorCache<?, ?>) obj;
return maximumSize == manifestCreatorCache.maximumSize
&& Double.compare(manifestCreatorCache.clearFactor, clearFactor) == 0
&& concurrentHashMap.equals(manifestCreatorCache.concurrentHashMap);
}
/**
* Returns a hash code of the current {@code ManifestCreatorCache}, using its
* {@link #maximumSize maximum size}, {@link #clearFactor clear factor} and
* {@link #concurrentHashMap internal concurrent hash map} used as a cache.
*
* @return a hash code of the current {@code ManifestCreatorCache}
*/
@Override
public int hashCode() {
return Objects.hash(maximumSize, clearFactor, concurrentHashMap);
}
/**
* Returns a string version of the {@link ConcurrentHashMap} used internally as the cache.
*
* @return the string version of the {@link ConcurrentHashMap} used internally as the cache
*/
@Nonnull
@Override
public String toString() {
return concurrentHashMap.toString();
return "ManifestCreatorCache[clearFactor=" + clearFactor + ", maximumSize=" + maximumSize
+ ", concurrentHashMap=" + concurrentHashMap + "]";
}
/**
@ -285,17 +246,16 @@ public final class ManifestCreatorCache<K extends Serializable, V extends Serial
final int difference = concurrentHashMap.size() - newLimit;
final ArrayList<Map.Entry<K, Pair<Integer, V>>> entriesToRemove = new ArrayList<>();
for (final Map.Entry<K, Pair<Integer, V>> entry : concurrentHashMap.entrySet()) {
concurrentHashMap.entrySet().forEach(entry -> {
final Pair<Integer, V> value = entry.getValue();
if (value.getFirst() < difference) {
entriesToRemove.add(entry);
} else {
value.setFirst(value.getFirst() - difference);
}
}
});
for (final Map.Entry<K, Pair<Integer, V>> entry : entriesToRemove) {
concurrentHashMap.remove(entry.getKey(), entry.getValue());
}
entriesToRemove.forEach(entry -> concurrentHashMap.remove(entry.getKey(),
entry.getValue()));
}
}

View File

@ -0,0 +1,69 @@
package org.schabi.newpipe.extractor.utils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ManifestCreatorCacheTest {
@Test
void basicMaximumSizeAndResetTest() {
final ManifestCreatorCache<String, String> cache = new ManifestCreatorCache<>();
// 30 elements set -> cache resized to 23 -> 5 new elements set to the cache -> 28
cache.setMaximumSize(30);
setCacheContent(cache);
assertEquals(28, cache.size(),
"Wrong cache size with default clear factor and 30 as the maximum size");
cache.reset();
assertEquals(0, cache.size(),
"The cache has been not cleared after a reset call (wrong cache size)");
assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(),
"Wrong maximum size after cache reset");
assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(),
"Wrong clear factor after cache reset");
}
@Test
void maximumSizeAndClearFactorSettersAndResettersTest() {
final ManifestCreatorCache<String, String> cache = new ManifestCreatorCache<>();
cache.setMaximumSize(20);
cache.setClearFactor(0.5);
setCacheContent(cache);
// 30 elements set -> cache resized to 10 -> 5 new elements set to the cache -> 15
assertEquals(15, cache.size(),
"Wrong cache size with 0.5 as the clear factor and 20 as the maximum size");
// Clear factor and maximum size getters tests
assertEquals(0.5, cache.getClearFactor(),
"Wrong clear factor gotten from clear factor getter");
assertEquals(20, cache.getMaximumSize(),
"Wrong maximum cache size gotten from maximum size getter");
// Resetters tests
cache.resetMaximumSize();
assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(),
"Wrong maximum cache size gotten from maximum size getter after maximum size "
+ "resetter call");
cache.resetClearFactor();
assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(),
"Wrong clear factor gotten from clear factor getter after clear factor resetter "
+ "call");
}
private static void setCacheContent(final ManifestCreatorCache<String, String> cache) {
int i = 0;
while (i < 26) {
cache.put(String.valueOf((char) ('a' + i)), "V");
++i;
}
i = 0;
while (i < 9) {
cache.put("a" + (char) ('a' + i), "V");
++i;
}
}
}

View File

@ -47,52 +47,4 @@ class UtilsTest {
assertEquals("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello",
Utils.followGoogleRedirectIfNeeded("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello"));
}
@Test
void dashManifestCreatorCacheTest() {
final ManifestCreatorCache<String, String> cache = new ManifestCreatorCache<>();
cache.setMaximumSize(30);
setCacheContent(cache);
// 30 elements set -> cache resized to 23 -> 5 new elements set to the cache -> 28
assertEquals(28, cache.size(),
"Wrong cache size with default clear factor and 30 as the maximum size");
cache.reset();
cache.setMaximumSize(20);
cache.setClearFactor(0.5);
setCacheContent(cache);
// 30 elements set -> cache resized to 10 -> 5 new elements set to the cache -> 15
assertEquals(15, cache.size(),
"Wrong cache size with 0.5 as the clear factor and 20 as the maximum size");
// Clear factor and maximum size getters tests
assertEquals(0.5, cache.getClearFactor(),
"Wrong clear factor gotten from clear factor getter");
assertEquals(20, cache.getMaximumSize(),
"Wrong maximum cache size gotten from maximum size getter");
// Resetters tests
cache.resetMaximumSize();
assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(),
"Wrong maximum cache size gotten from maximum size getter after maximum size reset");
cache.resetClearFactor();
assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(),
"Wrong clear factor gotten from clear factor getter after clear factor reset");
}
private void setCacheContent(@Nonnull final ManifestCreatorCache<String, String> cache) {
int i = 0;
while (i < 26) {
cache.put(Character.toString((char) (97 + i)), "V");
++i;
}
i = 0;
while (i < 9) {
cache.put("a" + (char) (97 + i), "V");
++i;
}
}
}