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

com.wantedtech.common.xpresso.comprehension.AbstractTupleComprehension Maven / Gradle / Ivy

The newest version!
package com.wantedtech.common.xpresso.comprehension;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.wantedtech.common.xpresso.x;
import com.wantedtech.common.xpresso.functional.Function;
import com.wantedtech.common.xpresso.functional.Predicate;
import com.wantedtech.common.xpresso.helpers.Helpers;
import com.wantedtech.common.xpresso.types.tuple;

public abstract class AbstractTupleComprehension implements Iterable{

	boolean before_for = true;
	List> if_functions = Helpers.newArrayList();
	List> else_functions = Helpers.newArrayList();
	Predicate if_predicate = x.TRUE;
	ArrayList original_elements = new ArrayList();
	ArrayList elements = new ArrayList();
	ArrayList transformed_elements = new ArrayList();
	
	List outputFieldNames = Helpers.newArrayList();
	List elementFieldNames = Helpers.newArrayList();

	public void when(Predicate predicate){
		if(before_for){
			if_predicate = predicate;
		}else{
			ArrayList filtered_elements = new ArrayList(); 
			for(tuple idx__element : x.enumerate(original_elements)){
				int index = (int)idx__element.get(0);
				Object element = idx__element.get(1);
				if(predicate.apply(element)){
					filtered_elements.add(transformed_elements.get(index));
				}
				
			}
			elements = filtered_elements;
		}
	}
	
	
	public void unless(Predicate predicate){
		when(x.NOT(predicate));
	}
	/*public void ifElementNot(Predicate predicate){
		when(x.NOT(predicate));
	}*/
		
	public Iterator iterator() {
		return elements.iterator();
	}
	
	public String toString() {
		StringBuilder result = new StringBuilder();
		for(tuple item: this){
			result.append(item.toString()+", ");
		}
		return result.toString();
	}
}