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

eu.lunisolar.magma.func.tuple.LObjIntObjTriple Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is part of "lunisolar-magma".
 *
 * (C) Copyright 2014-2019 Lunisolar (http://lunisolar.eu/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.lunisolar.magma.func.tuple;

import eu.lunisolar.magma.basics.meta.LTuple;
import eu.lunisolar.magma.basics.Null;
import eu.lunisolar.magma.basics.fluent.Fluent;
import eu.lunisolar.magma.func.function.LFunction;
import eu.lunisolar.magma.func.function.to.*;
import eu.lunisolar.magma.func.operator.unary.*;
import eu.lunisolar.magma.func.operator.binary.*;
import eu.lunisolar.magma.func.predicate.*;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.*;

/**
 * Exact equivalent of input parameters used in LTieConsumer.
 */
@SuppressWarnings("UnusedDeclaration")
public interface LObjIntObjTriple extends LTuple, LObjIntPair {

	int SIZE = 3;

	T1 first();

	default T1 value() {
		return first();
	}

	int second();

	T2 third();

	default Object get(int index) {
		switch (index) {
			case 1 :
				return first();
			case 2 :
				return second();
			case 3 :
				return third();
			default :
				throw new NoSuchElementException();
		}
	}

	/** Tuple size */
	default int size() {
		return SIZE;
	}

	/** Static hashCode() implementation method that takes same arguments as fields of the LObjIntObjTriple and calculates hash from it. */
	static  int argHashCode(T1 a1, int a2, T2 a3) {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((a1 == null) ? 0 : a1.hashCode());
		result = prime * result + Integer.hashCode(a2);
		result = prime * result + ((a3 == null) ? 0 : a3.hashCode());
		return result;
	}

	/** Static equals() implementation that takes same arguments (doubled) as fields of the LObjIntObjTriple and checks if all values are equal. */
	static  boolean argEquals(T1 a1, int a2, T2 a3, T1 b1, int b2, T2 b3) {
		return Null.equals(a1, b1) && //
				a2 == b2 && //
				Null.equals(a3, b3); //
	}

	/**
	 * Static equals() implementation that takes two tuples asnd checks if they are equal.
	 *
	 * Tuples are considered equal if are implementing same interface and their tuple values are equal regardless of the implementing class.
	 */
	static boolean argEquals(LObjIntObjTriple the, Object that) {
		return Null.equals(the, that, (one, two) -> {
			// Intentionally all implementations of LObjIntObjTriple are allowed.
				if (!(two instanceof LObjIntObjTriple)) {
					return false;
				}

				LObjIntObjTriple other = (LObjIntObjTriple) two;

				return argEquals(one.first(), one.second(), one.third(), other.first(), other.second(), other.third());
			});
	}

	default Iterator iterator() {
		return new Iterator() {

			private int index;

			@Override
			public boolean hasNext() {
				return index < SIZE;
			}

			@Override
			public Object next() {
				index++;
				return get(index);
			}
		};
	}

	interface ComparableObjIntObjTriple, T2 extends Comparable> extends LObjIntObjTriple, Comparable> {

		@Override
		default int compareTo(LObjIntObjTriple that) {
			return Null.compare(this, that, (one, two) -> {
				int retval = 0;

				return (retval = Null.compare(one.first(), two.first())) != 0 ? retval : //
						(retval = Integer.compare(one.second(), two.second())) != 0 ? retval : //
								(retval = Null.compare(one.third(), two.third())) != 0 ? retval : 0; //
				});
		}

	}

	abstract class AbstractObjIntObjTriple implements LObjIntObjTriple {

		@Override
		public boolean equals(Object that) {
			return LObjIntObjTriple.argEquals(this, that);
		}

		@Override
		public int hashCode() {
			return LObjIntObjTriple.argHashCode(first(), second(), third());
		}

		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();
			sb.append('(');
			sb.append(first());
			sb.append(',');
			sb.append(second());
			sb.append(',');
			sb.append(third());
			sb.append(')');
			return sb.toString();
		}

	}

	/**
	 * Mutable, non-comparable tuple.
	 */
	final class MutObjIntObjTriple extends AbstractObjIntObjTriple {

		private T1 first;
		private int second;
		private T2 third;

		public MutObjIntObjTriple(T1 a1, int a2, T2 a3) {
			this.first = a1;
			this.second = a2;
			this.third = a3;
		}

		public static  MutObjIntObjTriple of(T1 a1, int a2, T2 a3) {
			return new MutObjIntObjTriple(a1, a2, a3);
		}

		public static  MutObjIntObjTriple copyOf(LObjIntObjTriple tuple) {
			return of(tuple.first(), tuple.second(), tuple.third());
		}

		public T1 first() {
			return first;
		}

		public MutObjIntObjTriple first(T1 first) {
			this.first = first;
			return this;
		}

		public int second() {
			return second;
		}

		public MutObjIntObjTriple second(int second) {
			this.second = second;
			return this;
		}

		public T2 third() {
			return third;
		}

		public MutObjIntObjTriple third(T2 third) {
			this.third = third;
			return this;
		}

		public MutObjIntObjTriple setFirst(T1 first) {
			this.first = first;
			return this;
		}

		/** Sets value if predicate(newValue) OR newValue::predicate is true */
		public MutObjIntObjTriple setFirstIfArg(T1 first, LPredicate predicate) {
			if (predicate.test(first)) {
				this.first = first;
			}
			return this;
		}

		/** Sets value derived from non-null argument, only if argument is not null. */
		public  MutObjIntObjTriple setFirstIfArgNotNull(R arg, LFunction func) {
			if (arg != null) {
				this.first = func.apply(arg);
			}
			return this;
		}

		/** Sets value if predicate(current) OR current::predicate is true */
		public MutObjIntObjTriple setFirstIf(LPredicate predicate, T1 first) {
			if (predicate.test(this.first)) {
				this.first = first;
			}
			return this;
		}

		/** Sets new value if predicate predicate(newValue, current) OR newValue::something(current) is true. */
		public MutObjIntObjTriple setFirstIf(T1 first, LBiPredicate predicate) {
			// the order of arguments is intentional, to allow predicate:
			if (predicate.test(first, this.first)) {
				this.first = first;
			}
			return this;
		}

		/** Sets new value if predicate predicate(current, newValue) OR current::something(newValue) is true. */
		public MutObjIntObjTriple setFirstIf(LBiPredicate predicate, T1 first) {

			if (predicate.test(this.first, first)) {
				this.first = first;
			}
			return this;
		}

		public MutObjIntObjTriple setSecond(int second) {
			this.second = second;
			return this;
		}

		/** Sets value if predicate(newValue) OR newValue::predicate is true */
		public MutObjIntObjTriple setSecondIfArg(int second, LIntPredicate predicate) {
			if (predicate.test(second)) {
				this.second = second;
			}
			return this;
		}

		/** Sets value derived from non-null argument, only if argument is not null. */
		public  MutObjIntObjTriple setSecondIfArgNotNull(R arg, LToIntFunction func) {
			if (arg != null) {
				this.second = func.applyAsInt(arg);
			}
			return this;
		}

		/** Sets value if predicate(current) OR current::predicate is true */
		public MutObjIntObjTriple setSecondIf(LIntPredicate predicate, int second) {
			if (predicate.test(this.second)) {
				this.second = second;
			}
			return this;
		}

		/** Sets new value if predicate predicate(newValue, current) OR newValue::something(current) is true. */
		public MutObjIntObjTriple setSecondIf(int second, LBiIntPredicate predicate) {
			// the order of arguments is intentional, to allow predicate:
			if (predicate.test(second, this.second)) {
				this.second = second;
			}
			return this;
		}

		/** Sets new value if predicate predicate(current, newValue) OR current::something(newValue) is true. */
		public MutObjIntObjTriple setSecondIf(LBiIntPredicate predicate, int second) {

			if (predicate.test(this.second, second)) {
				this.second = second;
			}
			return this;
		}

		public MutObjIntObjTriple setThird(T2 third) {
			this.third = third;
			return this;
		}

		/** Sets value if predicate(newValue) OR newValue::predicate is true */
		public MutObjIntObjTriple setThirdIfArg(T2 third, LPredicate predicate) {
			if (predicate.test(third)) {
				this.third = third;
			}
			return this;
		}

		/** Sets value derived from non-null argument, only if argument is not null. */
		public  MutObjIntObjTriple setThirdIfArgNotNull(R arg, LFunction func) {
			if (arg != null) {
				this.third = func.apply(arg);
			}
			return this;
		}

		/** Sets value if predicate(current) OR current::predicate is true */
		public MutObjIntObjTriple setThirdIf(LPredicate predicate, T2 third) {
			if (predicate.test(this.third)) {
				this.third = third;
			}
			return this;
		}

		/** Sets new value if predicate predicate(newValue, current) OR newValue::something(current) is true. */
		public MutObjIntObjTriple setThirdIf(T2 third, LBiPredicate predicate) {
			// the order of arguments is intentional, to allow predicate:
			if (predicate.test(third, this.third)) {
				this.third = third;
			}
			return this;
		}

		/** Sets new value if predicate predicate(current, newValue) OR current::something(newValue) is true. */
		public MutObjIntObjTriple setThirdIf(LBiPredicate predicate, T2 third) {

			if (predicate.test(this.third, third)) {
				this.third = third;
			}
			return this;
		}

		public void reset() {
			first = null;
			second = 0;
			third = null;
		}
	}

	/**
	 * Mutable, comparable tuple.
	 */
	final class MutCompObjIntObjTriple, T2 extends Comparable> extends AbstractObjIntObjTriple implements ComparableObjIntObjTriple {

		private T1 first;
		private int second;
		private T2 third;

		public MutCompObjIntObjTriple(T1 a1, int a2, T2 a3) {
			this.first = a1;
			this.second = a2;
			this.third = a3;
		}

		public static , T2 extends Comparable> MutCompObjIntObjTriple of(T1 a1, int a2, T2 a3) {
			return new MutCompObjIntObjTriple(a1, a2, a3);
		}

		public static , T2 extends Comparable> MutCompObjIntObjTriple copyOf(LObjIntObjTriple tuple) {
			return of(tuple.first(), tuple.second(), tuple.third());
		}

		public T1 first() {
			return first;
		}

		public MutCompObjIntObjTriple first(T1 first) {
			this.first = first;
			return this;
		}

		public int second() {
			return second;
		}

		public MutCompObjIntObjTriple second(int second) {
			this.second = second;
			return this;
		}

		public T2 third() {
			return third;
		}

		public MutCompObjIntObjTriple third(T2 third) {
			this.third = third;
			return this;
		}

		public MutCompObjIntObjTriple setFirst(T1 first) {
			this.first = first;
			return this;
		}

		/** Sets value if predicate(newValue) OR newValue::predicate is true */
		public MutCompObjIntObjTriple setFirstIfArg(T1 first, LPredicate predicate) {
			if (predicate.test(first)) {
				this.first = first;
			}
			return this;
		}

		/** Sets value derived from non-null argument, only if argument is not null. */
		public  MutCompObjIntObjTriple setFirstIfArgNotNull(R arg, LFunction func) {
			if (arg != null) {
				this.first = func.apply(arg);
			}
			return this;
		}

		/** Sets value if predicate(current) OR current::predicate is true */
		public MutCompObjIntObjTriple setFirstIf(LPredicate predicate, T1 first) {
			if (predicate.test(this.first)) {
				this.first = first;
			}
			return this;
		}

		/** Sets new value if predicate predicate(newValue, current) OR newValue::something(current) is true. */
		public MutCompObjIntObjTriple setFirstIf(T1 first, LBiPredicate predicate) {
			// the order of arguments is intentional, to allow predicate:
			if (predicate.test(first, this.first)) {
				this.first = first;
			}
			return this;
		}

		/** Sets new value if predicate predicate(current, newValue) OR current::something(newValue) is true. */
		public MutCompObjIntObjTriple setFirstIf(LBiPredicate predicate, T1 first) {

			if (predicate.test(this.first, first)) {
				this.first = first;
			}
			return this;
		}

		public MutCompObjIntObjTriple setSecond(int second) {
			this.second = second;
			return this;
		}

		/** Sets value if predicate(newValue) OR newValue::predicate is true */
		public MutCompObjIntObjTriple setSecondIfArg(int second, LIntPredicate predicate) {
			if (predicate.test(second)) {
				this.second = second;
			}
			return this;
		}

		/** Sets value derived from non-null argument, only if argument is not null. */
		public  MutCompObjIntObjTriple setSecondIfArgNotNull(R arg, LToIntFunction func) {
			if (arg != null) {
				this.second = func.applyAsInt(arg);
			}
			return this;
		}

		/** Sets value if predicate(current) OR current::predicate is true */
		public MutCompObjIntObjTriple setSecondIf(LIntPredicate predicate, int second) {
			if (predicate.test(this.second)) {
				this.second = second;
			}
			return this;
		}

		/** Sets new value if predicate predicate(newValue, current) OR newValue::something(current) is true. */
		public MutCompObjIntObjTriple setSecondIf(int second, LBiIntPredicate predicate) {
			// the order of arguments is intentional, to allow predicate:
			if (predicate.test(second, this.second)) {
				this.second = second;
			}
			return this;
		}

		/** Sets new value if predicate predicate(current, newValue) OR current::something(newValue) is true. */
		public MutCompObjIntObjTriple setSecondIf(LBiIntPredicate predicate, int second) {

			if (predicate.test(this.second, second)) {
				this.second = second;
			}
			return this;
		}

		public MutCompObjIntObjTriple setThird(T2 third) {
			this.third = third;
			return this;
		}

		/** Sets value if predicate(newValue) OR newValue::predicate is true */
		public MutCompObjIntObjTriple setThirdIfArg(T2 third, LPredicate predicate) {
			if (predicate.test(third)) {
				this.third = third;
			}
			return this;
		}

		/** Sets value derived from non-null argument, only if argument is not null. */
		public  MutCompObjIntObjTriple setThirdIfArgNotNull(R arg, LFunction func) {
			if (arg != null) {
				this.third = func.apply(arg);
			}
			return this;
		}

		/** Sets value if predicate(current) OR current::predicate is true */
		public MutCompObjIntObjTriple setThirdIf(LPredicate predicate, T2 third) {
			if (predicate.test(this.third)) {
				this.third = third;
			}
			return this;
		}

		/** Sets new value if predicate predicate(newValue, current) OR newValue::something(current) is true. */
		public MutCompObjIntObjTriple setThirdIf(T2 third, LBiPredicate predicate) {
			// the order of arguments is intentional, to allow predicate:
			if (predicate.test(third, this.third)) {
				this.third = third;
			}
			return this;
		}

		/** Sets new value if predicate predicate(current, newValue) OR current::something(newValue) is true. */
		public MutCompObjIntObjTriple setThirdIf(LBiPredicate predicate, T2 third) {

			if (predicate.test(this.third, third)) {
				this.third = third;
			}
			return this;
		}

		public void reset() {
			first = null;
			second = 0;
			third = null;
		}
	}

	/**
	 * Immutable, non-comparable tuple.
	 */
	@Immutable
	final class ImmObjIntObjTriple extends AbstractObjIntObjTriple {

		private final T1 first;
		private final int second;
		private final T2 third;

		public ImmObjIntObjTriple(T1 a1, int a2, T2 a3) {
			this.first = a1;
			this.second = a2;
			this.third = a3;
		}

		public static  ImmObjIntObjTriple of(T1 a1, int a2, T2 a3) {
			return new ImmObjIntObjTriple(a1, a2, a3);
		}

		public static  ImmObjIntObjTriple copyOf(LObjIntObjTriple tuple) {
			return of(tuple.first(), tuple.second(), tuple.third());
		}

		public T1 first() {
			return first;
		}

		public int second() {
			return second;
		}

		public T2 third() {
			return third;
		}

	}

	/**
	 * Immutable, comparable tuple.
	 */
	@Immutable
	final class ImmCompObjIntObjTriple, T2 extends Comparable> extends AbstractObjIntObjTriple implements ComparableObjIntObjTriple {

		private final T1 first;
		private final int second;
		private final T2 third;

		public ImmCompObjIntObjTriple(T1 a1, int a2, T2 a3) {
			this.first = a1;
			this.second = a2;
			this.third = a3;
		}

		public static , T2 extends Comparable> ImmCompObjIntObjTriple of(T1 a1, int a2, T2 a3) {
			return new ImmCompObjIntObjTriple(a1, a2, a3);
		}

		public static , T2 extends Comparable> ImmCompObjIntObjTriple copyOf(LObjIntObjTriple tuple) {
			return of(tuple.first(), tuple.second(), tuple.third());
		}

		public T1 first() {
			return first;
		}

		public int second() {
			return second;
		}

		public T2 third() {
			return third;
		}

	}

}