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.internal.query.result;
022:
023: import com.db4o.ext.*;
024: import com.db4o.foundation.*;
025: import com.db4o.query.QueryComparator;
026:
027: /**
028: * @exclude
029: */
030: public class StatefulQueryResult implements Iterable4 {
031:
032: private final QueryResult _delegate;
033: private final Iterable4Adaptor _iterable;
034:
035: public StatefulQueryResult(QueryResult queryResult) {
036: _delegate = queryResult;
037: _iterable = new Iterable4Adaptor(queryResult);
038: }
039:
040: public Object get(int index) {
041: synchronized (lock()) {
042: return _delegate.get(index);
043: }
044: }
045:
046: public long[] getIDs() {
047: synchronized (lock()) {
048: long[] ids = new long[size()];
049: int i = 0;
050: final IntIterator4 iterator = _delegate.iterateIDs();
051: while (iterator.moveNext()) {
052: ids[i++] = iterator.currentInt();
053: }
054: return ids;
055: }
056: }
057:
058: public boolean hasNext() {
059: synchronized (lock()) {
060: return _iterable.hasNext();
061: }
062: }
063:
064: public Object next() {
065: synchronized (lock()) {
066: return _iterable.next();
067: }
068: }
069:
070: public void reset() {
071: synchronized (lock()) {
072: _iterable.reset();
073: }
074: }
075:
076: public int size() {
077: synchronized (lock()) {
078: return _delegate.size();
079: }
080: }
081:
082: public void sort(QueryComparator cmp) {
083: synchronized (lock()) {
084: _delegate.sort(cmp);
085: }
086: }
087:
088: public Object lock() {
089: return _delegate.lock();
090: }
091:
092: ExtObjectContainer objectContainer() {
093: return _delegate.objectContainer();
094: }
095:
096: public int indexOf(Object a_object) {
097: synchronized (lock()) {
098: int id = (int) objectContainer().getID(a_object);
099: if (id <= 0) {
100: return -1;
101: }
102: return _delegate.indexOf(id);
103: }
104: }
105:
106: public Iterator4 iterateIDs() {
107: synchronized (lock()) {
108: return _delegate.iterateIDs();
109: }
110: }
111:
112: public Iterator4 iterator() {
113: synchronized (lock()) {
114: return _delegate.iterator();
115: }
116: }
117: }
|