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

org.jooq.lambda.tuple.Tuple1 Maven / Gradle / Ivy

Go to download

jOOλ is part of the jOOQ series (along with jOOQ, jOOX, jOOR, jOOU) providing some useful extensions to Java 8 lambdas.

There is a newer version: 0.9.15
Show newest version
/**
 * Copyright (c) Data Geekery GmbH, [email protected]
 *
 * 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 org.jooq.lambda.tuple;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import org.jooq.lambda.Seq;
import org.jooq.lambda.function.Function1;

/**
 * A tuple of degree 1.
 *
 * @author Lukas Eder
 */
public class Tuple1 implements Tuple, Comparable>, Serializable, Cloneable {

    private static final long serialVersionUID = 1L;

    public final T1 v1;

    public T1 v1() {
        return v1;
    }

    public Tuple1(Tuple1 tuple) {
        this.v1 = tuple.v1;
    }

    public Tuple1(T1 v1) {
        this.v1 = v1;
    }

    /**
     * Concatenate a value to this tuple.
     */
    public final  Tuple2 concat(T2 value) {
        return new Tuple2<>(v1, value);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple2 concat(Tuple1 tuple) {
        return new Tuple2<>(v1, tuple.v1);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple3 concat(Tuple2 tuple) {
        return new Tuple3<>(v1, tuple.v1, tuple.v2);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple4 concat(Tuple3 tuple) {
        return new Tuple4<>(v1, tuple.v1, tuple.v2, tuple.v3);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple5 concat(Tuple4 tuple) {
        return new Tuple5<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple6 concat(Tuple5 tuple) {
        return new Tuple6<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple7 concat(Tuple6 tuple) {
        return new Tuple7<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple8 concat(Tuple7 tuple) {
        return new Tuple8<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple9 concat(Tuple8 tuple) {
        return new Tuple9<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple10 concat(Tuple9 tuple) {
        return new Tuple10<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple11 concat(Tuple10 tuple) {
        return new Tuple11<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9, tuple.v10);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple12 concat(Tuple11 tuple) {
        return new Tuple12<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9, tuple.v10, tuple.v11);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple13 concat(Tuple12 tuple) {
        return new Tuple13<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9, tuple.v10, tuple.v11, tuple.v12);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple14 concat(Tuple13 tuple) {
        return new Tuple14<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9, tuple.v10, tuple.v11, tuple.v12, tuple.v13);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple15 concat(Tuple14 tuple) {
        return new Tuple15<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9, tuple.v10, tuple.v11, tuple.v12, tuple.v13, tuple.v14);
    }

    /**
     * Concatenate a tuple to this tuple.
     */
    public final  Tuple16 concat(Tuple15 tuple) {
        return new Tuple16<>(v1, tuple.v1, tuple.v2, tuple.v3, tuple.v4, tuple.v5, tuple.v6, tuple.v7, tuple.v8, tuple.v9, tuple.v10, tuple.v11, tuple.v12, tuple.v13, tuple.v14, tuple.v15);
    }

    /**
     * Split this tuple into two tuples of degree 0 and 1.
     */
    public final Tuple2> split0() {
        return new Tuple2<>(limit0(), skip0());
    }

    /**
     * Split this tuple into two tuples of degree 1 and 0.
     */
    public final Tuple2, Tuple0> split1() {
        return new Tuple2<>(limit1(), skip1());
    }

    /**
     * Limit this tuple to degree 0.
     */
    public final Tuple0 limit0() {
        return new Tuple0();
    }

    /**
     * Limit this tuple to degree 1.
     */
    public final Tuple1 limit1() {
        return this;
    }

    /**
     * Skip 0 degrees from this tuple.
     */
    public final Tuple1 skip0() {
        return this;
    }

    /**
     * Skip 1 degrees from this tuple.
     */
    public final Tuple0 skip1() {
        return new Tuple0();
    }

    /**
     * Apply this tuple as arguments to a function.
     */
    public final  R map(Function function) {
        return function.apply(v1);
    }

    /**
     * Apply attribute 1 as argument to a function and return a new tuple with the substituted argument.
     */
    public final  Tuple1 map1(Function function) {
        return Tuple.tuple(function.apply(v1));
    }

    @Override
    @Deprecated
    public final Object[] array() {
        return toArray();
    }

    @Override
    public final Object[] toArray() {
        return new Object[] { v1 };
    }

    @Override
    @Deprecated
    public final List list() {
        return toList();
    }

    @Override
    public final List toList() {
        return Arrays.asList(toArray());
    }

    @Override
    public final Seq toSeq() {
        return Seq.seq(toList());
    }

    @Override
    public final Map toMap() {
        return toMap(i -> "v" + (i + 1));
    }

    @Override
    public final  Map toMap(Function keyMapper) {
        Map result = new LinkedHashMap<>();
        Object[] array = toArray();

        for (int i = 0; i < array.length; i++)
            result.put(keyMapper.apply(i), array[i]);

        return result;
    }

    /**
     * The degree of this tuple: 1.
     */
    @Override
    public final int degree() {
        return 1;
    }

    @Override
    @SuppressWarnings("unchecked")
    public final Iterator iterator() {
        return (Iterator) list().iterator();
    }

    @Override
    public int compareTo(Tuple1 other) {
        int result = 0;

        result = Tuples.compare(v1, other.v1); if (result != 0) return result;

        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof Tuple1))
            return false;

        @SuppressWarnings({ "unchecked", "rawtypes" })
        final Tuple1 that = (Tuple1) o;

        if (!Objects.equals(v1, that.v1)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;

        result = prime * result + ((v1 == null) ? 0 : v1.hashCode());

        return result;
    }

    @Override
    public String toString() {
        return "("
             +        v1
             + ")";
    }

    @Override
    public Tuple1 clone() {
        return new Tuple1<>(this);
    }
}