The Java Cursor Class

Cursor provides methods for iterating through instances of a class.

For an overview see page Java Classes

Class Definition

 
    public class Cursor<T> implements Iterable<T>, Iterator<T>, java.io.Closeable
    {
     
        public enum Operation 
        {
            LessThan,
            LessOrEquals,
            Equals,
            GreaterOrEquals,
            GreaterThan,
            Overlaps,
            Contains,
            ExactMatch,
            BestMatch,
            PrefixMatch,
            NextMatch,
            Neighbourhood,
            StrictEquals,
            IsPrefixOf,
            ContainsAll,
            ContainsAny
        };
         
        public Cursor(Connection con, Class cls, String indexName) 
        {
            ...
        }
        public Cursor(Connection con, Class cls) 
        {
            ...
        }
         
        public boolean search(Operation op, Object... value) 
        {
            ...
        }
         
        public boolean search(Operation op, long value) 
        {
            ...
        }
         
        public boolean search(Operation op, double value) 
        {
            ...
        }
         
        public boolean search(Operation op, String value) 
        {
            ...
        }
         
        public T find(Object... value) 
        {
            ...
        }
         
        public T find(long value) 
        {
            ...
        }
         
        public T find(String value) 
        {
            ...
        }
         
        public boolean hasPrev() 
        {
            ...
        }
         
        public boolean hasNext() 
        {
            ...
        }
         
        public boolean moveNext()
        {
            ...
        }
         
        public boolean movePrev()
        {
            ...
        }
         
        public T skip(int offset)
        {
            ...
        }
         
        public T getCurrent() 
        {
            ...
        }
         
        public T next() 
        {
            ...
        }
         
        public T prev() 
        {
            ...
        }
         
        public T first() 
        {
            ...
        }
         
        public T last() 
        {
            ...
        }
         
        public void remove() 
        {
            ...
        }
         
        public void update() 
        {
            ...
        }
         
        public void checkpoint() 
        {
            ...
        }
         
        public long getAutoId() 
        {
            ...
        }
         
        public void close() 
        {
            ...
        }
         
        public Iterator<T> iterator() 
        {
            ...
        }
    };
     

Methods

public Cursor(Connection con,

Class cls, String indexName)

Construct a cursor to iterate through instances of the specified class. (inheritance is not considered)

Parameters:

con A database connection
cls The iterated class
indexName The name of the index used for iteration and searching.

public Cursor(Connection con,

Class cls)

Construct a cursor for a class having a list or AUTOID index (list() or autoid() attributes of the Persistent annotation)

Parameters:

con A database connection
cls The iterated class

public boolean search(Operation op, Object... value)

public boolean search(Operation op, long value)

public boolean search(Operation op, double value)

public boolean search(Operation op, String value)

Perform the specified search operation op.

Parameters:

op The operation to be performed from enum Operation
value The Object, long, double or String key value to search

This method can be used for any type of index; the search operations supported depend on index type:

Hashtable Equals
BTree LessThan, LessOrEquals, Equals, GreaterThan, GreaterOrEquals
RTree Equals, Overlaps,Contains, Neighbourhood
Patricia Equals, ExactMatch, BestMatch, PrefixMatch, NextMatch
Trigram Contains

This method positions the cursor at the first element matching the specified search condition. Move forward or backward to locate all other elements for which this condition is true.

For example, in a LessThan search move backward and in a GreaterOrEquals search move forward.

public T find(Object... value)

public T find(long value)

public T find(String value)

Perform an exact match search on a unique index. Return an object with specified key or null if no object with this key value is found.

Parameters:

value The Object, long or String key value to search

This method is equivalent to

   
  cursor.search(Cursor.Equals, value);  
  T result = cursor.next();
   
public boolean hasPrev() Check if there is a previous result
public boolean hasNext() Check if there is a next result
public boolean moveNext() Move the cursor position forward without fetching the current object. This method allows traversal of the cursor without fetching objects. A desired object can be explicitly fetched using the getCurrent() method. Return true if the current element is not the last one, false otherwise
public boolean movePrev() Move the cursor position backward without fetching the current object This method allows traversal of the cursor without fetching objects. A desired object can be explicitly fetched using the getCurrent() method. Return true if the current element is not the first one, false otherwise
public T skip(int offset)

Skip the specified number of results. Return the object at the specified position in the cursor or null if this position can not be reached.

Parameters:

offset If positive move forward, if negative move backward (zero value is not accepted)
public T getCurrent() Get current object. This method should be used only with methods moveNext()or movePrev() as alternative to next() or prev()
public T next() Move forward. Return the next object in the result set or null if there are no more objects
public T prev() Move backward. Return the previous object in the result set or null if there are no more objects
public T first() Get the first object in the result set. Return the first object in the result set or null if the result set is empty
public T last() Get the last object in the result set. Return the last object in the result set or null if the result set is empty
public void remove() Remove the current object; i.e. the object previously returned by methods first(), last(), next(), prev() or skip()
public void update() Update the current object; i.e. the object previously returned by methods first(), last(), next(), prev() or skip()
public boolean checkpoint() Checkpoint the current object; i.e. Insert indexes for the updated object previously returned by methods first(), last(), next(), prev() or skip(). Return false if there is an MVCC conflict.
public long getAutoId() Get the AUTOID of the current object (if it has one). Return the AUTOID of the object previously returned by methods first(), last(), next(), prev() or skip()
public void close() Close the cursor
public Iterator<T> iterator() Return the iterator