Skip to content

Commit d64d7bb

Browse files
committed
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.
1 parent 2fb1a41 commit d64d7bb

3 files changed

Lines changed: 77 additions & 96 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/utils/ManifestCreatorCache.java

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.schabi.newpipe.extractor.utils;
22

3+
import javax.annotation.Nonnull;
34
import javax.annotation.Nullable;
45
import java.io.Serializable;
56
import java.util.ArrayList;
67
import java.util.Map;
7-
import java.util.Objects;
88
import java.util.concurrent.ConcurrentHashMap;
99

1010
/**
@@ -225,50 +225,11 @@ public void resetClearFactor() {
225225
this.clearFactor = DEFAULT_CLEAR_FACTOR;
226226
}
227227

228-
/**
229-
* Reveals whether an object is equal to a {@code ManifestCreator} cache existing object.
230-
*
231-
* @param obj the object to compare with the current {@code ManifestCreatorCache} object
232-
* @return whether the object compared is equal to the current {@code ManifestCreatorCache}
233-
* object
234-
*/
235-
@Override
236-
public boolean equals(final Object obj) {
237-
if (this == obj) {
238-
return true;
239-
}
240-
241-
if (obj == null || getClass() != obj.getClass()) {
242-
return false;
243-
}
244-
245-
final ManifestCreatorCache<?, ?> manifestCreatorCache =
246-
(ManifestCreatorCache<?, ?>) obj;
247-
return maximumSize == manifestCreatorCache.maximumSize
248-
&& Double.compare(manifestCreatorCache.clearFactor, clearFactor) == 0
249-
&& concurrentHashMap.equals(manifestCreatorCache.concurrentHashMap);
250-
}
251-
252-
/**
253-
* Returns a hash code of the current {@code ManifestCreatorCache}, using its
254-
* {@link #maximumSize maximum size}, {@link #clearFactor clear factor} and
255-
* {@link #concurrentHashMap internal concurrent hash map} used as a cache.
256-
*
257-
* @return a hash code of the current {@code ManifestCreatorCache}
258-
*/
259-
@Override
260-
public int hashCode() {
261-
return Objects.hash(maximumSize, clearFactor, concurrentHashMap);
262-
}
263-
264-
/**
265-
* Returns a string version of the {@link ConcurrentHashMap} used internally as the cache.
266-
*
267-
* @return the string version of the {@link ConcurrentHashMap} used internally as the cache
268-
*/
228+
@Nonnull
269229
@Override
270230
public String toString() {
271-
return concurrentHashMap.toString();
231+
return "ManifestCreatorCache[clearFactor=" + clearFactor + ", maximumSize=" + maximumSize
232+
+ ", concurrentHashMap=" + concurrentHashMap + "]";
272233
}
273234

