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-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* 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.Map;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.stream.Stream;
/**
*
* @since 0.8
*
* @author Haiyang Li
*
*/
public abstract class Tuple {
public static final Tuple EMPTY = new Tuple() {
@Override
public int arity() {
return 0;
}
@Override
public boolean anyNull() {
return false;
}
@Override
public boolean allNull() {
return true;
}
@Override
public Object[] toArray() {
return N.EMPTY_OBJECT_ARRAY;
}
@Override
public A[] toArray(A[] a) {
return a;
}
@Override
public void forEach(Try.Consumer, E> consumer) throws E {
N.checkArgNotNull(consumer);
// do nothing.
}
@Override
public Stream extends Tuple> stream() {
return Stream.empty();
}
@Override
public String toString() {
return "[]";
}
};
Tuple() {
}
public abstract int arity();
public abstract boolean anyNull();
public abstract boolean allNull();
public abstract Object[] toArray();
public abstract A[] toArray(A[] a);
public abstract void forEach(Try.Consumer, E> consumer) throws E;
protected abstract Stream extends Tuple> stream();
public static Tuple1 of(T1 _1) {
return new Tuple1<>(_1);
}
public static Tuple2 of(T1 _1, T2 _2) {
return new Tuple2<>(_1, _2);
}
public static Tuple3 of(T1 _1, T2 _2, T3 _3) {
return new Tuple3<>(_1, _2, _3);
}
public static Tuple4 of(T1 _1, T2 _2, T3 _3, T4 _4) {
return new Tuple4<>(_1, _2, _3, _4);
}
public static Tuple5 of(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5) {
return new Tuple5<>(_1, _2, _3, _4, _5);
}
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);
}
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);
}
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);
}
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);
}
public static Tuple2 from(final Map.Entry entry) {
return new Tuple2<>(entry.getKey(), entry.getValue());
}
public static T from(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 (T) result;
}
public static T from(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 (T) result;
}
public final static class Tuple1 extends Tuple {
public final T1 _1;
// For Kryo
Tuple1() {
this(null);
}
Tuple1(T1 _1) {
this._1 = _1;
}
@Override
public int arity() {
return 1;
}
@Override
public boolean anyNull() {
return _1 == null;
}
@Override
public boolean allNull() {
return _1 == null;
}
@Override
public Object[] toArray() {
return new Object[] { _1 };
}
@Override
public A[] toArray(A[] a) {
if (a.length < 1) {
a = N.copyOf(a, 1);
}
a[0] = (A) _1;
return a;
}
@Override
public void forEach(Try.Consumer, E> consumer) throws E {
final Try.Consumer