Example usage for com.mongodb.client MongoCursor MongoCursor

List of usage examples for com.mongodb.client MongoCursor MongoCursor

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor MongoCursor.

Prototype

MongoCursor

Source Link

Usage

From source file:com.ibm.research.mongotx.lrc.LRCSimpleTxDBCursor.java

License:Open Source License

@Override
public MongoCursor<Document> iterator() {

    return new MongoCursor<Document>() {
        boolean filled = false;
        int index = -1;

        private void fillIfNecessary() {
            if (filled)
                return;

            LRCSimpleTxDBCursor.this.fillIfNecessary();

            filled = true;//  w  ww.  java2 s.co  m
        }

        @Override
        public void close() {
        }

        @Override
        public boolean hasNext() {
            synchronized (tx) {
                fillIfNecessary();
                return index < (results.size() - 1);
            }
        }

        @Override
        public Document next() {
            synchronized (tx) {
                if (hasNext())
                    ++index;
                if (index >= results.size() || index < 0)
                    return null;
                return results.get(index);
            }
        }

        @Override
        public Document tryNext() {
            synchronized (tx) {
                if (hasNext())
                    return next();
                else
                    return null;
            }
        }

        @Override
        public ServerCursor getServerCursor() {
            throw new UnsupportedOperationException();
        }

        @Override
        public ServerAddress getServerAddress() {
            throw new UnsupportedOperationException();
        }
    };
}