/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. */
  16. package com.google.appengine.datanucleus.query;
  17. import com.google.appengine.api.datastore.Cursor;
  18. import com.google.appengine.api.datastore.QueryResultIterator;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. /**
  22. * Utilities for extracting {@link Cursor Cursors} from query results.
  23. *
  24. * @author Max Ross <max.ross@gmail.com>
  25. */
  26. class CursorHelper {
  27. static final String QUERY_CURSOR_PROPERTY_NAME = "gae.query.cursor";
  28. CursorHelper() {}
  29. /**
  30. * Extract a {@link Cursor} from the provided {@link List}. The Cursor
  31. * points to the last element in the list. A query that
  32. * is executed using the returned Cursor will start scanning directly after
  33. * the last element in the list.
  34. * <b>
  35. * A Cursor will only be available if the List is a query result and the
  36. * query had a limit set.
  37. *
  38. * @param list The {@link List} from which to extract a {@link Cursor}.
  39. * @return The {@link Cursor}, or {@code null} if no Cursor is available for
  40. * the provided list.
  41. */
  42. public static Cursor getCursor(List<?> list) {
  43. if (list instanceof StreamingQueryResult) {
  44. StreamingQueryResult sqr = (StreamingQueryResult) list;
  45. return sqr.getEndCursor();
  46. }
  47. return null;
  48. }
  49. /**
  50. * Extract a {@link Cursor} from the provided {@link Iterator}. The Cursor
  51. * points to the element most recently returned by the iterator. A query
  52. * that is executed using the returned Cursor will start scanning directly
  53. * after this element.
  54. * <b>
  55. * A Cursor will only be available if the Iterator was created from a query
  56. * result and the query did not have a limit set.
  57. *
  58. * @param iter The {@link Iterator} from which to extract a {@link Cursor}.
  59. * @return The {@link Cursor}, or {@code null} if no Cursor is available for
  60. * the provided cursor.
  61. */
  62. public static Cursor getCursor(Iterator<?> iter) {
  63. if (iter instanceof LazyResult.LazyAbstractListIterator) {
  64. Iterator<?> innerIter = ((LazyResult.LazyAbstractListIterator) iter).getInnerIterator();
  65. if (innerIter instanceof QueryResultIterator) {
  66. return ((QueryResultIterator) innerIter).getCursor();
  67. }
  68. }
  69. return null;
  70. }
  71. }