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.foundation;
022:
023: /**
024: * Using the CollectionElement the other way around:
025: * CollectionElement.i_next points to the previous element
026: *
027: * @exclude
028: */
029: public class NonblockingQueue implements Queue4 {
030:
031: protected final class Queue4Iterator implements Iterator4 {
032:
033: protected boolean _active = false;
034: protected List4 _current = null;
035:
036: public Object current() {
037: return _current._element;
038: }
039:
040: public boolean moveNext() {
041: if (!_active) {
042: _current = _last;
043: _active = true;
044: } else {
045: if (_current != null) {
046: _current = _current._next;
047: }
048: }
049: return _current != null;
050: }
051:
052: public void reset() {
053: _current = null;
054: _active = false;
055: }
056: }
057:
058: private List4 _first;
059: protected List4 _last;
060:
061: /* (non-Javadoc)
062: * @see com.db4o.foundation.Queue4#add(java.lang.Object)
063: */
064: public final void add(Object obj) {
065: List4 ce = new List4(null, obj);
066: if (_first == null) {
067: _last = ce;
068: } else {
069: _first._next = ce;
070: }
071: _first = ce;
072: }
073:
074: /* (non-Javadoc)
075: * @see com.db4o.foundation.Queue4#next()
076: */
077: public final Object next() {
078: if (_last == null) {
079: return null;
080: }
081: Object ret = _last._element;
082: _last = _last._next;
083: if (_last == null) {
084: _first = null;
085: }
086: return ret;
087: }
088:
089: /* (non-Javadoc)
090: * @see com.db4o.foundation.Queue4#hasNext()
091: */
092: public final boolean hasNext() {
093: return _last != null;
094: }
095:
096: /* (non-Javadoc)
097: * @see com.db4o.foundation.Queue4#iterator()
098: */
099: public Iterator4 iterator() {
100: return new Queue4Iterator();
101: }
102:
103: }
|