My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (C) 2010 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.google.providerhelper;

import java.lang.reflect.Method;
import java.util.Iterator;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
* Provides a java-style iterator over objects retrieved from rows of an Android
* ContentProvider; the objects' class must extend Builder.
* <p>
* Here is an example of the use of this class to process Call objects which
* represent entries in the phone-call log, assuming that Call extends Builder.
* </p>
*
* <pre>
* Reader&lt;Call&gt; calls = new Reader&lt;Call&gt;(Call.class, activity,
* CallLog.Calls.CONTENT_URI);
* for (Call call : calls) {
* // do something with call
* }
* </pre>
*
* @see Builder
*/
public class Reader<T extends Builder> implements Iterator<T>, Iterable<T> {
private Class<T> rowClass;
private Method loader;
private Cursor cursor;
private boolean moreToCome;

/**
* Creates a reader given a class to load, an Activity, and a
* Content-Provider Uri.
*
* @param row
* The class of the object which is to be loaded. Must extend
* Builder.
* @param c
* The current context.
* @param u
* The Uri which identifies the Content Provider to load from.
*/
public Reader(Class<T> rowClass, Context c, Uri u) {
this.rowClass = rowClass;
try {
loader = rowClass.getMethod("load", Cursor.class);
cursor = c.getContentResolver().query(u, null, null, null, null);
moreToCome = cursor == null ? false : cursor.moveToFirst();
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

/**
* Creates a reader given a class to load and a Cursor to read from.
*
* @param The
* class of the object which is to be loaded. Must extend
* Builder.
* @param The
* android.database.Cursor to read from.
*/
public Reader(Class<T> rowClass, Cursor cursor) {
this.rowClass = rowClass;
this.cursor = cursor;
try {
loader = rowClass.getMethod("load", Cursor.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
moreToCome = cursor.moveToFirst();
}

public boolean hasNext() {
return moreToCome;
}

public T next() {
try {
T next = (T) loader.invoke(rowClass.newInstance(), cursor);
moreToCome = cursor.moveToNext();
if ( !moreToCome ) {
cursor.close();
}
return next;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* Not implemented.
*/
public void remove() {
throw new RuntimeException(
"Reader does not support removal from Content Providers");
}

public Iterator<T> iterator() {
return this;
}
}

Change log

r5 by kolakowski.damian on Apr 16, 2010   Diff
Capturing "null" returned by
ContentProvider.query(..)
Go to: 
Project members, sign in to write a code review

Older revisions

r4 by twb...@google.com on Apr 13, 2010   Diff
Initial import

All revisions of this file

File info

Size: 3652 bytes, 121 lines
Powered by Google Project Hosting