functionalj.tuple.Tuple Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.tuple;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import functionalj.lens.core.LensSpec;
import functionalj.lens.lenses.AnyLens;
import functionalj.lens.lenses.ObjectLens;
public class Tuple {
public static ImmutableTuple2 of(T1 _1, T2 _2) {
return tuple2(_1, _2);
}
public static ImmutableTuple2 of(Map.Entry extends T1, ? extends T2> entry) {
return tuple2(entry);
}
public static ImmutableTuple3 of(T1 _1, T2 _2, T3 _3) {
return tuple3(_1, _2, _3);
}
public static ImmutableTuple4 of(T1 _1, T2 _2, T3 _3, T4 _4) {
return tuple4(_1, _2, _3, _4);
}
public static ImmutableTuple5 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5) {
return tuple5(_1, _2, _3, _4, _5);
}
public static ImmutableTuple6 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6) {
return tuple6(_1, _2, _3, _4, _5, _6);
}
public static ImmutableTuple2 tuple2(T1 _1, T2 _2) {
return new ImmutableTuple2<>(_1, _2);
}
public static ImmutableTuple2 tuple2(Map.Entry extends T1, ? extends T2> entry) {
if (entry == null)
return new ImmutableTuple2<>(null, null);
return new ImmutableTuple2<>(entry);
}
public static ImmutableTuple3 tuple3(T1 _1, T2 _2, T3 _3) {
return new ImmutableTuple3<>(_1, _2, _3);
}
public static ImmutableTuple4 tuple4(T1 _1, T2 _2, T3 _3, T4 _4) {
return new ImmutableTuple4<>(_1, _2, _3, _4);
}
public static ImmutableTuple5 tuple5(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5) {
return new ImmutableTuple5<>(_1, _2, _3, _4, _5);
}
public static ImmutableTuple6 tuple6(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6) {
return new ImmutableTuple6<>(_1, _2, _3, _4, _5, _6);
}
// == Use ==
public static T1 _1(Tuple2 t) { return t._1(); }
public static T2 _2(Tuple2 t) { return t._2(); }
public static T1 _1(Tuple3 t) { return t._1(); }
public static T2 _2(Tuple3 t) { return t._2(); }
public static T3 _3(Tuple3 t) { return t._3(); }
public static T1 _1(Tuple4 t) { return t._1(); }
public static T2 _2(Tuple4 t) { return t._2(); }
public static T3 _3(Tuple4 t) { return t._3(); }
public static T4 _4(Tuple4 t) { return t._4(); }
public static T1 _1(Tuple5 t) { return t._1(); }
public static T2 _2(Tuple5 t) { return t._2(); }
public static T3 _3(Tuple5 t) { return t._3(); }
public static T4 _4(Tuple5 t) { return t._4(); }
public static T5 _5(Tuple5 t) { return t._5(); }
public static T1 _1(Tuple6 t) { return t._1(); }
public static T2 _2(Tuple6 t) { return t._2(); }
public static T3 _3(Tuple6 t) { return t._3(); }
public static T4 _4(Tuple6 t) { return t._4(); }
public static T5 _5(Tuple6 t) { return t._5(); }
public static T6 _6(Tuple6 t) { return t._6(); }
// == Access & Lens ==
public static IntTuple2Lens, Object, ObjectLens, Object>>
theIntTuple2 = createTheTuple(ObjectLens::of);
public static , T2>>
IntTuple2Lens, T2, T2LENS> createTheTuple(
Function, T2>, T2LENS> T2LensCreator) {
return IntTuple2Lens.of(t -> t, (t, newT) -> newT, T2LensCreator);
}
//== toString ==
public static String toString(Tuple2 tuple) {
if (tuple == null)
return null;
return "(" + tuple._1() + "," + tuple._2() + ")";
}
public static String toString(Tuple3 tuple) {
if (tuple == null)
return null;
return "(" + tuple._1() + "," + tuple._2() + "," + tuple._3() + ")";
}
public static String toString(Tuple4 tuple) {
if (tuple == null)
return null;
return "(" + tuple._1() + "," + tuple._2() + "," + tuple._3() + "," + tuple._4() + ")";
}
public static String toString(Tuple5 tuple) {
if (tuple == null)
return null;
return "(" + tuple._1() + "," + tuple._2() + "," + tuple._3() + "," + tuple._4() + "," + tuple._5() + ")";
}
public static String toString(Tuple6 tuple) {
if (tuple == null)
return null;
return "(" + tuple._1() + "," + tuple._2() + "," + tuple._3() + "," + tuple._4() + "," + tuple._5() + "," + tuple._6() + ")";
}
//== hashCode ==
public static int hashCode(Tuple2 tuple) {
if (tuple == null)
return 0;
final int prime = 31;
int result = 1;
result = prime * result + ((tuple._1() == null) ? 0 : tuple._1().hashCode());
result = prime * result + ((tuple._2() == null) ? 0 : tuple._2().hashCode());
return result;
}
public static int hashCode(Tuple3 tuple) {
if (tuple == null)
return 0;
final int prime = 31;
int result = 1;
result = prime * result + ((tuple._1() == null) ? 0 : tuple._1().hashCode());
result = prime * result + ((tuple._2() == null) ? 0 : tuple._2().hashCode());
result = prime * result + ((tuple._3() == null) ? 0 : tuple._3().hashCode());
return result;
}
public static int hashCode(Tuple4 tuple) {
if (tuple == null)
return 0;
final int prime = 31;
int result = 1;
result = prime * result + ((tuple._1() == null) ? 0 : tuple._1().hashCode());
result = prime * result + ((tuple._2() == null) ? 0 : tuple._2().hashCode());
result = prime * result + ((tuple._3() == null) ? 0 : tuple._3().hashCode());
result = prime * result + ((tuple._4() == null) ? 0 : tuple._4().hashCode());
return result;
}
public static int hashCode(Tuple5 tuple) {
if (tuple == null)
return 0;
final int prime = 31;
int result = 1;
result = prime * result + ((tuple._1() == null) ? 0 : tuple._1().hashCode());
result = prime * result + ((tuple._2() == null) ? 0 : tuple._2().hashCode());
result = prime * result + ((tuple._3() == null) ? 0 : tuple._3().hashCode());
result = prime * result + ((tuple._4() == null) ? 0 : tuple._4().hashCode());
result = prime * result + ((tuple._5() == null) ? 0 : tuple._5().hashCode());
return result;
}
public static int hashCode(Tuple6 tuple) {
if (tuple == null)
return 0;
final int prime = 31;
int result = 1;
result = prime * result + ((tuple._1() == null) ? 0 : tuple._1().hashCode());
result = prime * result + ((tuple._2() == null) ? 0 : tuple._2().hashCode());
result = prime * result + ((tuple._3() == null) ? 0 : tuple._3().hashCode());
result = prime * result + ((tuple._4() == null) ? 0 : tuple._4().hashCode());
result = prime * result + ((tuple._5() == null) ? 0 : tuple._5().hashCode());
result = prime * result + ((tuple._6() == null) ? 0 : tuple._6().hashCode());
return result;
}
//== equals ==
public static boolean equals(Tuple2 tuple, Object obj) {
if (tuple == null)
return (obj == null);
if (tuple == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Tuple2)
return false;
@SuppressWarnings("rawtypes")
Tuple2 other = (Tuple2) obj;
if (!Objects.equals(tuple._1(), other._1())) return false;
if (!Objects.equals(tuple._2(), other._2())) return false;
return true;
}
public static boolean equals(Tuple3 tuple, Object obj) {
if (tuple == null)
return (obj == null);
if (tuple == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Tuple3)
return false;
@SuppressWarnings("rawtypes")
Tuple3 other = (Tuple3) obj;
if (!Objects.equals(tuple._1(), other._1())) return false;
if (!Objects.equals(tuple._2(), other._2())) return false;
if (!Objects.equals(tuple._3(), other._3())) return false;
return true;
}
public static boolean equals(Tuple4 tuple, Object obj) {
if (tuple == null)
return (obj == null);
if (tuple == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Tuple4)
return false;
@SuppressWarnings("rawtypes")
Tuple4 other = (Tuple4) obj;
if (!Objects.equals(tuple._1(), other._1())) return false;
if (!Objects.equals(tuple._2(), other._2())) return false;
if (!Objects.equals(tuple._3(), other._3())) return false;
if (!Objects.equals(tuple._4(), other._4())) return false;
return true;
}
public static boolean equals(Tuple5 tuple, Object obj) {
if (tuple == null)
return (obj == null);
if (tuple == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Tuple5)
return false;
@SuppressWarnings("rawtypes")
Tuple5 other = (Tuple5) obj;
if (!Objects.equals(tuple._1(), other._1())) return false;
if (!Objects.equals(tuple._2(), other._2())) return false;
if (!Objects.equals(tuple._3(), other._3())) return false;
if (!Objects.equals(tuple._4(), other._4())) return false;
if (!Objects.equals(tuple._5(), other._5())) return false;
return true;
}
public static boolean equals(Tuple6 tuple, Object obj) {
if (tuple == null)
return (obj == null);
if (tuple == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Tuple6)
return false;
@SuppressWarnings("rawtypes")
Tuple6 other = (Tuple6) obj;
if (!Objects.equals(tuple._1(), other._1())) return false;
if (!Objects.equals(tuple._2(), other._2())) return false;
if (!Objects.equals(tuple._3(), other._3())) return false;
if (!Objects.equals(tuple._4(), other._4())) return false;
if (!Objects.equals(tuple._5(), other._5())) return false;
if (!Objects.equals(tuple._6(), other._6())) return false;
return true;
}
}