274235
/**
@@ -285,17 +246,16 @@ private void keepNewestEntries(final int newLimit) {
285246
final int difference = concurrentHashMap.size() - newLimit;
286247
final ArrayList<Map.Entry<K, Pair<Integer, V>>> entriesToRemove = new ArrayList<>();
287248

288-
for (final Map.Entry<K, Pair<Integer, V>> entry : concurrentHashMap.entrySet()) {
249+
concurrentHashMap.entrySet().forEach(entry -> {
289250
final Pair<Integer, V> value = entry.getValue();
290251
if (value.getFirst() < difference) {
291252
entriesToRemove.add(entry);
292253
} else {
293254
value.setFirst(value.getFirst() - difference);
294255
}
295-
}
256+
});
296257

297-
for (final Map.Entry<K, Pair<Integer, V>> entry : entriesToRemove) {
298-
concurrentHashMap.remove(entry.getKey(), entry.getValue());
299-
}
258+
entriesToRemove.forEach(entry -> concurrentHashMap.remove(entry.getKey(),
259+
entry.getValue()));
300260
}
301261
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.schabi.newpipe.extractor.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class ManifestCreatorCacheTest {
8+
@Test
9+
void basicMaximumSizeAndResetTest() {
10+
final ManifestCreatorCache<String, String> cache = new ManifestCreatorCache<>();
11+
12+
// 30 elements set -> cache resized to 23 -> 5 new elements set to the cache -> 28
13+
cache.setMaximumSize(30);
14+
setCacheContent(cache);
15+
assertEquals(28, cache.size(),
16+
"Wrong cache size with default clear factor and 30 as the maximum size");
17+
cache.reset();
18+
19+
assertEquals(0, cache.size(),
20+
"The cache has been not cleared after a reset call (wrong cache size)");
21+
assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(),
22+
"Wrong maximum size after cache reset");
23+
assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(),
24+
"Wrong clear factor after cache reset");
25+
}
26+
27+
@Test
28+
void maximumSizeAndClearFactorSettersAndResettersTest() {
29+
final ManifestCreatorCache<String, String> cache = new ManifestCreatorCache<>();
30+
cache.setMaximumSize(20);
31+
cache.setClearFactor(0.5);
32+
33+
setCacheContent(cache);
34+
// 30 elements set -> cache resized to 10 -> 5 new elements set to the cache -> 15
35+
assertEquals(15, cache.size(),
36+
"Wrong cache size with 0.5 as the clear factor and 20 as the maximum size");
37+
38+
// Clear factor and maximum size getters tests
39+
assertEquals(0.5, cache.getClearFactor(),
40+
"Wrong clear factor gotten from clear factor getter");
41+
assertEquals(20, cache.getMaximumSize(),
42+
"Wrong maximum cache size gotten from maximum size getter");
43+
44+
// Resetters tests
45+
cache.resetMaximumSize();
46+
assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(),
47+
"Wrong maximum cache size gotten from maximum size getter after maximum size "
48+
+ "resetter call");
49+
50+
cache.resetClearFactor();
51+
assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(),
52+
"Wrong clear factor gotten from clear factor getter after clear factor resetter "
53+
+ "call");
54+
}
55+
56+
private static void setCacheContent(final ManifestCreatorCache<String, String> cache) {
57+
int i = 0;
58+
while (i < 26) {
59+
cache.put(String.valueOf((char) ('a' + i)), "V");
60+
++i;
61+
}
62+
63+
i = 0;
64+
while (i < 9) {
65+
cache.put("a" + (char) ('a' + i), "V");
66+
++i;
67+
}
68+
}
69+
}

extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,4 @@ void testFollowGoogleRedirect() {
4747
assertEquals("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello",
4848
Utils.followGoogleRedirectIfNeeded("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello"));
4949
}
50-
51-
@Test
52-
void dashManifestCreatorCacheTest() {
53-
final ManifestCreatorCache<String, String> cache = new ManifestCreatorCache<>();
54-
cache.setMaximumSize(30);
55-
setCacheContent(cache);
56-
// 30 elements set -> cache resized to 23 -> 5 new elements set to the cache -> 28
57-
assertEquals(28, cache.size(),
58-
"Wrong cache size with default clear factor and 30 as the maximum size");
59-
60-
cache.reset();
61-
cache.setMaximumSize(20);
62-
cache.setClearFactor(0.5);
63-
64-
setCacheContent(cache);
65-
// 30 elements set -> cache resized to 10 -> 5 new elements set to the cache -> 15
66-
assertEquals(15, cache.size(),
67-
"Wrong cache size with 0.5 as the clear factor and 20 as the maximum size");
68-
69-
// Clear factor and maximum size getters tests
70-
assertEquals(0.5, cache.getClearFactor(),
71-
"Wrong clear factor gotten from clear factor getter");
72-
assertEquals(20, cache.getMaximumSize(),
73-
"Wrong maximum cache size gotten from maximum size getter");
74-
75-
// Resetters tests
76-
cache.resetMaximumSize();
77-
assertEquals(ManifestCreatorCache.DEFAULT_MAXIMUM_SIZE, cache.getMaximumSize(),
78-
"Wrong maximum cache size gotten from maximum size getter after maximum size reset");
79-
80-
cache.resetClearFactor();
81-
assertEquals(ManifestCreatorCache.DEFAULT_CLEAR_FACTOR, cache.getClearFactor(),
82-
"Wrong clear factor gotten from clear factor getter after clear factor reset");
83-
}
84-
85-
private void setCacheContent(@Nonnull final ManifestCreatorCache<String, String> cache) {
86-
int i = 0;
87-
while (i < 26) {
88-
cache.put(Character.toString((char) (97 + i)), "V");
89-
++i;
90-
}
91-
92-
i = 0;
93-
while (i < 9) {
94-
cache.put("a" + (char) (97 + i), "V");
95-
++i;
96-
}
97-
}
9850
}

0 commit comments

Comments
 (0)