KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > chooser > RoundRobinChooser


1 package com.ubermq.kernel.chooser;
2
3 import com.ubermq.kernel.*;
4 import java.util.*;
5
6 /**
7  * Chooses an object sequentially from the
8  * given Collection. This implementation only
9  * works on a single Collection at a time. If
10  * the <code>choose</code> method is called with
11  * differing collections, an <code>IllegalStateException</code>
12  * will be thrown.
13  */

14 public class RoundRobinChooser
15     extends AbstractOrderedChooser
16     implements Chooser
17 {
18     private int n;
19
20     public void reset()
21     {
22         super.reset();
23         n = 0;
24     }
25
26     /**
27      * Chooses the next object in
28      * the collection, wrapping to the beginning
29      * of the collection if necessary. The
30      * collection must be an instance of <code>List</code>,
31      * because the sequential nature of this algorithm
32      * requires an ordered collection.<P>
33      *
34      * The initial call may return any of the
35      * items in the Collection, not just the first.<P>
36      *
37      * @param objects a List of objects
38      * @return an Object r, such that <code>objects.contains(r)</code> is true.
39      * @throws IllegalStateException if the chooser can
40      * only operate on a single Collection object, and
41      * the Collection does not match that of the previous
42      * caller.
43      */

44     public Object JavaDoc choose()
45     {
46         return myList.get(n++ % size);
47     }
48
49 }
50
Popular Tags