<commit_before>package com.apigee.sdk.data.client.entities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.apigee.sdk.data.client.ApigeeDataClient;
import com.apigee.sdk.data.client.ApigeeDataClient.Query;
import com.apigee.sdk.data.client.response.ApiResponse;

/**
 * Models a collection of entities as a local object. Collections
 * are the primary way that entities are organized in API BaaS.
 *
 * @see <a href="http://apigee.com/docs/app-services/content/collections">Collections documentation</a>
 */
public class Collection
{
	private ApigeeDataClient dataClient;
	private String type;
	private Map<String,Object> qs;

	private ArrayList<Entity> list;
	private int iterator;
	private ArrayList<String> previous;
	private String next;
	private String cursor;

	/**
	 * Default constructor for a Collection object.
	 *
	 * @param  dataClient  an instance of the DataClient class
	 * @param  type  the entity 'type' associated with the colleciton
	 * @param  qs  optional Map object of query parameters to apply to the collection retrieval
	 */	
	public Collection(ApigeeDataClient dataClient, String type, Map<String,Object> qs) {
	    this.dataClient = dataClient;
	    this.type = type;
	    
	    if( qs == null )
	    {
	    	this.qs = new HashMap<String,Object>();
	    }
	    else
	    {
	    	this.qs = qs;
	    }

	    this.list = new ArrayList<Entity>();
	    this.iterator = -1;

	    this.previous = new ArrayList<String>();
	    this.next = null;
	    this.cursor = null;

	    this.fetch();
	}

	/**
	 * Gets the entity 'type' associated with the collection.
	 *
	 * @return the collection type
	 */	
	public String getType(){
	   return this.type;
	}
	
	/**
	 * Sets the entity 'type' associated with the collection.
	 *
	 * @param  type  the collection type
	 */	
	public void setType(String type){
	   this.type = type;
	}

	/**
	 * Retrieves the current state of the collection from the server, and populates
	 * an the Collection object with the returned set of entities. Executes synchronously.
	 *
	 * @return  an ApiResponse object
	 */	
	public ApiResponse fetch() {
	    if (this.cursor != null) {
	    	this.qs.put("cursor", this.cursor);
	    }
	    
	    Query query = this.dataClient.queryEntitiesRequest("GET", this.qs, null,
                this.dataClient.getOrganizationId(),  this.dataClient.getApplicationId(), this.type);
	    ApiResponse response = query.getResponse();
	    if (response.getError() != null) {
	    	this.dataClient.writeLog("Error getting collection.");
	    } else {
	    	String theCursor = response.getCursor();
    		int count = response.getEntityCount();
    		
    		UUID nextUUID = response.getNext();
    		if( nextUUID != null ) {
    			this.next = nextUUID.toString();
    		} else {
    			this.next = null;
    		}
    		this.cursor = theCursor;

	    	this.saveCursor(theCursor);
	    	if ( count > 0 ) {
	    		this.resetEntityPointer();
	    		this.list = new ArrayList<Entity>();
	    		List<Entity> retrievedEntities = response.getEntities();
	    		
	    		for ( Entity retrievedEntity : retrievedEntities ) {
	    			if( retrievedEntity.getUuid() != null ) {
	    				retrievedEntity.setType(this.type);
	    				this.list.add(retrievedEntity);
	    			}
	    		}
	    	}
	    }
	    
	    return response;
	}

	/**
	 * Adds an entity to the Collection object.
	 *
	 * @param  entityData  a Map object of entity properties to be saved in the entity
	 * @return  an Entity object that represents the newly added entity. Must include
	 *		a 'type' property. Executes synchronously.
	 */	
	public Entity addEntity(Map<String,Object> entityData) {
		Entity entity = null;		
		ApiResponse response = this.dataClient.createEntity(entityData);
		if( (response != null) && (response.getError() == null) && (response.getEntityCount() > 0) ) {
			entity = response.getFirstEntity();
			if (entity != null) {
				this.list.add(entity);
			}
		}
		return entity;
	}

	/**
	 * Deletes the provided entity on the server, then updates the
	 * Collection object by calling fetch(). Executes synchronously.
	 *
	 * @param entity an Entity object that contains a 'type' and 'uuid' property
	 */	
	public ApiResponse destroyEntity(Entity entity) {
		ApiResponse response = entity.destroy();
		if (response.getError() != null) {
			this.dataClient.writeLog("Could not destroy entity.");
		} else {
			response = this.fetch();
		}
	    
		return response;
	}

	/**
	 * Retrieves an entity from the server.
	 *
	 * @param uuid the UUID of the entity to retrieve
	 * @return an ApiResponse object
	 */	
	public ApiResponse getEntityByUuid(UUID uuid) {
		Entity entity = new Entity(this.dataClient);
	    entity.setType(this.type);
	    entity.setUuid(uuid);
	    return entity.fetch();
	}

	/**
	 * Gets the first entity in the Collection object.
	 *
	 * @return  an Entity object
	 */	
	public Entity getFirstEntity() {
		return ((this.list.size() > 0) ? this.list.get(0) : null);
	}

	/**
	 * Gets the last entity in the Collection object.
	 *
	 * @return  an Entity object
	 */	
	public Entity getLastEntity() {
		return ((this.list.size() > 0) ? this.list.get(this.list.size()-1) : null);
	}

