Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
eu.lunisolar.magma.func.tuple.LObjBytePair Maven / Gradle / Ivy
/*
* 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 LObjByteConsumer.
*/
@SuppressWarnings("UnusedDeclaration")
public interface LObjBytePair extends LTuple, LSingle {
int SIZE = 2;
T first();
default T value() {
return first();
}
byte second();
default Object get(int index) {
switch (index) {
case 1 :
return first();
case 2 :
return second();
default :
throw new NoSuchElementException();
}
}
/** Tuple size */
default int size() {
return SIZE;
}
/** Static hashCode() implementation method that takes same arguments as fields of the LObjBytePair and calculates hash from it. */
static int argHashCode(T a1, byte a2) {
final int prime = 31;
int result = 1;
result = prime * result + ((a1 == null) ? 0 : a1.hashCode());
result = prime * result + Byte.hashCode(a2);
return result;
}
/** Static equals() implementation that takes same arguments (doubled) as fields of the LObjBytePair and checks if all values are equal. */
static boolean argEquals(T a1, byte a2, T b1, byte b2) {
return Null.equals(a1, b1) && //
a2 == b2; //
}
/**
* 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(LObjBytePair the, Object that) {
return Null.equals(the, that, (one, two) -> {
// Intentionally all implementations of LObjBytePair are allowed.
if (!(two instanceof LObjBytePair)) {
return false;
}
LObjBytePair other = (LObjBytePair) two;
return argEquals(one.first(), one.second(), other.first(), other.second());
});
}
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 ComparableObjBytePair> extends LObjBytePair, Comparable> {
@Override
default int compareTo(LObjBytePair that) {
return Null.compare(this, that, (one, two) -> {
int retval = 0;
return (retval = Null.compare(one.first(), two.first())) != 0 ? retval : //
(retval = Byte.compare(one.second(), two.second())) != 0 ? retval : 0; //
});
}
}
abstract class AbstractObjBytePair implements LObjBytePair {
@Override
public boolean equals(Object that) {
return LObjBytePair.argEquals(this, that);
}
@Override
public int hashCode() {
return LObjBytePair.argHashCode(first(), second());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('(');
sb.append(first());
sb.append(',');
sb.append(second());
sb.append(')');
return sb.toString();
}
}
/**
* Mutable, non-comparable tuple.
*/
final class MutObjBytePair extends AbstractObjBytePair {
private T first;
private byte second;
public MutObjBytePair(T a1, byte a2) {
this.first = a1;
this.second = a2;
}
public static MutObjBytePair of(T a1, byte a2) {
return new MutObjBytePair(a1, a2);
}
public static MutObjBytePair copyOf(LObjBytePair tuple) {
return of(tuple.first(), tuple.second());
}
public T first() {
return first;
}
public MutObjBytePair first(T first) {
this.first = first;
return this;
}
public byte second() {
return second;
}
public MutObjBytePair second(byte second) {
this.second = second;
return this;
}
public MutObjBytePair setFirst(T first) {
this.first = first;
return this;
}
/** Sets value if predicate(newValue) OR newValue::predicate is true */
public MutObjBytePair setFirstIfArg(T 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 MutObjBytePair 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 MutObjBytePair setFirstIf(LPredicate predicate, T 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 MutObjBytePair setFirstIf(T 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 MutObjBytePair setFirstIf(LBiPredicate predicate, T first) {
if (predicate.test(this.first, first)) {
this.first = first;
}
return this;
}
public MutObjBytePair setSecond(byte second) {
this.second = second;
return this;
}
/** Sets value if predicate(newValue) OR newValue::predicate is true */
public MutObjBytePair setSecondIfArg(byte second, LBytePredicate predicate) {
if (predicate.test(second)) {
this.second = second;
}
return this;
}
/** Sets value derived from non-null argument, only if argument is not null. */
public MutObjBytePair setSecondIfArgNotNull(R arg, LToByteFunction func) {
if (arg != null) {
this.second = func.applyAsByte(arg);
}
return this;
}
/** Sets value if predicate(current) OR current::predicate is true */
public MutObjBytePair setSecondIf(LBytePredicate predicate, byte 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 MutObjBytePair setSecondIf(byte second, LBiBytePredicate 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 MutObjBytePair setSecondIf(LBiBytePredicate predicate, byte second) {
if (predicate.test(this.second, second)) {
this.second = second;
}
return this;
}
public void reset() {
first = null;
second = (byte) 0;
}
}
/**
* Mutable, comparable tuple.
*/
final class MutCompObjBytePair> extends AbstractObjBytePair implements ComparableObjBytePair {
private T first;
private byte second;
public MutCompObjBytePair(T a1, byte a2) {
this.first = a1;
this.second = a2;
}
public static > MutCompObjBytePair of(T a1, byte a2) {
return new MutCompObjBytePair(a1, a2);
}
public static > MutCompObjBytePair copyOf(LObjBytePair tuple) {
return of(tuple.first(), tuple.second());
}
public T first() {
return first;
}
public MutCompObjBytePair first(T first) {
this.first = first;
return this;
}
public byte second() {
return second;
}
public MutCompObjBytePair second(byte second) {
this.second = second;
return this;
}
public MutCompObjBytePair setFirst(T first) {
this.first = first;
return this;
}
/** Sets value if predicate(newValue) OR newValue::predicate is true */
public MutCompObjBytePair setFirstIfArg(T 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 MutCompObjBytePair 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 MutCompObjBytePair setFirstIf(LPredicate predicate, T 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 MutCompObjBytePair setFirstIf(T 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 MutCompObjBytePair setFirstIf(LBiPredicate predicate, T first) {
if (predicate.test(this.first, first)) {
this.first = first;
}
return this;
}
public MutCompObjBytePair setSecond(byte second) {
this.second = second;
return this;
}
/** Sets value if predicate(newValue) OR newValue::predicate is true */
public MutCompObjBytePair setSecondIfArg(byte second, LBytePredicate predicate) {
if (predicate.test(second)) {
this.second = second;
}
return this;
}
/** Sets value derived from non-null argument, only if argument is not null. */
public MutCompObjBytePair setSecondIfArgNotNull(R arg, LToByteFunction func) {
if (arg != null) {
this.second = func.applyAsByte(arg);
}
return this;
}
/** Sets value if predicate(current) OR current::predicate is true */
public MutCompObjBytePair setSecondIf(LBytePredicate predicate, byte 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 MutCompObjBytePair setSecondIf(byte second, LBiBytePredicate 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 MutCompObjBytePair setSecondIf(LBiBytePredicate predicate, byte second) {
if (predicate.test(this.second, second)) {
this.second = second;
}
return this;
}
public void reset() {
first = null;
second = (byte) 0;
}
}
/**
* Immutable, non-comparable tuple.
*/
@Immutable
final class ImmObjBytePair extends AbstractObjBytePair {
private final T first;
private final byte second;
public ImmObjBytePair(T a1, byte a2) {
this.first = a1;
this.second = a2;
}
public static ImmObjBytePair of(T a1, byte a2) {
return new ImmObjBytePair(a1, a2);
}
public static ImmObjBytePair copyOf(LObjBytePair tuple) {
return of(tuple.first(), tuple.second());
}
public T first() {
return first;
}
public byte second() {
return second;
}
}
/**
* Immutable, comparable tuple.
*/
@Immutable
final class ImmCompObjBytePair> extends AbstractObjBytePair implements ComparableObjBytePair {
private final T first;
private final byte second;
public ImmCompObjBytePair(T a1, byte a2) {
this.first = a1;
this.second = a2;
}
public static > ImmCompObjBytePair of(T a1, byte a2) {
return new ImmCompObjBytePair(a1, a2);
}
public static > ImmCompObjBytePair copyOf(LObjBytePair tuple) {
return of(tuple.first(), tuple.second());
}
public T first() {
return first;
}
public byte second() {
return second;
}
}
}