Class RecentlyViewedQueue

All Implemented Interfaces:
Serializable, Iterable<SearchedItemPanel>, Collection<SearchedItemPanel>, BlockingQueue<SearchedItemPanel>, Queue<SearchedItemPanel>

public class RecentlyViewedQueue extends ArrayBlockingQueue<SearchedItemPanel>
RecentlyViewedQueue is a queue to manage recently viewed items, with a fixed capacity. Oldest item is removed when capacity is exceeded. Does not allow duplicates.
See Also:
  • Field Details

    • CAPACITY

      private static final int CAPACITY
      Capacity of the recently viewed queue, set to 10 for a reasonable number of items to display without overwhelming the user.
      See Also:
    • itemRepo

      private final ItemRepo itemRepo
      Repository to retrieve item details based on item IDs, used to create SearchedItemPanel instances when adding items to the queue.
    • itemIds

      private final ArrayBlockingQueue<String> itemIds
      Queue to keep track of item IDs for quick lookup and to prevent duplicates. Faster than using SearchedItemPanel directly since a lot of work is done to create a panel before checking.
  • Constructor Details

    • RecentlyViewedQueue

      public RecentlyViewedQueue(ItemRepo itemRepo)
      Constructor for the RecentlyViewedQueue.
      Parameters:
      itemRepo - The ItemRepo to use for retrieving item details.
  • Method Details

    • addAttempt

      public void addAttempt(String itemId)
      Attempts to add an item to the RecentlyViewedQueue.
      Parameters:
      itemId - The ID of the item to add.
      Preconditions:
      itemId is not null and corresponds to a valid key in the ItemHashMap.
      Postconditions:
      If the item is not already in the queue, it is added and the oldest item is removed. If the item is already in the queue, it is moved to the top (most recent position) without duplication. If the queue is full, the oldest item is removed to make space for the new item.
    • add

      private void add(String itemId)
      Adds an item to the RecentlyViewedQueue without checking for duplicates or capacity. Should only be called from addAttempt after those checks have been made.
      Parameters:
      itemId - The ID of the item to add.
      Preconditions:
      itemId is not null and is the key of an item allowed in the queue based on checks in addAttempt.
      Postconditions:
      The item with the given ID is added to the queue as a SearchedItemPanel.
    • peekAll

      public List<SearchedItemPanel> peekAll()
      Returns a List containing all items in the queue in reverse order, without removing them.
      Returns:
      A List of SearchedItemPanel objects representing the items in the queue, in order from newest to oldest.