001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o;
022:
023: import java.util.*;
024:
025: /**
026: * JDK 2 Iterator
027: *
028: * Intended state: i_next is always active.
029: *
030: * @persistent
031: */
032: class P2ListElementIterator implements Iterator {
033:
034: private final P2LinkedList i_list;
035:
036: private P1ListElement i_preprevious;
037: private P1ListElement i_previous;
038: private P1ListElement i_next;
039:
040: P2ListElementIterator(P2LinkedList a_list, P1ListElement a_next) {
041: i_list = a_list;
042: i_next = a_next;
043: checkNextActive();
044: }
045:
046: private void checkNextActive() {
047: if (i_next != null) {
048: i_next.checkActive();
049: }
050: }
051:
052: public void remove() {
053: if (i_previous != null) {
054: synchronized (i_previous.streamLock()) {
055: if (i_preprevious != null) {
056: i_preprevious.i_next = i_previous.i_next;
057: i_preprevious.update();
058: }
059: i_list.checkRemoved(i_preprevious, i_previous);
060: i_previous.delete(i_list.i_deleteRemoved);
061: }
062: }
063: }
064:
065: public boolean hasNext() {
066: return i_next != null;
067: }
068:
069: public Object next() {
070: if (i_next != null) {
071: synchronized (i_next.streamLock()) {
072: i_preprevious = i_previous;
073: i_previous = i_next;
074: Object obj = i_next.activatedObject(i_list
075: .elementActivationDepth());
076: i_next = i_next.i_next;
077: checkNextActive();
078: return obj;
079: }
080: }
081: return null;
082: }
083:
084: P1ListElement nextElement() {
085: i_preprevious = i_previous;
086: i_previous = i_next;
087: i_next = i_next.i_next;
088: checkNextActive();
089: return i_previous;
090: }
091:
092: P1ListElement move(int a_elements) {
093: if (a_elements < 0) {
094: return null;
095: }
096: for (int i = 0; i < a_elements; i++) {
097: if (hasNext()) {
098: nextElement();
099: } else {
100: return null;
101: }
102: }
103: if (hasNext()) {
104: return nextElement();
105: }
106: return null;
107: }
108:
109: }
|