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

structures.lazy.LazyArrayList Maven / Gradle / Ivy

Go to download

Extension of classic Java data structures that avoid the use of Stream abstraction in favour of direct list transformation methods

The newest version!
package structures.lazy;

import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Vector;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

import factory.ArrayListFactory;
import factory.IOCollectionFactory;
import functions.ConsumerIndexed;
import functions.Function2;
import operations.IOCollection;
import operations.OperationImpl;
import structures.ArrayList;
import structures.Collection;
import structures.List;
import utils.Pair;

public class LazyArrayList extends java.util.ArrayList implements LazyList {

	private LazyExecuter lazyExecuter = new LazyExecuter<>();
	private LazyArrayList inner;
	private LazyActionManager lazyActionManager = new LazyActionManager<>(new ArrayListFactory());

	private  LazyArrayList(LazyArrayList lazyArrayList) {
		this.inner = lazyArrayList;
	}
	
	public LazyArrayList() {
	}
	
	@Override
	public  ArrayList executeActions(){
		if(lazyActionManager.numActions() == 0 && inner == null){
			ArrayList thisAsStrict = new ArrayList();
			for(T value : this){
				thisAsStrict.add(value);
			}
			return (ArrayList) thisAsStrict;
		}
		ArrayList inputList = null;
		if(inner != null){
			inputList = inner.executeActions();
		}
		else {
			inputList = (ArrayList) lazyActionManager.getInput();
			for(T value : this){
				inputList.add(value);
			}
		}
		lazyActionManager.setInput(inputList);
		return (ArrayList) lazyExecuter.executeTasks(lazyActionManager);
	}

	private OperationImpl getOperationList(){
		return lazyActionManager.getOperationList();
	}
	

	@Override
	public boolean all(Predicate predicate) {
		ArrayList strictList = executeActions();
		return strictList.all(predicate);
	}

	@Override
	public boolean any(Predicate predicate) {
		ArrayList strictList = executeActions();
		return strictList.any(predicate);
	}

	@Override
	public int count(Predicate fCounter) {
		ArrayList strictList = executeActions();
		return strictList.count(fCounter);
	}

	@Override
	public LazyArrayList distinct() {
		lazyActionManager.addAction(() -> getOperationList().distinct());
		return this;
	}
	
	@Override
	public LazyArrayList filter(Predicate predicate) {
		lazyActionManager.addAction(() -> getOperationList().filter(predicate));
		return this;
	}

	@Override
	public LazyArrayList filterIndexed(BiPredicate predicate) {
		lazyActionManager.addAction(() -> getOperationList().filterIndexed(predicate));
		return this;
	}

	@Override
	public LazyArrayList filterNotNull() {
		lazyActionManager.addAction(() -> getOperationList().filterNotNull());
		return this;
	}

	@Override
	public T first() {
		ArrayList strictList = executeActions();
		return strictList.first();
	}

	@Override
	public T first(T defaultValue) {
		ArrayList strictList = executeActions();
		return strictList.first(defaultValue);
	}

	@Override
	public T first(Predicate predicate, T defaultValue) {
		ArrayList strictList = executeActions();
		return strictList.first(predicate, defaultValue);
	}

	@Override
	public T firstOrNull() {
		ArrayList strictList = executeActions();
		return strictList.firstOrNull();
	}

	@Override
	public T firstOrNull(Predicate predicate) {
		ArrayList strictList = executeActions();
		return strictList.firstOrNull(predicate);
	}
	

	@Override
	public void forEach(Consumer consumer) {
		lazyActionManager.addAction(() -> getOperationList().forEach(consumer));
	}
	
	@Override
	public void forEachIndexed(ConsumerIndexed consumerI) {
		lazyActionManager.addAction(() -> getOperationList().forEachIndexed(consumerI));
		
	}

	@Override
	public void forEachReverse(Consumer consumer) {
		lazyActionManager.addAction(() -> getOperationList().forEachReverse(consumer));
		
	}


	@Override
	public  Map> groupBy(Function thisFuct) {
		ArrayList strictList = executeActions();
		return strictList.groupBy(thisFuct);
	}

	@Override
	public LazyArrayList intersection(java.util.Collection collection) {
		lazyActionManager.addAction(() -> getOperationList().intersection(collection));
		return this;
	}

	@Override
	public T last(T defaultValue) {
		ArrayList strictList = executeActions();
		return strictList.last(defaultValue);
	}

	@Override
	public T last() {
		ArrayList strictList = executeActions();
		return strictList.last();
	}

	@Override
	public T last(Predicate predicate, T defaultValue) {
		ArrayList strictList = executeActions();
		return strictList.last(predicate, defaultValue);
	}

	@Override
	public T lastOrNull() {
		ArrayList strictList = executeActions();
		return strictList.lastOrNull();
	}

	@Override
	public T lastOrNull(Predicate predicate) {
		ArrayList strictList = executeActions();
		return strictList.lastOrNull(predicate);
	}
	
	@Override
	public  LazyArrayList map(Function mapper) {
		LazyArrayList mapOutput = new LazyArrayList(this);
		lazyActionManager.addTrasformingTypeAction(() -> getOperationList().map(mapper));
		return mapOutput;
	}	

	@Override
	public  LazyArrayList mapIndexed(BiFunction mapper) {
		lazyActionManager.addTrasformingTypeAction(() -> getOperationList().mapIndexed(mapper));
		return new LazyArrayList<>(this);
	}

	@Override
	public T maxBy(Comparator comparator) {
		ArrayList strictList = executeActions();
		return strictList.maxBy(comparator);
	}

	@Override
	public T minBy(Comparator comparator) {
		ArrayList strictList = executeActions();
		return strictList.minBy(comparator);
	}

	@Override
	public LazyArrayList orderBy(Comparator comparator) {
		lazyActionManager.addAction(() -> getOperationList().orderBy(comparator));
		return this;
	}

	@Override
	public LazyArrayList orderDecreasingBy(Comparator comparator) {
		lazyActionManager.addAction(() -> getOperationList().orderDecreasingBy(comparator));
		return this;
	}
	

	@Override
	public T reduce(BinaryOperator accumulator) {
		ArrayList strictList = executeActions();
		return strictList.reduce(accumulator);
	}

	@Override
	public T reduceReverse(BinaryOperator accumulator) {
		ArrayList strictList = executeActions();
		return strictList.reduceReverse(accumulator);
	}

	@Override
	public LazyArrayList reverse() {
		lazyActionManager.addAction(() -> getOperationList().reverse());
		return this;
	}

	@Override
	public LazyArrayList take(int n) {
		lazyActionManager.addAction(() -> getOperationList().take(n));
		return this;
	}

	@Override
	public LazyArrayList takeLast(int n) {
		lazyActionManager.addAction(() -> getOperationList().takeLast(n));
		return this;
	}

	@Override
	public LazyArrayList union(java.util.Collection collection) {
		lazyActionManager.addAction(() -> getOperationList().union(collection));
		return this;
	}

	@Override
	public LazyArrayList> zipIndexed() {
		lazyActionManager.addTrasformingTypeAction(() -> getOperationList().zipIndexed());
		return new LazyArrayList<>(this);
	}

	@Override
	public  LazyArrayList> zipWith(java.util.Collection other) {
		lazyActionManager.addTrasformingTypeAction(() -> getOperationList().zipWith(other));
		return new LazyArrayList<>(this);
	}

}