PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/appengine/datanucleus/query/CursorHelper.java

http://datanucleus-appengine.googlecode.com/
Java | 78 lines | 25 code | 7 blank | 46 comment | 3 complexity | ca4131bc40ec3307b9ec022d3ceb3745 MD5 | raw file
 1/*
 2 * Copyright (C) 2010 Google Inc
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.google.appengine.datanucleus.query;
17
18import com.google.appengine.api.datastore.Cursor;
19import com.google.appengine.api.datastore.QueryResultIterator;
20
21import java.util.Iterator;
22import java.util.List;
23
24/**
25 * Utilities for extracting {@link Cursor Cursors} from query results.
26 *
27 * @author Max Ross <max.ross@gmail.com>
28 */
29class CursorHelper {
30
31  static final String QUERY_CURSOR_PROPERTY_NAME = "gae.query.cursor";
32
33  CursorHelper() {}
34
35  /**
36   * Extract a {@link Cursor} from the provided {@link List}.  The Cursor
37   * points to the last element in the list.  A query that
38   * is executed using the returned Cursor will start scanning directly after
39   * the last element in the list.
40   * <b>
41   * A Cursor will only be available if the List is a query result and the
42   * query had a limit set.
43   *
44   * @param list The {@link List} from which to extract a {@link Cursor}.
45   * @return The {@link Cursor}, or {@code null} if no Cursor is available for
46   * the provided list.
47   */
48  public static Cursor getCursor(List<?> list) {
49    if (list instanceof StreamingQueryResult) {
50      StreamingQueryResult sqr = (StreamingQueryResult) list;
51      return sqr.getEndCursor();
52    }
53    return null;
54  }
55
56  /**
57   * Extract a {@link Cursor} from the provided {@link Iterator}.  The Cursor
58   * points to the element most recently returned by the iterator.  A query
59   * that is executed using the returned Cursor will start scanning directly
60   * after this element.
61   * <b>
62   * A Cursor will only be available if the Iterator was created from a query
63   * result and the query did not have a limit set.
64   *
65   * @param iter The {@link Iterator} from which to extract a {@link Cursor}.
66   * @return The {@link Cursor}, or {@code null} if no Cursor is available for
67   * the provided cursor.
68   */
69  public static Cursor getCursor(Iterator<?> iter) {
70    if (iter instanceof LazyResult.LazyAbstractListIterator) {
71      Iterator<?> innerIter = ((LazyResult.LazyAbstractListIterator) iter).getInnerIterator();
72      if (innerIter instanceof QueryResultIterator) {
73        return ((QueryResultIterator) innerIter).getCursor();
74      }
75    }
76    return null;
77  }
78}