LimeWire collection component api

org.limewire.collection
Class BucketQueue<E>

java.lang.Object
  extended by org.limewire.collection.BucketQueue<E>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<E>

public class BucketQueue<E>
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Iterable<E>

Provides a discrete-case priority queue. BucketQueue is designed to be a replacement for a binary heap for the special case when there are only a small number of positive priorities. (Priorities are zero based and therefore, when priority is set to 3, the values are [0, 1, 2]. Also, larger numbers are higher priority.)

You determine how many element cases per priority are allowed in the queue. When the queue attempts to add an element more than the maximum capacity for the priority, the first element is removed upon insert(Object, int).

This class is not thread-safe.

                                        //priorities, capacity
    BucketQueue<String> bq = new BucketQueue<String>(3,2);

    bq.insert("Abby", 1);
    bq.insert("Bob", 1);
    System.out.println(bq);

    String returnFromInsert ;
    returnFromInsert = bq.insert("Chris", 1);
    if(returnFromInsert != null)
        System.out.println("Element " + returnFromInsert + " popped because there are already 2 elements of priority 1.");

    System.out.println(bq);
    bq.insert("Dan", 2);
    bq.insert("Eric", 2);
    bq.insert("Fred", 0);

    System.out.println("Max: " + bq.getMax().toString() + " and bq is " + bq);      

    Output:    
        [[], [Bob, Abby], []]
        Element Abby popped because there are already 2 elements of priority 1.
        [[], [Chris, Bob], []]
        Max: Eric and bq is [[Eric, Dan], [Chris, Bob], [Fred]]

 


Constructor Summary
BucketQueue(BucketQueue<? extends E> other)
          "Copy constructor": constructs a a new shallow copy of other.
BucketQueue(int[] capacities)
           
BucketQueue(int priorities, int capacityPerPriority)
           
 
Method Summary
 void clear()
          Removes all elements from the queue.
 BucketQueue<E> clone()
          Returns a shallow copy of this, of type BucketQueue.
 E extractMax()
           
 E getMax()
           
 E insert(E o, int priority)
           
 boolean isEmpty()
           
 java.util.Iterator<E> iterator()
           
 java.util.Iterator<E> iterator(int startPriority, int n)
           
 boolean removeAll(java.lang.Object o)
           
 int size()
           
 int size(int priority)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BucketQueue

public BucketQueue(int priorities,
                   int capacityPerPriority)
            throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException - priorities or capacityPerPriority is non-positive.

BucketQueue

public BucketQueue(int[] capacities)
            throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException - capacities.length<=0 or capacities[i]<=0 for any i

BucketQueue

public BucketQueue(BucketQueue<? extends E> other)
"Copy constructor": constructs a a new shallow copy of other.

Method Detail

clear

public void clear()
Removes all elements from the queue.


insert

public E insert(E o,
                int priority)
Throws:
java.lang.IllegalArgumentException - priority is not a legal priority, as determined by this' constructor

removeAll

public boolean removeAll(java.lang.Object o)

extractMax

public E extractMax()
             throws java.util.NoSuchElementException
Throws:
java.util.NoSuchElementException

getMax

public E getMax()
         throws java.util.NoSuchElementException
Throws:
java.util.NoSuchElementException

size

public int size()

size

public int size(int priority)
         throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException - priority is not a legal priority, as determined by this' constructor

isEmpty

public boolean isEmpty()

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>

iterator

public java.util.Iterator<E> iterator(int startPriority,
                                      int n)
                               throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException - startPriority is not a legal priority as determined by this' constructor

clone

public BucketQueue<E> clone()
                     throws java.lang.CloneNotSupportedException
Returns a shallow copy of this, of type BucketQueue.

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

LimeWire collection component api

Copyright © 2009 Lime Wire LLC. All Rights Reserved.