Debian Codegraph @

core/java/android/content/CursorEntityIterator.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009 The Android Open Source Project
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
17 package android.content;
18
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.database.Cursor;
21 import android.os.RemoteException;
22
23 /**
24 * Abstract implementation of EntityIterator that makes it easy to wrap a cursor
25 * that can contain several consecutive rows for an entity.
26 * @hide
27 */
28 public abstract class CursorEntityIterator implements EntityIterator {
29 private final Cursor mCursor;
30 private boolean mIsClosed;
31
32 /**
33 * Constructor that makes initializes the cursor such that the iterator points to the
34 * first Entity, if there are any.
35 * @param cursor the cursor that contains the rows that make up the entities
36 */
37 @UnsupportedAppUsage
38 public CursorEntityIterator(Cursor cursor) {
39 mIsClosed = false;
40 mCursor = cursor;
41 mCursor.moveToFirst();
42 }
43
44 /**
45 * Returns the entity that the cursor is currently pointing to. This must take care to advance
46 * the cursor past this entity. This will never be called if the cursor is at the end.
47 * @param cursor the cursor that contains the entity rows
48 * @return the entity that the cursor is currently pointing to
49 * @throws RemoteException if a RemoteException is caught while attempting to build the Entity
50 */
51 public abstract Entity getEntityAndIncrementCursor(Cursor cursor) throws RemoteException;
52
53 /**
54 * Returns whether there are more elements to iterate, i.e. whether the
55 * iterator is positioned in front of an element.
56 *
57 * @return {@code true} if there are more elements, {@code false} otherwise.
58 * @see EntityIterator#next()
59 */
60 public final boolean hasNext() {
61 if (mIsClosed) {
62 throw new IllegalStateException("calling hasNext() when the iterator is closed");
63 }
64
65 return !mCursor.isAfterLast();
66 }
67
68 /**
69 * Returns the next object in the iteration, i.e. returns the element in
70 * front of the iterator and advances the iterator by one position.
71 *
72 * @return the next object.
73 * @throws java.util.NoSuchElementException
74 * if there are no more elements.
75 * @see EntityIterator#hasNext()
76 */
77 public Entity next() {
78 if (mIsClosed) {
79 throw new IllegalStateException("calling next() when the iterator is closed");
80 }
81 if (!hasNext()) {
82 throw new IllegalStateException("you may only call next() if hasNext() is true");
83 }
84
85 try {
86 return getEntityAndIncrementCursor(mCursor);
87 } catch (RemoteException e) {
88 throw new RuntimeException("caught a remote exception, this process will die soon", e);
89 }
90 }
91
92 public void remove() {
93 throw new UnsupportedOperationException("remove not supported by EntityIterators");
94 }
95
96 public final void reset() {
97 if (mIsClosed) {
98 throw new IllegalStateException("calling reset() when the iterator is closed");
99 }
100 mCursor.moveToFirst();
101 }
102
103 /**
104 * Indicates that this iterator is no longer needed and that any associated resources
105 * may be released (such as a SQLite cursor).
106 */
107 public final void close() {
108 if (mIsClosed) {
109 throw new IllegalStateException("closing when already closed");
110 }
111 mIsClosed = true;
112 mCursor.close();
113 }
114 }