com.landawn.abacus.util.Tuple Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2017, Haiyang Li.
*
* 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 com.landawn.abacus.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.stream.Stream;
/**
*
* @author Haiyang Li
* @since 0.8
*/
@com.landawn.abacus.annotation.Immutable
@SuppressWarnings({ "java:S116", "java:S117" })
public abstract class Tuple implements Immutable {
private static final Tuple0 EMPTY = new Tuple0();
Tuple() {
}
/**
*
*
* @return
*/
public abstract int arity();
/**
*
* @return
*/
public abstract boolean anyNull();
/**
*
* @return
*/
public abstract boolean allNull();
/**
*
* @param objToFind
* @return
*/
public abstract boolean contains(final Object objToFind);
/**
*
*
* @return
*/
public abstract Object[] toArray();
/**
*
* @param
* @param a
* @return
*/
public abstract A[] toArray(A[] a);
/**
*
* @param
* @param consumer
* @throws E the e
*/
public abstract void forEach(Throwables.Consumer, E> consumer) throws E;
/**
*
* @param
* @param action
* @throws E the e
*/
public void accept(final Throwables.Consumer super TP, E> action) throws E {
action.accept((TP) this);
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws E the e
*/
public U map(final Throwables.Function super TP, U, E> mapper) throws E {
return mapper.apply((TP) this);
}
/**
*
* @param
* @param predicate
* @return
* @throws E the e
*/
public Optional filter(final Throwables.Predicate super TP, E> predicate) throws E {
return predicate.test((TP) this) ? Optional.of((TP) this) : Optional. empty();
}
/**
*
*
* @return
*/
public Stream stream() {
return Stream.of((TP) this);
}
/**
*
*
* @param
* @param
* @param func
* @return
* @throws E
*/
public Stream stream(final Throwables.Function super TP, Stream, E> func) throws E {
return func.apply((TP) this);
}
/**
*
* @return
*/
public Optional toOptional() {
return Optional.of((TP) this);
}
/**
*
* @param
* @param _1 the 1
* @return
*/
public static Tuple1 of(T1 _1) {
return new Tuple1<>(_1);
}
/**
*
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @return
*/
public static Tuple2 of(T1 _1, T2 _2) {
return new Tuple2<>(_1, _2);
}
/**
*
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @return
*/
public static Tuple3 of(T1 _1, T2 _2, T3 _3) {
return new Tuple3<>(_1, _2, _3);
}
/**
*
* @param
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @param _4 the 4
* @return
*/
public static Tuple4 of(T1 _1, T2 _2, T3 _3, T4 _4) {
return new Tuple4<>(_1, _2, _3, _4);
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @param _4 the 4
* @param _5 the 5
* @return
*/
public static Tuple5 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5) {
return new Tuple5<>(_1, _2, _3, _4, _5);
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @param _4 the 4
* @param _5 the 5
* @param _6 the 6
* @return
*/
public static Tuple6 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6) {
return new Tuple6<>(_1, _2, _3, _4, _5, _6);
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @param _4 the 4
* @param _5 the 5
* @param _6 the 6
* @param _7 the 7
* @return
*/
public static Tuple7 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6, T7 _7) {
return new Tuple7<>(_1, _2, _3, _4, _5, _6, _7);
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @param _4 the 4
* @param _5 the 5
* @param _6 the 6
* @param _7 the 7
* @param _8 the 8
* @return
* @deprecated you should consider using class Some Bean/Record class: MyRecord { final T1 propName1, final T2 propName2...}
*/
@Deprecated
public static Tuple8 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6, T7 _7, T8 _8) {
return new Tuple8<>(_1, _2, _3, _4, _5, _6, _7, _8);
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param _1 the 1
* @param _2 the 2
* @param _3 the 3
* @param _4 the 4
* @param _5 the 5
* @param _6 the 6
* @param _7 the 7
* @param _8 the 8
* @param _9 the 9
* @return
* @deprecated you should consider using class Some Bean/Record class: MyRecord { final T1 propName1, final T2 propName2...}
*/
@Deprecated
public static Tuple9 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6, T7 _7, T8 _8,
T9 _9) {
return new Tuple9<>(_1, _2, _3, _4, _5, _6, _7, _8, _9);
}
/**
*
* @param the key type
* @param the value type
* @param entry
* @return
*/
@Beta
public static Tuple2 create(final Map.Entry entry) {
return new Tuple2<>(entry.getKey(), entry.getValue());
}
/**
*
*
* @param
* @param a
* @return
*/
@Beta
public static > TP create(final Object[] a) {
final int len = a == null ? 0 : a.length;
Tuple> result = null;
switch (len) {
case 0:
result = EMPTY;
break;
case 1:
result = new Tuple1<>(a[0]);
break;
case 2:
result = new Tuple2<>(a[0], a[1]);
break;
case 3:
result = new Tuple3<>(a[0], a[1], a[2]);
break;
case 4:
result = new Tuple4<>(a[0], a[1], a[2], a[3]);
break;
case 5:
result = new Tuple5<>(a[0], a[1], a[2], a[3], a[4]);
break;
case 6:
result = new Tuple6<>(a[0], a[1], a[2], a[3], a[4], a[5]);
break;
case 7:
result = new Tuple7<>(a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
break;
case 8:
result = new Tuple8<>(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
break;
case 9:
result = new Tuple9<>(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
break;
default:
throw new RuntimeException("Too many elements(" + a.length + ") to fill in Tuple.");
}
return (TP) result;
}
/**
*
*
* @param
* @param c
* @return
*/
@Beta
public static > TP create(final Collection> c) {
final int len = c == null ? 0 : c.size();
final Iterator> iter = c == null ? null : c.iterator();
Tuple> result = null;
switch (len) {
case 0:
result = EMPTY;
break;
case 1:
result = new Tuple1<>(iter.next());
break;
case 2:
result = new Tuple2<>(iter.next(), iter.next());
break;
case 3:
result = new Tuple3<>(iter.next(), iter.next(), iter.next());
break;
case 4:
result = new Tuple4<>(iter.next(), iter.next(), iter.next(), iter.next());
break;
case 5:
result = new Tuple5<>(iter.next(), iter.next(), iter.next(), iter.next(), iter.next());
break;
case 6:
result = new Tuple6<>(iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next());
break;
case 7:
result = new Tuple7<>(iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next());
break;
case 8:
result = new Tuple8<>(iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next());
break;
case 9:
result = new Tuple9<>(iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next(), iter.next());
break;
default:
throw new RuntimeException("Too many elements(" + c.size() + ") to fill in Tuple.");
}
return (TP) result;
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple1 extends T> tp) {
return N.asList(tp._1);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple2 extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple3 extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple4 extends T, ? extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3, tp._4);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple5 extends T, ? extends T, ? extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3, tp._4, tp._5);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple6 extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3, tp._4, tp._5, tp._6);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple7 extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3, tp._4, tp._5, tp._6, tp._7);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(Tuple8 extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3, tp._4, tp._5, tp._6, tp._7, tp._8);
}
/**
*
*
* @param
* @param tp
* @return
*/
@Beta
public static List toList(
Tuple9 extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T, ? extends T> tp) {
return N.asList(tp._1, tp._2, tp._3, tp._4, tp._5, tp._6, tp._7, tp._8, tp._9);
}
/**
*
*
* @param
* @param
* @param
* @param tp
* @return
*/
@Beta
public static Tuple3 flatten(Tuple2, T3> tp) {
return new Tuple3<>(tp._1._1, tp._1._2, tp._2);
}
/**
*
*
* @param
* @param
* @param
* @param
* @param
* @param tp
* @return
*/
@Beta
public static Tuple5 flatten(Tuple3, T4, T5> tp) {
return new Tuple5<>(tp._1._1, tp._1._2, tp._1._3, tp._2, tp._3);
}
// public static Tuple4 flatt(Tuple2, T3>, T4> tp) {
// return new Tuple4<>(tp._1._1._1, tp._1._1._2, tp._1._2, tp._2);
// }
/**
* The Class Tuple1.
*
* @param
*/
@Beta
static final class Tuple0 extends Tuple {
@Override
public int arity() {
return 0;
}
@Override
public boolean anyNull() {
return false;
}
@Override
public boolean allNull() {
return true;
}
@Override
public boolean contains(final Object objToFind) {
return false;
}
@Override
public Object[] toArray() {
return N.EMPTY_OBJECT_ARRAY;
}
@Override
public A[] toArray(A[] a) {
return a;
}
@Override
public void forEach(Throwables.Consumer, E> consumer) throws E {
N.checkArgNotNull(consumer);
// do nothing.
}
@Override
public String toString() {
return "[]";
}
}
/**
* The Class Tuple1.
*
* @param
*/
public static final class Tuple1 extends Tuple> {
/** The 1. */
public final T1 _1;
/**
* Instantiates a new tuple 1.
*/
// For Kryo
Tuple1() {
this(null);
}
/**
* Instantiates a new tuple 1.
*
* @param _1 the 1
*/
Tuple1(T1 _1) {
this._1 = _1;
}
/**
*
* @return
*/
@Override
public int arity() {
return 1;
}
/**
*
* @return
*/
@Override
public boolean anyNull() {
return _1 == null;
}
/**
*
* @return
*/
@Override
public boolean allNull() {
return _1 == null;
}
/**
*
*
* @param objToFind
* @return
*/
@Override
public boolean contains(final Object objToFind) {
return N.equals(_1, objToFind);
}
/**
*
* @return
*/
@Override
public Object[] toArray() {
return new Object[] { _1 };
}
/**
*
* @param
* @param a
* @return
*/
@Override
public A[] toArray(A[] a) {
if (a.length < 1) {
a = N.copyOf(a, 1);
}
a[0] = (A) _1;
return a;
}
/**
*
* @param
* @param consumer
* @throws E the e
*/
@Override
public void forEach(Throwables.Consumer, E> consumer) throws E {
final Throwables.Consumer