Class ManifestCreatorCache<K extends java.io.Serializable,V extends java.io.Serializable>
- java.lang.Object
-
- org.schabi.newpipe.extractor.utils.ManifestCreatorCache<K,V>
-
- Type Parameters:
K
- the type of cache keys, which must beserializable
V
- the type of the second element ofpairs
used as values of the cache, which must beserializable
- All Implemented Interfaces:
java.io.Serializable
public final class ManifestCreatorCache<K extends java.io.Serializable,V extends java.io.Serializable> extends java.lang.Object implements java.io.Serializable
Aserializable
cache class used by the extractor to cache manifests generated with extractor's manifests generators.It relies internally on a
ConcurrentHashMap
to allow concurrent access to the cache.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static double
DEFAULT_CLEAR_FACTOR
The default clear factor of a manifest cache.static int
DEFAULT_MAXIMUM_SIZE
The default maximum size of a manifest cache.
-
Constructor Summary
Constructors Constructor Description ManifestCreatorCache()
Creates a newManifestCreatorCache
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the cached manifests.boolean
containsKey(K key)
Tests if the specified key is in the cache.Pair<java.lang.Integer,V>
get(K key)
Returns the value to which the specified key is mapped, ornull
if the cache contains no mapping for the key.double
getClearFactor()
long
getMaximumSize()
V
put(K key, V value)
Adds a new element to the cache.void
reset()
Resets the cache.void
resetClearFactor()
Resets the clear factor to itsdefault value
.void
resetMaximumSize()
Resets the maximum size of the cache to itsdefault value
.void
setClearFactor(double clearFactor)
Sets the clear factor of the cache, used when the cache limit size is reached.void
setMaximumSize(int maximumSize)
Sets the maximum size of the cache.int
size()
java.lang.String
toString()
-
-
-
Field Detail
-
DEFAULT_MAXIMUM_SIZE
public static final int DEFAULT_MAXIMUM_SIZE
The default maximum size of a manifest cache.- See Also:
- Constant Field Values
-
DEFAULT_CLEAR_FACTOR
public static final double DEFAULT_CLEAR_FACTOR
The default clear factor of a manifest cache.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ManifestCreatorCache
public ManifestCreatorCache()
Creates a newManifestCreatorCache
.
-
-
Method Detail
-
containsKey
public boolean containsKey(K key)
Tests if the specified key is in the cache.- Parameters:
key
- the key to test its presence in the cache- Returns:
true
if the key is in the cache,false
otherwise.
-
get
@Nullable public Pair<java.lang.Integer,V> get(K key)
Returns the value to which the specified key is mapped, ornull
if the cache contains no mapping for the key.- Parameters:
key
- the key to which getting its value- Returns:
- the value to which the specified key is mapped, or
null
-
put
@Nullable public V put(K key, V value)
Adds a new element to the cache.If the cache limit is reached, oldest elements will be cleared first using the load factor and the maximum size.
- Parameters:
key
- the key to putvalue
- the value to associate to the key- Returns:
- the previous value associated with the key, or
null
if there was no mapping for the key (note that a null return can also indicate that the cache previously associatednull
with the key).
-
clear
public void clear()
Clears the cached manifests.The cache will be empty after this method is called.
-
reset
public void reset()
Resets the cache.The cache will be empty and the clear factor and the maximum size will be reset to their default values.
- See Also:
clear()
,resetClearFactor()
,resetMaximumSize()
-
size
public int size()
- Returns:
- the number of cached manifests in the cache
-
getMaximumSize
public long getMaximumSize()
- Returns:
- the maximum size of the cache
-
setMaximumSize
public void setMaximumSize(int maximumSize)
Sets the maximum size of the cache. If the current cache size is more than the new maximum size, the percentage of one less the clear factor of the maximum new size of manifests in the cache will be removed.- Parameters:
maximumSize
- the new maximum size of the cache- Throws:
java.lang.IllegalArgumentException
- ifmaximumSize
is less than or equal to 0
-
resetMaximumSize
public void resetMaximumSize()
Resets the maximum size of the cache to itsdefault value
.
-
getClearFactor
public double getClearFactor()
- Returns:
- the current clear factor of the cache, used when the cache limit size is reached
-
setClearFactor
public void setClearFactor(double clearFactor)
Sets the clear factor of the cache, used when the cache limit size is reached.The clear factor must be a double between
0
excluded and1
excluded.Note that it will be only used the next time the cache size limit is reached.
- Parameters:
clearFactor
- the new clear factor of the cache- Throws:
java.lang.IllegalArgumentException
- if the clear factor passed a parameter is invalid
-
resetClearFactor
public void resetClearFactor()
Resets the clear factor to itsdefault value
.
-
toString
@Nonnull public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-