blob: ceeebe383340f614e17ccca8c89b27b28bfaf564 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mail.content;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.util.SparseArray;
/**
* A cursor-backed type that can return an object for each row of the cursor. This class is most
* useful when:
* 1. The cursor is returned in conjunction with an AsyncTaskLoader and created off the UI thread.
* 2. A single row in the cursor specifies everything for an object.
*/
public class ObjectCursor <T> extends CursorWrapper {
/** The cache for objects in the underlying cursor. */
private final SparseArray<T> mCache;
/** An object that knows how to construct {@link T} objects using cursors. */
private final CursorCreator<T> mFactory;
/**
* Creates a new object cursor.
* @param cursor the underlying cursor this wraps.
*/
public ObjectCursor(Cursor cursor, CursorCreator<T> factory) {
super(cursor);
if (cursor != null) {
mCache = new SparseArray<T>(cursor.getCount());
} else {
mCache = null;
}
mFactory = factory;
}
/**
* Create a concrete object at the current cursor position. There is no guarantee on object
* creation: an object might have been previously created, or the cache might be populated
* by calling {@link #fillCache()}. In both these cases, the previously created object is
* returned.
* @return a model
*/
public final T getModel() {
final Cursor c = getWrappedCursor();
if (c == null ) {
return null;
}
final int currentPosition = c.getPosition();
// The cache contains this object, return it.
final T prev = mCache.get(currentPosition);
if (prev != null) {
return prev;
}
// Get the object at the current position and add it to the cache.
final T model = mFactory.createFromCursor(c);
mCache.put(currentPosition, model);
return model;
}
/**
* Reads the entire cursor to populate the objects in the cache. Subsequent calls to {@link
* #getModel()} will return the cached objects as far as the underlying cursor does not change.
*/
final void fillCache() {
final Cursor c = getWrappedCursor();
if (c == null || !c.moveToFirst()) {
return;
}
do {
// As a side effect of getModel, the model is cached away.
getModel();
} while (c.moveToNext());
}
@Override
public void close() {
super.close();
mCache.clear();
}
}
|