KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Adapts Iterable4/Iterator4 iteration model (moveNext, current) to the old db4o
25  * and jdk model (hasNext, next).
26  *
27  * @exclude
28  */

29 public class Iterable4Adaptor {
30     
31     private static final Object JavaDoc EOF = new Object JavaDoc();
32     private static final Object JavaDoc MOVE_NEXT = new Object JavaDoc();
33     
34     private final Iterable4 _delegate;
35     
36     private Iterator4 _iterator;
37     
38     private Object JavaDoc _current = MOVE_NEXT;
39     
40     public Iterable4Adaptor(Iterable4 delegate) {
41         _delegate = delegate;
42     }
43     
44     public boolean hasNext() {
45         if (_current == MOVE_NEXT) {
46             return moveNext();
47         }
48         return _current != EOF;
49     }
50     
51     public Object JavaDoc next() {
52         if (!hasNext()) {
53             throw new IllegalStateException JavaDoc();
54         }
55         Object JavaDoc returnValue = _current;
56         _current = MOVE_NEXT;
57         return returnValue;
58     }
59
60     private boolean moveNext() {
61         if (null == _iterator) {
62             _iterator = _delegate.iterator();
63         }
64         if (_iterator.moveNext()) {
65             _current = _iterator.current();
66             return true;
67         }
68         _current = EOF;
69         return false;
70     }
71
72     public void reset() {
73         _iterator = null;
74         _current = MOVE_NEXT;
75     }
76 }
77
Popular Tags