Class ManifestCreatorCache<K extends Serializable,V extends Serializable>

java.lang.Object
org.schabi.newpipe.extractor.utils.ManifestCreatorCache<K,V>
Type Parameters:
K - the type of cache keys, which must be serializable
V - the type of the second element of pairs used as values of the cache, which must be serializable
All Implemented Interfaces:
Serializable

public final class ManifestCreatorCache<K extends Serializable,V extends Serializable> extends Object implements Serializable
A serializable 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:
  • Field Details

    • DEFAULT_MAXIMUM_SIZE

      public static final int DEFAULT_MAXIMUM_SIZE
      The default maximum size of a manifest cache.
      See Also:
    • DEFAULT_CLEAR_FACTOR

      public static final double DEFAULT_CLEAR_FACTOR
      The default clear factor of a manifest cache.
      See Also:
  • Constructor Details

  • Method Details

    • 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<Integer,V> get(K key)
      Returns the value to which the specified key is mapped, or null 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 put
      value - 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 associated null 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:
    • 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:
      IllegalArgumentException - if maximumSize is less than or equal to 0
    • resetMaximumSize

      public void resetMaximumSize()
      Resets the maximum size of the cache to its default 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 and 1 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:
      IllegalArgumentException - if the clear factor passed a parameter is invalid
    • resetClearFactor

      public void resetClearFactor()
      Resets the clear factor to its default value.
    • toString

      @Nonnull public String toString()
      Overrides:
      toString in class Object