	/**
	 * Checks if there is another entity in the Collection after the current pointer position.<commit_msg>Added method to the Collection class that returns the full list of entities.<commit_after>package com.apigee.sdk.data.client.entities;

import com.apigee.sdk.data.client.ApigeeDataClient;
import com.apigee.sdk.data.client.ApigeeDataClient.Query;
import com.apigee.sdk.data.client.response.ApiResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Models a collection of entities as a local object. Collections
 * are the primary way that entities are organized in API BaaS.
 *
 * @see <a href="http://apigee.com/docs/app-services/content/collections">Collections documentation</a>
 */
public class Collection
{
	private ApigeeDataClient dataClient;
	private String type;
	private Map<String,Object> qs;

	private ArrayList<Entity> list;
	private int iterator;
	private ArrayList<String> previous;
	private String next;
	private String cursor;

	/**
	 * Default constructor for a Collection object.
	 *
	 * @param  dataClient  an instance of the DataClient class
	 * @param  type  the entity 'type' associated with the colleciton
	 * @param  qs  optional Map object of query parameters to apply to the collection retrieval
	 */	
	public Collection(ApigeeDataClient dataClient, String type, Map<String,Object> qs) {
	    this.dataClient = dataClient;
	    this.type = type;
	    
	    if( qs == null )
	    {
	    	this.qs = new HashMap<String,Object>();
	    }
	    else
	    {
	    	this.qs = qs;
	    }

	    this.list = new ArrayList<Entity>();
	    this.iterator = -1;

	    this.previous = new ArrayList<String>();
	    this.next = null;
	    this.cursor = null;

	    this.fetch();
	}

    /**
	 * Gets the entity 'type' associated with the collection.
	 *
	 * @return the collection type
	 */	
	public String getType(){
	   return this.type;
	}
	
	/**
	 * Sets the entity 'type' associated with the collection.
	 *
	 * @param  type  the collection type
	 */	
	public void setType(String type){
	   this.type = type;
	}

	/**
	 * Retrieves the current state of the collection from the server, and populates
	 * an the Collection object with the returned set of entities. Executes synchronously.
	 *
	 * @return  an ApiResponse object
	 */	
	public ApiResponse fetch() {
	    if (this.cursor != null) {
	    	this.qs.put("cursor", this.cursor);
	    }
	    
	    Query query = this.dataClient.queryEntitiesRequest("GET", this.qs, null,
                this.dataClient.getOrganizationId(),  this.dataClient.getApplicationId(), this.type);
	    ApiResponse response = query.getResponse();
	    if (response.getError() != null) {
	    	this.dataClient.writeLog("Error getting collection.");
	    } else {
	    	String theCursor = response.getCursor();
    		int count = response.getEntityCount();
    		
    		UUID nextUUID = response.getNext();
    		if( nextUUID != null ) {
    			this.next = nextUUID.toString();
    		} else {
    			this.next = null;
    		}
    		this.cursor = theCursor;

	    	this.saveCursor(theCursor);
	    	if ( count > 0 ) {
	    		this.resetEntityPointer();
	    		this.list = new ArrayList<Entity>();
	    		List<Entity> retrievedEntities = response.getEntities();
	    		
	    		for ( Entity retrievedEntity : retrievedEntities ) {
	    			if( retrievedEntity.getUuid() != null ) {
	    				retrievedEntity.setType(this.type);
	    				this.list.add(retrievedEntity);
	    			}
	    		}
	    	}
	    }
	    
	    return response;
	}

	/**
	 * Adds an entity to the Collection object.
	 *
	 * @param  entityData  a Map object of entity properties to be saved in the entity
	 * @return  an Entity object that represents the newly added entity. Must include
	 *		a 'type' property. Executes synchronously.
	 */	
	public Entity addEntity(Map<String,Object> entityData) {
		Entity entity = null;		
		ApiResponse response = this.dataClient.createEntity(entityData);
		if( (response != null) && (response.getError() == null) && (response.getEntityCount() > 0) ) {
			entity = response.getFirstEntity();
			if (entity != null) {
				this.list.add(entity);
			}
		}
		return entity;
	}

	/**
	 * Deletes the provided entity on the server, then updates the
	 * Collection object by calling fetch(). Executes synchronously.
	 *
	 * @param entity an Entity object that contains a 'type' and 'uuid' property
	 */	
	public ApiResponse destroyEntity(Entity entity) {
		ApiResponse response = entity.destroy();
		if (response.getError() != null) {
			this.dataClient.writeLog("Could not destroy entity.");
		} else {
			response = this.fetch();
		}
	    
		return response;
	}

	/**
	 * Retrieves an entity from the server.
	 *
	 * @param uuid the UUID of the entity to retrieve
	 * @return an ApiResponse object
	 */	
	public ApiResponse getEntityByUuid(UUID uuid) {
		Entity entity = new Entity(this.dataClient);
	    entity.setType(this.type);
	    entity.setUuid(uuid);
	    return entity.fetch();
	}

    /**
     * Gets the list of entities in the Collection object.
     *
     * @return the list of entities
     */
    public ArrayList<Entity> getList() {
        return this.list;
    }

	/**
	 * Gets the first entity in the Collection object.
	 *
	 * @return  an Entity object
	 */	
	public Entity getFirstEntity() {
		return ((this.list.size() > 0) ? this.list.get(0) : null);
	}

	/**
	 * Gets the last entity in the Collection object.
	 *
	 * @return  an Entity object
	 */	
	public Entity getLastEntity() {
		return ((this.list.size() > 0) ? this.list.get(this.list.size()-1) : null);
	}

	/**
	 * Checks if there is another entity in the Collection after the current pointer position.
{'fraction_non_alphanumeric': 0.11183129450429068, 'fraction_numerical': 0.0012780719371918933, 'mean_word_length': 2.918097281831187, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7152340', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3275, 'n_words': 964}