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

com.aol.cyclops.lambda.tuple.PTuple8 Maven / Gradle / Ivy

The newest version!
package com.aol.cyclops.lambda.tuple;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.function.Function;

import com.aol.cyclops.functions.OctFunction;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap1PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap2PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap3PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap4PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap5PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap6PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap7PTuple8;
import com.aol.cyclops.lambda.tuple.lazymap.LazyMap8PTuple8;

public interface PTuple8 extends PTuple7 {
	
	default T8 v8(){
		if(arity()<8)
			throw new ClassCastException("Attempt to upscale to " + PTuple8.class.getCanonicalName() + " from com.aol.cyclops.lambda.tuple.Tuple"+arity());
		return (T8)getCachedValues().get(7);
	}
	default T8 _8(){
		return v8();
	}

	default T8 getT8(){
		return v8();
	}
	default int arity(){
		return 8;
	}
	default  R apply8(Function>>>>>>>  fn){
		return fn.apply(v1()).apply(v2()).apply(v3()).apply(v4()).apply(v5()).apply(v6()).apply(v7()).apply(v8());
	}
	
	default  CompletableFuture applyAsync8(Function>>>>>>>  fn){
		return CompletableFuture.completedFuture(v8())
				.thenApplyAsync(fn.apply(v1()).apply(v2()).apply(v3()).apply(v4()).apply(v5()).apply(v6()).apply(v7()));
	}
	default  R call(OctFunction fn){
		return fn.apply(v1(),v2(),v3(),v4(),v5(),v6(),v7(),v8());
	}
	default  CompletableFuture  callAsync(OctFunction fn){
		return CompletableFuture.completedFuture(this).thenApplyAsync(i->fn.apply(i.v1(), 
				i.v2(),i.v3(),i.v4(),i.v5(),i.v6(),i.v7(),i.v8()));
	}
	
	default  CompletableFuture  callAsync(OctFunction fn, Executor e){
		return CompletableFuture.completedFuture(this).thenApplyAsync(i->fn.apply(i.v1(), 
				i.v2(),i.v3(),i.v4(),i.v5(),i.v6(),i.v7(),i.v8()),e);
	}
	default  CompletableFuture applyAsync8(Function>>>>>>>  fn, Executor e){
		return CompletableFuture.completedFuture(v8())
				.thenApplyAsync(fn.apply(v1()).apply(v2()).apply(v3()).apply(v4()).apply(v5()).apply(v6()).apply(v7()),e);
	}
	
	default PTuple7 tuple7(){
		return (PTuple7)withArity(7);
	}
	default PTuple8 swap8(){
		return of(v8(),v7(),v6(),v5(),v4(),v3(),v2(),v1());
	}
	

	
	
	/**Strict mapping of the first element
	 * 
	 * @param fn Mapping function
	 * @return Tuple8
	 */
	default  PTuple8 map1(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map1(fn);
		else
			return PowerTuples.tuple(fn.apply(v1()),v2(),v3(),v4(),v5(),v6(),v7(),v8());
	}
	/**
	 * Lazily Map 1st element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap1(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap1(fn);
		
		return new LazyMap1PTuple8(fn,this);
		
	}
	/**
	 * Lazily Map 2nd element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap2(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap2(fn);
		
		
		return new LazyMap2PTuple8(fn,(PTuple8)this);
		
	}
	
	/** Map the second element in this Tuple
	 * @param fn mapper function
	 * @return new Tuple3
	 */
	default  PTuple8 map2(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map2(fn);
		else
			return of(v1(),fn.apply(v2()),v3(),v4(),v5(),v6(),v7(),v8());
	}
	/**
	 * Lazily Map 3rd element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap3(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap3(fn);
		
	
		return new LazyMap3PTuple8(fn,(PTuple8)this);
		
	}
	
	/* 
	 * @see com.aol.cyclops.lambda.tuple.Tuple4#map3(java.util.function.Function)
	 */
	default  PTuple8 map3(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map3(fn);
		
		return of(v1(),v2(),fn.apply(v3()),v4(),v5(),v6(),v7(),v8());
	}
	/**
	 * Lazily Map 4th element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap4(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap4(fn);
		
		
		return new LazyMap4PTuple8(fn,(PTuple8)this);
		
	}
	/* 
	 * Map element 4
	 * @see com.aol.cyclops.lambda.tuple.Tuple4#map4(java.util.function.Function)
	 */
	default  PTuple8 map4(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map4(fn);
		
		return of(v1(),v2(),v3(),fn.apply(v4()),v5(),v6(),v7(),v8());
	}
	/*
	 * Lazily Map 5th element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap5(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap5(fn);

		return new LazyMap5PTuple8(fn,(PTuple8)this);
		
	}
	/**
	 * Map the 5th element in a tuple to a different value
	 * 
	 * @param fn Mapper function
	 * @return new Tuple5
	 */
	default  PTuple8 map5(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map5(fn);
		
		return of(v1(),v2(),v3(),v4(),fn.apply(v5()),v6(),v7(),v8());
	}
	/*
	 * Lazily Map 6th element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap6(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap6(fn);


		return new LazyMap6PTuple8(fn,(PTuple8)this);
	}
	/**
	 * 
	 * Map the 6th element in a tuple to a different value
	 * 
	 * @param fn Mapper function
	 * @return new Tuple8
	 */
	default  PTuple8 map6(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map6(fn);
		
		return of(v1(),v2(),v3(),v4(),v5(),fn.apply(v6()),v7(),v8());
	}
	/*
	 * Lazily Map 7th element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap7(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.lazyMap7(fn);
		
		
		return new LazyMap7PTuple8(fn,(PTuple8)this);
		
	}
	
	/**
	 * 
	 * Map the 7th element in a tuple to a different value
	 * 
	 * @param fn Mapper function
	 * @return new Tuple8
	 */
	default  PTuple8 map7(Function fn){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.map7(fn);
		
		return of(v1(),v2(),v3(),v4(),v5(),v6(),fn.apply(v7()),v8());
	}
	/*
	 * Lazily Map 8th element and memoise the result
	 * @param fn Map function
	 * @return
	 */
	default  PTuple8 lazyMap8(Function fn){
		
		
		return new LazyMap8PTuple8(fn,(PTuple8)this);
	}
		
