Class Bucket<E>

  • Type Parameters:
    E - The content type
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>

    public class Bucket<E>
    extends java.util.AbstractCollection<E>
    A LRU-like organized collection, i.e. calls to the add() method always insert elements at the front of the collection. If an already exisiting element is added to the bucket it is moved to the front of the bucket. If a maximum size is set for the bucket it does not grow beyond this limit by dropping elements at the end of the bucket.
    • Constructor Summary

      Constructors 
      Constructor Description
      Bucket()
      Creates a Bucket object with unlimited maximum size.
      Bucket​(int maxSize)
      Creates a Bucket object with the specified maximum size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E o)
      Add an object to the front of the bucket.
      void clear()
      Empty bucket.
      boolean contains​(java.lang.Object o)
      Check if an object is inside the bucket.
      boolean containsAll​(java.util.Collection<?> c)
      Check if a list of objects is inside the bucket.
      boolean equals​(java.lang.Object o)
      Check if a given object is the same as the actual bucket.
      E get​(int index)
      Get a specific item from the bucket.
      int getMaxSize()
      Returns the maximum size of this bucket that it is not allowed to outgrow.
      int hashCode()
      This implementation is pretty naive...
      boolean isEmpty()
      Check if bucket is empty.
      java.util.Iterator<E> iterator()
      Get an interator for the bucket content.
      boolean remove​(java.lang.Object o)
      Remove a given object from the bucket.
      boolean removeAll​(java.util.Collection<?> c)
      Remove a list of items from the bucket.
      boolean retainAll​(java.util.Collection<?> c)
      DOCUMENT ME!
      void setMaxSize​(int maxSize)
      Sets the specified maximum size for this bucket.
      int size()
      Get the number of items inside the bucket.
      java.lang.Object[] toArray()
      Get array of all items inside the bucket.
      <T> T[] toArray​(T[] a)  
      java.lang.String toString()
      Create a string representation of the bucket.
      • Methods inherited from class java.util.AbstractCollection

        addAll
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Constructor Detail

      • Bucket

        public Bucket()
        Creates a Bucket object with unlimited maximum size. I.e. this bucket will never drop elements by itself.
      • Bucket

        public Bucket​(int maxSize)
        Creates a Bucket object with the specified maximum size.
        Parameters:
        maxSize - maximum size of the bucket.
    • Method Detail

      • add

        public boolean add​(E o)
        Add an object to the front of the bucket. If the number of elements in the bucket exceeds the bucket's maximum size an element at the end of the bucket is dropped. If object o is already contained in the bucket, it is moved to the front of the bucket.
        Specified by:
        add in interface java.util.Collection<E>
        Overrides:
        add in class java.util.AbstractCollection<E>
        Parameters:
        o - object to add to the bucket.
      • clear

        public void clear()
        Empty bucket.
        Specified by:
        clear in interface java.util.Collection<E>
        Overrides:
        clear in class java.util.AbstractCollection<E>
      • contains

        public boolean contains​(java.lang.Object o)
        Check if an object is inside the bucket.
        Specified by:
        contains in interface java.util.Collection<E>
        Overrides:
        contains in class java.util.AbstractCollection<E>
        Parameters:
        o - object to look for.
        Returns:
        true if found, false if not.
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Check if a list of objects is inside the bucket.
        Specified by:
        containsAll in interface java.util.Collection<E>
        Overrides:
        containsAll in class java.util.AbstractCollection<E>
        Parameters:
        c - collection of objects to look for.
        Returns:
        true if all were found, false if not.
      • equals

        public boolean equals​(java.lang.Object o)
        Check if a given object is the same as the actual bucket. This is a semi-deep version
        Specified by:
        equals in interface java.util.Collection<E>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - object to be compared with.
        Returns:
        true if it is the same, false if not.
      • hashCode

        public int hashCode()
        This implementation is pretty naive...
        Specified by:
        hashCode in interface java.util.Collection<E>
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • isEmpty

        public boolean isEmpty()
        Check if bucket is empty.
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Overrides:
        isEmpty in class java.util.AbstractCollection<E>
        Returns:
        true if empty, false if not.
      • iterator

        public java.util.Iterator<E> iterator()
        Get an interator for the bucket content.
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in class java.util.AbstractCollection<E>
        Returns:
        an iterator for the objects inside the bucket.
      • remove

        public boolean remove​(java.lang.Object o)
        Remove a given object from the bucket.
        Specified by:
        remove in interface java.util.Collection<E>
        Overrides:
        remove in class java.util.AbstractCollection<E>
        Parameters:
        o - object to remove from the bucket.
        Returns:
        true if removal was successful, false if not.
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Remove a list of items from the bucket.
        Specified by:
        removeAll in interface java.util.Collection<E>
        Overrides:
        removeAll in class java.util.AbstractCollection<E>
        Parameters:
        c - collection of all the items to remove.
        Returns:
        true if removal was successful, false if not.
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        DOCUMENT ME!
        Specified by:
        retainAll in interface java.util.Collection<E>
        Overrides:
        retainAll in class java.util.AbstractCollection<E>
        Parameters:
        c - collection of the items to retain.
        Returns:
        true if retaining was successfull, false if not.
      • size

        public int size()
        Get the number of items inside the bucket.
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
        Returns:
        the number of objects inside the bucket.
      • toArray

        public java.lang.Object[] toArray()
        Get array of all items inside the bucket.
        Specified by:
        toArray in interface java.util.Collection<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
        Returns:
        array of items inside the bucket.
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
        See Also:
        AbstractCollection.toArray(Object[])
      • getMaxSize

        public int getMaxSize()
        Returns the maximum size of this bucket that it is not allowed to outgrow.
        Returns:
        number of items the bucket may contain.
      • setMaxSize

        public void setMaxSize​(int maxSize)
        Sets the specified maximum size for this bucket. If there are more elements in the bucket than maxSize, elements at the end of the bucket are dropped until the size of the bucket equals maxSize.
        Parameters:
        maxSize - number of items the bucket may contain.
      • toString

        public java.lang.String toString()
        Create a string representation of the bucket.
        Overrides:
        toString in class java.util.AbstractCollection<E>
        Returns:
        string representation of the bucket.
      • get

        public E get​(int index)
        Get a specific item from the bucket.
        Parameters:
        index - number of a specific item in the bucket.
        Returns:
        the element at the specified index.