Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
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 com.facebook.widget;
18
 
19
import android.database.CursorIndexOutOfBoundsException;
20
import com.facebook.model.GraphObject;
21
 
22
import java.util.ArrayList;
23
import java.util.Collection;
24
 
25
class SimpleGraphObjectCursor<T extends GraphObject> implements GraphObjectCursor<T> {
26
    private int pos = -1;
27
    private boolean closed = false;
28
    private ArrayList<T> graphObjects = new ArrayList<T>();
29
    private boolean moreObjectsAvailable = false;
30
    private boolean fromCache = false;
31
 
32
    SimpleGraphObjectCursor() {
33
    }
34
 
35
    SimpleGraphObjectCursor(SimpleGraphObjectCursor<T> other) {
36
        pos = other.pos;
37
        closed = other.closed;
38
        graphObjects = new ArrayList<T>();
39
        graphObjects.addAll(other.graphObjects);
40
        fromCache = other.fromCache;
41
 
42
        // We do not copy observers.
43
    }
44
 
45
    public void addGraphObjects(Collection<T> graphObjects, boolean fromCache) {
46
        this.graphObjects.addAll(graphObjects);
47
        // We consider this cached if ANY results were from the cache.
48
        this.fromCache |= fromCache;
49
    }
50
 
51
    public boolean isFromCache() {
52
        return fromCache;
53
    }
54
 
55
    public void setFromCache(boolean fromCache) {
56
        this.fromCache = fromCache;
57
    }
58
 
59
    public boolean areMoreObjectsAvailable() {
60
        return moreObjectsAvailable;
61
    }
62
 
63
    public void setMoreObjectsAvailable(boolean moreObjectsAvailable) {
64
        this.moreObjectsAvailable = moreObjectsAvailable;
65
    }
66
 
67
    @Override
68
    public int getCount() {
69
        return graphObjects.size();
70
    }
71
 
72
    @Override
73
    public int getPosition() {
74
        return pos;
75
    }
76
 
77
    @Override
78
    public boolean move(int offset) {
79
        return moveToPosition(pos + offset);
80
    }
81
 
82
    @Override
83
    public boolean moveToPosition(int position) {
84
        final int count = getCount();
85
        if (position >= count) {
86
            pos = count;
87
            return false;
88
        }
89
 
90
        if (position < 0) {
91
            pos = -1;
92
            return false;
93
        }
94
 
95
        pos = position;
96
        return true;
97
    }
98
 
99
    @Override
100
    public boolean moveToFirst() {
101
        return moveToPosition(0);
102
    }
103
 
104
    @Override
105
    public boolean moveToLast() {
106
        return moveToPosition(getCount() - 1);
107
    }
108
 
109
    @Override
110
    public boolean moveToNext() {
111
        return moveToPosition(pos + 1);
112
    }
113
 
114
    @Override
115
    public boolean moveToPrevious() {
116
        return moveToPosition(pos - 1);
117
    }
118
 
119
    @Override
120
    public boolean isFirst() {
121
        return (pos == 0) && (getCount() != 0);
122
    }
123
 
124
    @Override
125
    public boolean isLast() {
126
        final int count = getCount();
127
        return (pos == (count - 1)) && (count != 0);
128
    }
129
 
130
    @Override
131
    public boolean isBeforeFirst() {
132
        return (getCount() == 0) || (pos == -1);
133
    }
134
 
135
    @Override
136
    public boolean isAfterLast() {
137
        final int count = getCount();
138
        return (count == 0) || (pos == count);
139
    }
140
 
141
    @Override
142
    public T getGraphObject() {
143
        if (pos < 0) {
144
            throw new CursorIndexOutOfBoundsException("Before first object.");
145
        }
146
        if (pos >= graphObjects.size()) {
147
            throw new CursorIndexOutOfBoundsException("After last object.");
148
        }
149
        return graphObjects.get(pos);
150
    }
151
 
152
    @Override
153
    public void close() {
154
        closed = true;
155
    }
156
 
157
    @Override
158
    public boolean isClosed() {
159
        return closed;
160
    }
161
 
162
}