Blame | Last modification | View Log | RSS feed
/*** Copyright 2010-present Facebook.** 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.facebook.widget;import android.database.CursorIndexOutOfBoundsException;import com.facebook.model.GraphObject;import java.util.ArrayList;import java.util.Collection;class SimpleGraphObjectCursor<T extends GraphObject> implements GraphObjectCursor<T> {private int pos = -1;private boolean closed = false;private ArrayList<T> graphObjects = new ArrayList<T>();private boolean moreObjectsAvailable = false;private boolean fromCache = false;SimpleGraphObjectCursor() {}SimpleGraphObjectCursor(SimpleGraphObjectCursor<T> other) {pos = other.pos;closed = other.closed;graphObjects = new ArrayList<T>();graphObjects.addAll(other.graphObjects);fromCache = other.fromCache;// We do not copy observers.}public void addGraphObjects(Collection<T> graphObjects, boolean fromCache) {this.graphObjects.addAll(graphObjects);// We consider this cached if ANY results were from the cache.this.fromCache |= fromCache;}public boolean isFromCache() {return fromCache;}public void setFromCache(boolean fromCache) {this.fromCache = fromCache;}public boolean areMoreObjectsAvailable() {return moreObjectsAvailable;}public void setMoreObjectsAvailable(boolean moreObjectsAvailable) {this.moreObjectsAvailable = moreObjectsAvailable;}@Overridepublic int getCount() {return graphObjects.size();}@Overridepublic int getPosition() {return pos;}@Overridepublic boolean move(int offset) {return moveToPosition(pos + offset);}@Overridepublic boolean moveToPosition(int position) {final int count = getCount();if (position >= count) {pos = count;return false;}if (position < 0) {pos = -1;return false;}pos = position;return true;}@Overridepublic boolean moveToFirst() {return moveToPosition(0);}@Overridepublic boolean moveToLast() {return moveToPosition(getCount() - 1);}@Overridepublic boolean moveToNext() {return moveToPosition(pos + 1);}@Overridepublic boolean moveToPrevious() {return moveToPosition(pos - 1);}@Overridepublic boolean isFirst() {return (pos == 0) && (getCount() != 0);}@Overridepublic boolean isLast() {final int count = getCount();return (pos == (count - 1)) && (count != 0);}@Overridepublic boolean isBeforeFirst() {return (getCount() == 0) || (pos == -1);}@Overridepublic boolean isAfterLast() {final int count = getCount();return (count == 0) || (pos == count);}@Overridepublic T getGraphObject() {if (pos < 0) {throw new CursorIndexOutOfBoundsException("Before first object.");}if (pos >= graphObjects.size()) {throw new CursorIndexOutOfBoundsException("After last object.");}return graphObjects.get(pos);}@Overridepublic void close() {closed = true;}@Overridepublic boolean isClosed() {return closed;}}