	/**
	 * 
	 * Map the 8th element in a tuple to a different value
	 * 
	 * @param fn Mapper function
	 * @return new Tuple8
	 */
	default  PTuple8 map8(Function fn){
		return of(v1(),v2(),v3(),v4(),v5(),v6(),v7(),fn.apply(v8()));
	}
	/**
	 * Lazily reorder a PTuple8
	 * 
	 * @param v1S Function that determines new first element
	 * @param v2S Function that determines new second element
	 * @param v3S Function that determines new third element
	 * @param v4S Function that determines new fourth element
	 * @param v5S Function that determines new fifth element
	 * @param v6S Function that determines new sixth element
	 * @param v7S Function that determines new seventh element
	 * @param v8S Function that determines new eighth element
	 * @return reordered PTuple8
	 */
	default  PTuple8 reorder(
			Function, NT1> v1S,
			Function, NT2> v2S,
			Function, NT3> v3S,
			Function, NT4> v4S,
			Function, NT5> v5S,
			Function, NT6> v6S,
			Function, NT7> v7S,
			Function, NT8> v8S) {

		PTuple8 host =  this;
		return new TupleImpl(Arrays.asList(), 5) {
			public NT1 v1() {
				return v1S.apply(host);
			}

			public NT2 v2() {
				return v2S.apply(host);
			}

			public NT3 v3() {
				return v3S.apply(host);
			}

			public NT4 v4() {
				return v4S.apply(host);
			}
			public NT5 v5() {
				return v5S.apply(host);
			}

			public NT6 v6() {
				return v6S.apply(host);
			}
			public NT7 v7() {
				return v7S.apply(host);
			}

			public NT8 v8() {
				return v8S.apply(host);
			}

			@Override
			public List getCachedValues() {
				return Arrays.asList(v1(), v2(), v3(), v4(),v5(),v6(),v7(),v8());
			}

			@Override
			public Iterator iterator() {
				return getCachedValues().iterator();
			}

		};

	}
	default PTuple8 memo(){
		if(arity()!=8)
			return (PTuple8)PTuple7.super.memo();
		PTuple8 host =  this;
		Map values = new ConcurrentHashMap<>();
		
		return new TupleImpl(Arrays.asList(),8){
			
			
			public T1 v1(){
				return ( T1)values.computeIfAbsent(new Integer(0), key -> host.v1());
			}

			public T2 v2(){
				return ( T2)values.computeIfAbsent(new Integer(1), key -> host.v2());
			}

			public T3 v3(){
				return ( T3)values.computeIfAbsent(new Integer(2), key -> host.v3());
			}

			public T4 v4(){
				return ( T4)values.computeIfAbsent(new Integer(3), key -> host.v4());
			}

			public T5 v5(){
				return ( T5)values.computeIfAbsent(new Integer(4), key -> host.v5());
			}

			public T6 v6(){
				return ( T6)values.computeIfAbsent(new Integer(5), key -> host.v6());
			}

			public T7 v7(){
				return ( T7)values.computeIfAbsent(new Integer(6), key -> host.v7());
			}

			public T8 v8(){
				return ( T8)values.computeIfAbsent(new Integer(7), key -> host.v8());
			}


			
			@Override
			public List getCachedValues() {
				return Arrays.asList(v1(),v2(),v3(),v4(),v5(),v6(),v7(),v8());
			}

			@Override
			public Iterator iterator() {
				return getCachedValues().iterator();
			}

			
		};
		
	}
	
	public static  PTuple8 ofTuple(Object tuple8){
		return (PTuple8)new TupleImpl(tuple8,8);
	}
	public static  PTuple8 of(T1 t1, T2 t2,T3 t3,T4 t4,T5 t5,
																		T6 t6, T7 t7,T8 t8){
		return (PTuple8)new TupleImpl(Arrays.asList(t1,t2,t3,t4,t5,t6,t7,t8),8);
	}
}