Class CacheManager

java.lang.Object
model.logic.server.dictionary.CacheManager

public class CacheManager extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
    The CacheManager function is responsible for managing the cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(String word)
    The add function adds a word to the cache.
    boolean
    The equals function is used to compare two objects.
    int
    The hashCode function is used to generate a unique hash code for each instance of the class.
    boolean
    query(String word)
    The query function takes in a string and returns true if the word is in the dictionary, false otherwise.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CacheManager

      public CacheManager(int size, CacheReplacementPolicy crp)
      The CacheManager function is responsible for managing the cache. It will add a new entry to the cache if it does not exist, or update an existing one. If there is no space in the cache, it will remove an entry according to its replacement policy and then add/update as needed.
      Parameters:
      size - size Set the size of the cache
      crp - crp Determine which cache replacement policy to use
  • Method Details

    • equals

      public boolean equals(Object o)
      The equals function is used to compare two objects.
      Overrides:
      equals in class Object
      Parameters:
      o - o Compare the current object with another object
      Returns:
      True if the words cache are equal
    • hashCode

      public int hashCode()
      The hashCode function is used to generate a unique hash code for each instance of the class. This is useful when storing instances in HashMaps or HashSets, as it allows us to quickly determine if two objects are equal by comparing their hash codes. If two objects have the same hash code, they are not guaranteed to be equal (as there may be collisions), but if they do not have the same hash code, then we know that they cannot possibly be equal. Thus, we can avoid having to compare them at all! The default implementation of this function in Java uses each field in an object's
      Overrides:
      hashCode in class Object
      Returns:
      The hashcode of the words cache object
    • query

      public boolean query(String word)
      The query function takes in a string and returns true if the word is in the dictionary, false otherwise.
      Parameters:
      word - word Check if the word is in the cache
      Returns:
      A boolean value indicating if the word is in the trie
    • add

      public void add(String word)
      The add function adds a word to the cache. If the cache is full, it removes the least recently used word from both wordsCache and crp. Then, it adds the new word to both wordsCache and crp.
      Parameters:
      word - word Add a word to the cache