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

com.github.davidmoten.rx.jdbc.tuple.Tuples Maven / Gradle / Ivy

There is a newer version: 0.7.19
Show newest version
package com.github.davidmoten.rx.jdbc.tuple;

import static com.github.davidmoten.rx.jdbc.Util.mapObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import rx.functions.Func1;

/**
 * Utility methods for tuples.
 */
public final class Tuples {

    /**
     * Private constructor to prevent instantiation.
     */
    private Tuples() {
        // prevent instantiation.
    }

    public static  Func1 single(final Class cls) {
        return new Func1() {

            @SuppressWarnings("unchecked")
            @Override
            public T call(ResultSet rs) {
                return (T) mapObject(rs, cls, 1);
            }

        };
    }

    public static  Func1> tuple(final Class cls1, final Class cls2) {
        return new Func1>() {

            @SuppressWarnings("unchecked")
            @Override
            public Tuple2 call(ResultSet rs) {
                return new Tuple2((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2));
            }
        };
    }

    public static  Func1> tuple(final Class cls1, final Class cls2,
            final Class cls3) {
        return new Func1>() {
            @SuppressWarnings("unchecked")
            @Override
            public Tuple3 call(ResultSet rs) {
                return new Tuple3((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2), (T3) mapObject(
                        rs, cls3, 3));
            }
        };
    }

    public static  Func1> tuple(final Class cls1,
            final Class cls2, final Class cls3, final Class cls4) {
        return new Func1>() {
            @SuppressWarnings("unchecked")
            @Override
            public Tuple4 call(ResultSet rs) {
                return new Tuple4((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2),
                        (T3) mapObject(rs, cls3, 3), (T4) mapObject(rs, cls4, 4));
            }
        };
    }

    public static  Func1> tuple(final Class cls1,
            final Class cls2, final Class cls3, final Class cls4, final Class cls5) {
        return new Func1>() {
            @SuppressWarnings("unchecked")
            @Override
            public Tuple5 call(ResultSet rs) {
                return new Tuple5((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2),
                        (T3) mapObject(rs, cls3, 3), (T4) mapObject(rs, cls4, 4), (T5) mapObject(rs, cls5, 5));
            }
        };
    }

    public static  Func1> tuple(final Class cls1,
            final Class cls2, final Class cls3, final Class cls4, final Class cls5, final Class cls6) {

        return new Func1>() {
            @SuppressWarnings("unchecked")
            @Override
            public Tuple6 call(ResultSet rs) {
                return new Tuple6((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2),
                        (T3) mapObject(rs, cls3, 3), (T4) mapObject(rs, cls4, 4), (T5) mapObject(rs, cls5, 5),
                        (T6) mapObject(rs, cls6, 6));
            }
        };
    }

    public static  Func1> tuple(
            final Class cls1, final Class cls2, final Class cls3, final Class cls4,
            final Class cls5, final Class cls6, final Class cls7) {

        return new Func1>() {
            @SuppressWarnings("unchecked")
            @Override
            public Tuple7 call(ResultSet rs) {
                return new Tuple7((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2),
                        (T3) mapObject(rs, cls3, 3), (T4) mapObject(rs, cls4, 4), (T5) mapObject(rs, cls5, 5),
                        (T6) mapObject(rs, cls6, 6), (T7) mapObject(rs, cls7, 7));
            }
        };
    }

    public static  Func1> tupleN(final Class cls) {
        return new Func1>() {
            @Override
            public TupleN call(ResultSet rs) {
                return toTupleN(cls, rs);
            }
        };
    }

    @SuppressWarnings("unchecked")
    private static  TupleN toTupleN(final Class cls, ResultSet rs) {
        try {
            int n = rs.getMetaData().getColumnCount();
            List list = new ArrayList();
            for (int i = 1; i <= n; i++) {
                list.add((T) mapObject(rs, cls, i));
            }
            return new TupleN(list);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy