Class RecentlyViewedQueue
java.lang.Object
java.util.AbstractCollection<SearchedItemPanel>
java.util.AbstractQueue<SearchedItemPanel>
java.util.concurrent.ArrayBlockingQueue<SearchedItemPanel>
org.troy.capstone.data_structures.RecentlyViewedQueue
- All Implemented Interfaces:
Serializable, Iterable<SearchedItemPanel>, Collection<SearchedItemPanel>, BlockingQueue<SearchedItemPanel>, Queue<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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final intCapacity of the recently viewed queue, set to 10 for a reasonable number of items to display without overwhelming the user.private final ArrayBlockingQueue<String> Queue to keep track of item IDs for quick lookup and to prevent duplicates.private final ItemRepoRepository to retrieve item details based on item IDs, used to create SearchedItemPanel instances when adding items to the queue. -
Constructor Summary
ConstructorsConstructorDescriptionRecentlyViewedQueue(ItemRepo itemRepo) Constructor for theRecentlyViewedQueue. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidAdds an item to theRecentlyViewedQueuewithout checking for duplicates or capacity.voidaddAttempt(String itemId) Attempts to add an item to theRecentlyViewedQueue.peekAll()Returns aListcontaining all items in the queue in reverse order, without removing them.Methods inherited from class ArrayBlockingQueue
add, clear, contains, drainTo, drainTo, forEach, iterator, offer, offer, peek, poll, poll, put, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, take, toArray, toArray, toStringMethods inherited from class AbstractQueue
addAll, element, removeMethods inherited from class AbstractCollection
containsAll, isEmptyMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray
-
Field Details
-
CAPACITY
private static final int CAPACITYCapacity of the recently viewed queue, set to 10 for a reasonable number of items to display without overwhelming the user.- See Also:
-
itemRepo
Repository to retrieve item details based on item IDs, used to create SearchedItemPanel instances when adding items to the queue. -
itemIds
Queue to keep track of item IDs for quick lookup and to prevent duplicates. Faster than usingSearchedItemPaneldirectly since a lot of work is done to create a panel before checking.
-
-
Constructor Details
-
RecentlyViewedQueue
Constructor for theRecentlyViewedQueue.- Parameters:
itemRepo- TheItemRepoto use for retrieving item details.
-
-
Method Details
-
addAttempt
Attempts to add an item to theRecentlyViewedQueue.- 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
Adds an item to theRecentlyViewedQueuewithout checking for duplicates or capacity. Should only be called fromaddAttemptafter 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
Returns aListcontaining all items in the queue in reverse order, without removing them.- Returns:
- A
ListofSearchedItemPanelobjects representing the items in the queue, in order from newest to oldest.
-