Android Open Source - GradeCalculator-Android Storage Iterator






From Project

Back to project page GradeCalculator-Android.

License

The source code is released under:

Copyright (C) 2012 Jimmy Theis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in ...

If you think the Android project GradeCalculator-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jetheis.android.grades.storage;
/*from w w  w .j  a v a2  s .com*/
import java.util.Iterator;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * An abstract iterator for {@link Storable} subclasses retrieved from a
 * {@link SQLiteDatabase}. Subclasses should only need to implement
 * {@link #getObjectFromNextRow(Cursor)}, and will likely be internal to their
 * {@link Storable} subclass's {@link StorageAdapter} subclass.
 * 
 * @param <T>
 *            The {@link Storable} subclass that this {@link StorageIterator}
 *            provides.
 */
public abstract class StorageIterator<T extends Storable> implements Iterable<T>, Iterator<T> {

    private Cursor mCursor;

    /**
     * Default constructor. This initializes the iterator with a {@link Cursor}
     * pointing to a set of database query results. After this initialization,
     * the {@link StorageIterator} is ready to use.
     * 
     * @param cursor
     *            The fresh {@link Cursor} returned from a
     *            {@link SQLiteDatabase} query.
     */
    public StorageIterator(Cursor cursor) {
        mCursor = cursor;
    }

    /**
     * Convert the next row returned by the Cursor to a proper {@link Storable},
     * for use by the rest of the app. Implementations of this method need not
     * and should not call {@link Cursor#moveToNext()}, as this will be called
     * before the {@link Cursor} is passed ot this method.
     * 
     * @param cursor
     *            This {@link StorageIterator}'s internal {@link Cursor}, with
     *            the next row ready to be read and converted to a
     *            {@link Storable} ({@link Cursor#moveToNext()} already called).
     * @return A {@link Storable} subclass instance populated with the values
     *         from the next row of the {@link Cursor}.
     */
    protected abstract T getObjectFromNextRow(Cursor cursor);

    /**
     * Get the total number of results contained by this iterator's
     * {@link Cursor}.
     * 
     * @return The total number of results contained by this iterator's
     *         {@link Cursor}.
     */
    public int getCount() {
        return mCursor.getCount();
    }

    @Override
    public boolean hasNext() {
        return mCursor.getPosition() < mCursor.getCount() - 1;
    }

    @Override
    public T next() {
        mCursor.moveToNext();
        return getObjectFromNextRow(mCursor);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Remove not allowed on StorageIterators");
    }

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




Java Source Code List

com.jetheis.android.grades.Constants.java
com.jetheis.android.grades.activity.AboutActivity.java
com.jetheis.android.grades.activity.CourseActivity.java
com.jetheis.android.grades.activity.CourseListActivity.java
com.jetheis.android.grades.billing.BillingWrapper.java
com.jetheis.android.grades.billing.FreeBillingWrapper.java
com.jetheis.android.grades.billing.Security.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingConstants.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingReceiver.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingService.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingWrapper.java
com.jetheis.android.grades.fragment.AboutFragment.java
com.jetheis.android.grades.fragment.AddCourseDialogFragment.java
com.jetheis.android.grades.fragment.AddGradeComponentDialogFragment.java
com.jetheis.android.grades.fragment.BuyFullVersionFragment.java
com.jetheis.android.grades.fragment.CourseListFragment.java
com.jetheis.android.grades.fragment.CourseOverviewFragment.java
com.jetheis.android.grades.fragment.EditCourseDialogFragment.java
com.jetheis.android.grades.fragment.EditGradeComponentDialogFragment.java
com.jetheis.android.grades.fragment.GradeComponentListFragment.java
com.jetheis.android.grades.listadapter.CourseArrayAdapter.java
com.jetheis.android.grades.model.Course.java
com.jetheis.android.grades.model.GradeComponent.java
com.jetheis.android.grades.model.PercentageGradeComponent.java
com.jetheis.android.grades.model.PointTotalGradeComponent.java
com.jetheis.android.grades.storage.CourseStorageAdapter.java
com.jetheis.android.grades.storage.DatabaseHelper.java
com.jetheis.android.grades.storage.GradeComponentStorageAdapter.java
com.jetheis.android.grades.storage.Storable.java
com.jetheis.android.grades.storage.StorageAdapter.java
com.jetheis.android.grades.storage.StorageIterator.java