KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > queue > QueueReader


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.util.queue;
21 import java.util.*;
22
23 /** A queue of Object's. One can add objects to the queue, and they are
24  * later read by a QueueReader. One can create arbitrary numbers of
25  * QueueReader's for a queue, and each one receives all the Object's that
26  * are added. Only objects that have not been read by all the QueueReader's
27  * are kept. A QueueReader only receives the Object's added to the queue
28  * <b>after</b> the QueueReader was created.
29  * @author Ondrej Lhotak
30  */

31 public class QueueReader implements java.util.Iterator JavaDoc
32 {
33     private Object JavaDoc[] q;
34     private int index;
35     QueueReader( Object JavaDoc[] q, int index ) {
36         this.q = q;
37         this.index = index;
38     }
39     /** Returns (and removes) the next object in the queue, or null if
40      * there are none. */

41     public final Object JavaDoc next() {
42         if( q[index] == null ) throw new NoSuchElementException();
43         if( index == q.length - 1 ) {
44             q = (Object JavaDoc[]) q[index];
45             index = 0;
46             if( q[index] == null ) throw new NoSuchElementException();
47         }
48         Object JavaDoc ret = q[index];
49         if( ret == ChunkedQueue.NULL_CONST ) ret = null;
50         index++;
51         return ret;
52     }
53
54     /** Returns true iff there is currently another object in the queue. */
55     public final boolean hasNext() {
56         if (q[index] == null) return false;
57         if (index == q.length - 1) {
58             q = (Object JavaDoc[]) q[index];
59             index = 0;
60             if (q[index] == null) return false;
61         }
62         return true;
63     }
64
65     public final void remove() {
66         throw new UnsupportedOperationException JavaDoc();
67     }
68
69     public final Object JavaDoc clone() {
70         return new QueueReader( q, index );
71     }
72 }
73
74
75
Popular Tags