All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backendless.BackendlessCollection Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * ********************************************************************************************************************
 *  

* BACKENDLESS.COM CONFIDENTIAL *

* ******************************************************************************************************************** *

* Copyright 2012 BACKENDLESS.COM. All Rights Reserved. *

* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers, * if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its * suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret * or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden * unless prior written permission is obtained from Backendless.com. *

* ******************************************************************************************************************** */ package com.backendless; import com.backendless.async.callback.AsyncCallback; import com.backendless.commons.AbstractBackendlessCollection; import com.backendless.exceptions.BackendlessException; import com.backendless.geo.BackendlessGeoQuery; import com.backendless.persistence.BackendlessDataQuery; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class BackendlessCollection extends AbstractBackendlessCollection { private final Object dataLock = new Object(); private Class type; private volatile IBackendlessQuery query; private String tableName; public BackendlessCollection() { data = new CopyOnWriteArrayList(); } public Class getType() { return type; } public void setType( Class type ) { this.type = type; } public void setTableName( String tableName ) { this.tableName = tableName; } public void setPageSize( int pageSize ) { query.setPageSize( pageSize ); } public List getCurrentPage() { return data; } //Sync methods public BackendlessCollection nextPage() throws BackendlessException { checkQuery(); int offset = query.getOffset(); int pageSize = query.getPageSize(); return getPage( pageSize, offset + pageSize ); } public BackendlessCollection getPage( int pageSize, int offset ) throws BackendlessException { return downloadPage( pageSize, offset ); } //Download page logic private BackendlessCollection downloadPage( int pageSize, int offset ) throws BackendlessException { checkQuery(); IBackendlessQuery tempQuery = query.newInstance(); tempQuery.setOffset( offset ); tempQuery.setPageSize( pageSize ); if( tempQuery instanceof BackendlessGeoQuery ) { return (BackendlessCollection) Backendless.Geo.getPoints( (BackendlessGeoQuery) tempQuery ); } else { if( tableName != null ) return (BackendlessCollection) Backendless.Persistence.of( tableName ).find( (BackendlessDataQuery) tempQuery ); else return Backendless.Persistence.find( type, (BackendlessDataQuery) tempQuery ); } } public BackendlessCollection previousPage() throws BackendlessException { checkQuery(); int offset = query.getOffset(); int pageSize = query.getPageSize(); if( (offset - pageSize) >= 0 ) { return getPage( pageSize, offset - pageSize ); } else { return this.newInstance(); } } protected BackendlessCollection newInstance() { BackendlessCollection result = new BackendlessCollection(); result.setData( data ); result.setQuery( query ); result.setType( type ); result.setTableName( tableName ); result.setTotalObjects( totalObjects ); return result; } public void setData( List data ) { synchronized( dataLock ) { this.data = new CopyOnWriteArrayList( data ); } } public void setQuery( IBackendlessQuery query ) { if( query == null ) { this.query = null; } else { this.query = query.newInstance(); } } public IBackendlessQuery getQuery() { return query; } //Async methods public void nextPage( AsyncCallback> responder ) { new CollectionDownloadTask( responder ) { @Override protected BackendlessCollection doInBackground( int pageSize, int offset ) throws Exception { return nextPage(); } }.executeThis(); } public void previousPage( AsyncCallback> responder ) { new CollectionDownloadTask( responder ) { @Override protected BackendlessCollection doInBackground( int pageSize, int offset ) throws Exception { return previousPage(); } }.executeThis(); } public void getPage( int pageSize, int offset, AsyncCallback> responder ) { new CollectionDownloadTask( responder ) { @Override protected BackendlessCollection doInBackground( int pageSize, int offset ) throws Exception { return getPage( pageSize, offset ); } }.executeThis( pageSize, offset ); } private void checkQuery() { if( query != null ) return; BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery(); backendlessDataQuery.setOffset( 0 ); backendlessDataQuery.setPageSize( data.size() ); query = backendlessDataQuery; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy