KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > Queue4


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation;
22
23
24 /**
25  * Using the CollectionElement the other way around:
26  * CollectionElement.i_next points to the previous element
27  *
28  * @exclude
29  */

30 public class Queue4 {
31     private final class Queue4Iterator implements Iterator4 {
32         private boolean _active=false;
33         private List4 _current=null;
34         
35         public Object JavaDoc current() {
36             return _current._element;
37         }
38
39         public boolean moveNext() {
40             if(!_active) {
41                 _current=_last;
42                 _active=true;
43             }
44             else {
45                 if(_current!=null) {
46                     _current=_current._next;
47                 }
48             }
49             return _current!=null;
50         }
51
52         public void reset() {
53             _current=null;
54             _active=false;
55         }
56     }
57
58     private List4 _first;
59     private List4 _last;
60     
61     public final void add(Object JavaDoc obj) {
62         List4 ce = new List4(null, obj);
63         if(_first == null){
64             _last = ce;
65         }else{
66             _first._next = ce;
67         }
68         _first = ce;
69     }
70     
71     public final Object JavaDoc next() {
72         if(_last == null){
73             return null;
74         }
75         Object JavaDoc ret = _last._element;
76         _last = _last._next;
77         if(_last == null){
78             _first = null;
79         }
80         return ret;
81     }
82     
83     public final boolean hasNext(){
84         return _last != null;
85     }
86
87     public Iterator4 iterator() {
88         return new Queue4Iterator();
89     }
90     
91 }
92
Popular Tags