<commit_before>/*
 * ********************************************************************************************************************
 *  <p/>
 *  BACKENDLESS.COM CONFIDENTIAL
 *  <p/>
 *  ********************************************************************************************************************
 *  <p/>
 *  Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
 *  <p/>
 *  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.
 *  <p/>
 *  ********************************************************************************************************************
 */

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<E> extends AbstractBackendlessCollection<E>
{
  private final Object dataLock = new Object();
  private Class<E> type;
  private volatile IBackendlessQuery query;
  private String tableName;

  public BackendlessCollection()
  {
    data = new CopyOnWriteArrayList<E>();
  }

  public Class<E> getType()
  {
    return type;
  }

  public void setType( Class<E> type )
  {
    this.type = type;
  }

  public void setTableName( String tableName )
  {
    this.tableName = tableName;
  }

  public void setPageSize( int pageSize )
  {
    query.setPageSize( pageSize );
  }

  public List<E> getCurrentPage()
  {
    return data;
  }

  //Sync methods
  public BackendlessCollection<E> nextPage() throws BackendlessException
  {
    checkQuery();
    int offset = query.getOffset();
    int pageSize = query.getPageSize();

    return getPage( pageSize, offset + pageSize );
  }

  public BackendlessCollection<E> getPage( int pageSize, int offset ) throws BackendlessException
  {
    return downloadPage( pageSize, offset );
  }

  //Download page logic
  private BackendlessCollection<E> downloadPage( int pageSize, int offset )  throws BackendlessException
  {
    checkQuery();
    IBackendlessQuery tempQuery = query.newInstance();
    tempQuery.setOffset( offset );
    tempQuery.setPageSize( pageSize );

    if( tempQuery instanceof BackendlessGeoQuery )
    {
      return (BackendlessCollection<E>) Backendless.Geo.getPoints( (BackendlessGeoQuery) tempQuery );
    }
    else
    {
      if( tableName != null )
        return (BackendlessCollection<E>) Backendless.Persistence.of( tableName ).find( (BackendlessDataQuery) tempQuery );
      else
        return Backendless.Persistence.find( type, (BackendlessDataQuery) tempQuery );
    }
  }

  public BackendlessCollection<E> 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<E> newInstance()
  {
    BackendlessCollection<E> result = new BackendlessCollection<E>();
    result.setData( data );
    result.setQuery( query );
    result.setType( type );
    result.setTableName( tableName );
    result.setTotalObjects( totalObjects );

    return result;
  }

  public void setData( List<E> data )
  {
    synchronized( dataLock )
    {
      this.data = new CopyOnWriteArrayList<E>( 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<BackendlessCollection<E>> responder )
  {
    new CollectionDownloadTask( responder )
    {
      @Override
      protected BackendlessCollection<E> doInBackground( int pageSize, int offset ) throws Exception
      {
        return nextPage();
      }
    }.executeThis();
  }

  public void previousPage( AsyncCallback<BackendlessCollection<E>> responder )
  {
    new CollectionDownloadTask( responder )
    {
      @Override
      protected BackendlessCollection<E> doInBackground( int pageSize, int offset ) throws Exception
      {
        return previousPage();
      }
    }.executeThis();
  }

  public void getPage( int pageSize, int offset, AsyncCallback<BackendlessCollection<E>> responder )
  {
    new CollectionDownloadTask( responder )
    {
      @Override
      protected BackendlessCollection<E> 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;
  }
}<commit_msg>fixed BKNDLSS-12214 Service invocation failed by timeout in hosted service http://support.backendless.com/topic/service-invocation-failed-by-timeout-on-hosted-service<commit_after>/*
 * ********************************************************************************************************************
 *  <p/>
 *  BACKENDLESS.COM CONFIDENTIAL
 *  <p/>
 *  ********************************************************************************************************************
 *  <p/>
 *  Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
 *  <p/>
 *  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.
 *  <p/>
 *  ********************************************************************************************************************
 */

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 weborb.service.ExcludeProperties;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@ExcludeProperties( propertyNames = {"type"} )
public class BackendlessCollection<E> extends AbstractBackendlessCollection<E>
{
  private final Object dataLock = new Object();
  private Class<E> type;
  private volatile IBackendlessQuery query;
  private String tableName;

  public BackendlessCollection()
  {
    data = new CopyOnWriteArrayList<E>();
  }

  public Class<E> getType()
  {
    return type;
  }

  public void setType( Class<E> type )
  {
    this.type = type;
  }

  public void setTableName( String tableName )
  {
    this.tableName = tableName;
  }

  public void setPageSize( int pageSize )
  {
    query.setPageSize( pageSize );
  }

  public List<E> getCurrentPage()
  {
    return data;
  }

  //Sync methods
  public BackendlessCollection<E> nextPage() throws BackendlessException
  {
    checkQuery();
    int offset = query.getOffset();
    int pageSize = query.getPageSize();

    return getPage( pageSize, offset + pageSize );
  }

  public BackendlessCollection<E> getPage( int pageSize, int offset ) throws BackendlessException
  {
    return downloadPage( pageSize, offset );
  }

  //Download page logic
  private BackendlessCollection<E> downloadPage( int pageSize, int offset )  throws BackendlessException
  {
    checkQuery();
    IBackendlessQuery tempQuery = query.newInstance();
    tempQuery.setOffset( offset );
    tempQuery.setPageSize( pageSize );

    if( tempQuery instanceof BackendlessGeoQuery )
    {
      return (BackendlessCollection<E>) Backendless.Geo.getPoints( (BackendlessGeoQuery) tempQuery );
    }
    else
    {
      if( tableName != null )
        return (BackendlessCollection<E>) Backendless.Persistence.of( tableName ).find( (BackendlessDataQuery) tempQuery );
      else
        return Backendless.Persistence.find( type, (BackendlessDataQuery) tempQuery );
    }
  }

  public BackendlessCollection<E> 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<E> newInstance()
  {
    BackendlessCollection<E> result = new BackendlessCollection<E>();
    result.setData( data );
    result.setQuery( query );
    result.setType( type );
    result.setTableName( tableName );
    result.setTotalObjects( totalObjects );

    return result;
  }

  public void setData( List<E> data )
  {
    synchronized( dataLock )
    {
      this.data = new CopyOnWriteArrayList<E>( 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<BackendlessCollection<E>> responder )
  {
    new CollectionDownloadTask( responder )
    {
      @Override
      protected BackendlessCollection<E> doInBackground( int pageSize, int offset ) throws Exception
      {
        return nextPage();
      }
    }.executeThis();
  }

  public void previousPage( AsyncCallback<BackendlessCollection<E>> responder )
  {
    new CollectionDownloadTask( responder )
    {
      @Override
      protected BackendlessCollection<E> doInBackground( int pageSize, int offset ) throws Exception
      {
        return previousPage();
      }
    }.executeThis();
  }

  public void getPage( int pageSize, int offset, AsyncCallback<BackendlessCollection<E>> responder )
  {
    new CollectionDownloadTask( responder )
    {
      @Override
      protected BackendlessCollection<E> 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;
  }
}
{'fraction_non_alphanumeric': 0.1479856051961731, 'fraction_numerical': 0.0014921442991310454, 'mean_word_length': 3.631707317073171, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '969981', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3136, 'n_words': 813}