Class LastRecentlyUsed<T>

  • All Implemented Interfaces:
    Iterable<T>, Collection<T>

    public class LastRecentlyUsed<T>
    extends Object
    implements Collection<T>
    A simple container for N last-recently-used items.
    Author:
    Johannes Schindelin
    • Constructor Detail

      • LastRecentlyUsed

        public LastRecentlyUsed​(int size)
    • Method Detail

      • next

        public int next​(int index)
        Given the index of an entry, returns the index of the next newer entry.
        Parameters:
        index - the index of the current entry, or -1 to wrap around to the oldest entry.
        Returns:
        the index of the next newer entry, or -1 when there is no such entry.
      • previous

        public int previous​(int index)
        Given the index of an entry, returns the index of the next older entry.
        Parameters:
        index - the index of the current entry, or -1 to wrap around to the newest entry.
        Returns:
        the index of the next older entry, or -1 when there is no such entry.
      • get

        public T get​(int index)
        Returns the entry for the given index.
        Parameters:
        index - the index of the entry
        Returns:
        the entry
      • lookup

        public int lookup​(T value)
        Looks up the index for a given entry.
        Parameters:
        value - the value of the entry to find
        Returns:
        the corresponding index, or -1 if the entry was not found
      • add

        public boolean add​(T value)
        Add a new newest entry.
        Specified by:
        add in interface Collection<T>
        Parameters:
        value - the value of the entry
        Returns:
        whether the entry was added
      • addToEnd

        public void addToEnd​(T value)
        Add a new oldest entry.

        This method helps recreating LastRecentlyUsed instances given the entries in the order newest first, oldest last.

        Parameters:
        value - the value of the entry to add
      • replace

        public boolean replace​(int index,
                               T newValue)
      • clear

        public void clear()
        Empties the data structure.
        Specified by:
        clear in interface Collection<T>
      • toArray

        public <S> S[] toArray​(S[] array)
        Specified by:
        toArray in interface Collection<T>
      • assertConsistency

        protected void assertConsistency()