Java Code Examples for com.github.davidmoten.guavamini.Preconditions#checkNotNull()

The following examples show how to use com.github.davidmoten.guavamini.Preconditions#checkNotNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParametersBuilder.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private final T parameterList(Object[] values) {
    Preconditions.checkNotNull(values);
    if (values.length == 0) {
        // no effect
        return (T) this;
    }
    Preconditions.checkArgument(sqlInfo.numParameters() == 0 || values.length % sqlInfo.numParameters() == 0,
            "number of values should be a multiple of number of parameters in sql: " + sqlInfo.sql());
    Preconditions.checkArgument(Arrays.stream(values)
            .allMatch(o -> sqlInfo.names().isEmpty() || (o instanceof Parameter && ((Parameter) o).hasName())));
    for (Object val : values) {
        if (val == null) {
            parameterBuffer.add(Parameter.NULL);
        } else {
            parameterBuffer.add(val);
        }
    }
    return (T) this;
}
 
Example 2
Source File: TransactedReturnGeneratedKeysBuilder.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
/**
 * Transforms the results using the given function.
 *
 * @param mapper
 *            maps the query results to an object
 * @return the results of the query as an Observable
 */
@Override
public <T> Flowable<Tx<T>> get(@Nonnull ResultSetMapper<? extends T> mapper) {
    Preconditions.checkNotNull(mapper, "mapper cannot be null");
    return Flowable.defer(() -> {
        AtomicReference<Connection> connection = new AtomicReference<Connection>();
        Flowable<T> o = Update.<T>createReturnGeneratedKeys( //
                update.updateBuilder.connections //
                        .map(c -> Util.toTransactedConnection(connection, c)),
                update.parameterGroupsToFlowable(), update.updateBuilder.sql, mapper, false);
        return o.materialize() //
                .flatMap(n -> Tx.toTx(n, connection.get(), db)) //
                .doOnNext(tx -> {
                    if (tx.isComplete()) {
                        ((TxImpl<T>) tx).connection().commit();
                    }
                });
    });
}
 
Example 3
Source File: ConnectionProvider.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
static ConnectionProvider from(@Nonnull String url, String username, String password) {
    Preconditions.checkNotNull(url, "url cannot be null");
    return new ConnectionProvider() {

        @Override
        public Connection get() {
            try {
                return DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
        }

        @Override
        public void close() {
            // do nothing as closure will be handle by pool
        }
    };
}
 
Example 4
Source File: ConnectionProvider.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
static ConnectionProvider from(@Nonnull String url, Properties properties) {
    Preconditions.checkNotNull(url, "url cannot be null");
    Preconditions.checkNotNull(properties, "properties cannot be null");
    return new ConnectionProvider() {

        @Override
        public Connection get() {
            try {
                return DriverManager.getConnection(url, properties);
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
        }

        @Override
        public void close() {
            // do nothing as closure will be handle by pool
        }
    };
}
 
Example 5
Source File: Tuples.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
public static <T1, T2, T3, T4> ResultSetMapper<Tuple4<T1, T2, T3, T4>> tuple(
        final Class<T1> cls1, final Class<T2> cls2, final Class<T3> cls3,
        final Class<T4> cls4) {
    Preconditions.checkNotNull(cls1, "cls1 cannot be null");
    Preconditions.checkNotNull(cls2, "cls2 cannot be null");
    Preconditions.checkNotNull(cls3, "cls3 cannot be null");
    Preconditions.checkNotNull(cls4, "cls4 cannot be null");
    return new ResultSetMapper<Tuple4<T1, T2, T3, T4>>() {
        @Override
        public Tuple4<T1, T2, T3, T4> apply(ResultSet rs) {
            return new Tuple4<T1, T2, T3, T4>(mapObject(rs, cls1, 1),
                    mapObject(rs, cls2, 2), mapObject(rs, cls3, 3),
                    mapObject(rs, cls4, 4));
        }
    };
}
 
Example 6
Source File: Tuples.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
public static <T1, T2, T3, T4, T5> ResultSetMapper<Tuple5<T1, T2, T3, T4, T5>> tuple(
        final Class<T1> cls1, final Class<T2> cls2, final Class<T3> cls3, final Class<T4> cls4,
        final Class<T5> cls5) {
    Preconditions.checkNotNull(cls1, "cls1 cannot be null");
    Preconditions.checkNotNull(cls2, "cls2 cannot be null");
    Preconditions.checkNotNull(cls3, "cls3 cannot be null");
    Preconditions.checkNotNull(cls4, "cls4 cannot be null");
    Preconditions.checkNotNull(cls5, "cls5 cannot be null");

    return new ResultSetMapper<Tuple5<T1, T2, T3, T4, T5>>() {
        @Override
        public Tuple5<T1, T2, T3, T4, T5> apply(ResultSet rs) {
            return new Tuple5<T1, T2, T3, T4, T5>(mapObject(rs, cls1, 1),
                    mapObject(rs, cls2, 2), mapObject(rs, cls3, 3),
                    mapObject(rs, cls4, 4), mapObject(rs, cls5, 5));
        }
    };
}
 
Example 7
Source File: Strings.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static Flowable<String> from(final File file, final Charset charset) {
    Preconditions.checkNotNull(file);
    Preconditions.checkNotNull(charset);
    Callable<Reader> resourceFactory = new Callable<Reader>() {
        @Override
        public Reader call() throws FileNotFoundException {
            return new InputStreamReader(new FileInputStream(file), charset);
        }
    };
    return from(resourceFactory);
}
 
Example 8
Source File: FlowableWindowMinMax.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public FlowableWindowMinMax(Flowable<T> source, int windowSize, Comparator<? super T> comparator, Metric metric) {
    Preconditions.checkArgument(windowSize > 0, "windowSize must be greater than zero");
    Preconditions.checkNotNull(comparator, "comparator cannot be null");
    Preconditions.checkNotNull(metric, "metric cannot be null");
    this.source = source;
    this.windowSize = windowSize;
    this.comparator = comparator;
    this.metric = metric;
}
 
Example 9
Source File: SelectBuilder.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Flowable<T> get(@Nonnull ResultSetMapper<? extends T> mapper) {
    Preconditions.checkNotNull(mapper, "mapper cannot be null");
    Flowable<List<Object>> pg = super.parameterGroupsToFlowable();
    Flowable<T> f = Select.<T>create(connection, pg, sql, fetchSize, mapper, true, queryTimeoutSec);
    if (dependsOn != null) {
        return dependsOn.ignoreElements().andThen(f);
    } else {
        return f;
    }
}
 
Example 10
Source File: SelectBuilder.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
SelectBuilder(String sql, Single<Connection> connection, Database db) {
    super(sql);
    Preconditions.checkNotNull(sql);
    Preconditions.checkNotNull(connection);
    this.sql = sql;
    this.connection = connection;
    this.db = db;
}
 
Example 11
Source File: StateMachineDefinition.java    From state-machine with Apache License 2.0 5 votes vote down vote up
public <R extends Event<? super T>> State<T, R> createState(String name, Class<R> eventClass) {
    Preconditions.checkArgument(!eventClass.isAnnotationPresent(GenerateImmutable.class),
            "cannot base a state on an event that is annotated with @GenerateImmutable, use the generated immutable class instead");
    Preconditions.checkNotNull(name);
    if (name.equals("Initial")) {
        name = name.concat("_1");
    }
    State<T, R> state = new State<T, R>(this, name, eventClass);
    states.add(state);
    return state;
}
 
Example 12
Source File: ReturnGeneratedKeysBuilder.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms the results using the given function.
 *
 * @param mapper
 *            maps the query result to an object
 * @return the results of the query as an Observable
 */
@Override
public <T> Flowable<T> get(@Nonnull ResultSetMapper<? extends T> mapper) {
    Preconditions.checkNotNull(mapper, "mapper cannot be null");
    return update.startWithDependency(Update.<T>createReturnGeneratedKeys(update.connections,
            update.parameterGroupsToFlowable(), update.sql, mapper, true));

}
 
Example 13
Source File: Database.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
public SelectBuilder select(@Nonnull String sql) {
    Preconditions.checkNotNull(sql, "sql cannot be null");
    return new SelectBuilder(sql, connection(), this);
}
 
Example 14
Source File: TxGetter1.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
default <T> CallableResultSets1Builder<T> getAs(@Nonnull Class<T> cls) {
    Preconditions.checkNotNull(cls, "cls cannot be null");
    return get(rs -> Util.mapObject(rs, cls, 1));
}
 
Example 15
Source File: Getter4.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
default <T4> CallableResultSets4Builder<T1, T2, T3, T4> getAs(@Nonnull Class<T4> cls) {
    Preconditions.checkNotNull(cls, "cls cannot be null");
    return get(rs -> Util.mapObject(rs, cls, 1));
}
 
Example 16
Source File: DependsOn.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
default T dependsOn(@Nonnull Observable<?> observable) {
    Preconditions.checkNotNull(observable, "observable cannot be null");
    return dependsOn(observable.ignoreElements().toFlowable());
}
 
Example 17
Source File: ParametersBuilder.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public final T parameter(@Nonnull String name, Object value) {
    Preconditions.checkNotNull(name, "name cannot be null");
    parameterBuffer.add(new Parameter(name, value));
    return (T) this;
}
 
Example 18
Source File: TransactedBuilder.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
public TransactedSelectBuilder select(@Nonnull String sql) {
    Preconditions.checkNotNull(sql, "sql cannot be null");
    return new SelectBuilder(sql, connections, db).transacted();
}
 
Example 19
Source File: TxGetterN.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
default <T> CallableResultSetsNBuilder getAs(@Nonnull Class<T> cls) {
    Preconditions.checkNotNull(cls, "cls cannot be null");
    return get(rs -> Util.mapObject(rs, cls, 1));
}
 
Example 20
Source File: GetterTx.java    From rxjava2-jdbc with Apache License 2.0 2 votes vote down vote up
/**
 * <p>
 * Transforms each row of the {@link ResultSet} into an instance of
 * <code>T</code> using <i>automapping</i> of the ResultSet columns into
 * corresponding constructor parameters that are assignable. Beyond normal
 * assignable criteria (for example Integer 123 is assignable to a Double) other
 * conversions exist to facilitate the automapping:
 * </p>
 * <p>
 * They are:
 * <ul>
 * <li>java.sql.Blob --&gt; byte[]</li>
 * <li>java.sql.Blob --&gt; java.io.InputStream</li>
 * <li>java.sql.Clob --&gt; String</li>
 * <li>java.sql.Clob --&gt; java.io.Reader</li>
 * <li>java.sql.Date --&gt; java.util.Date</li>
 * <li>java.sql.Date --&gt; Long</li>
 * <li>java.sql.Timestamp --&gt; java.util.Date</li>
 * <li>java.sql.Timestamp --&gt; Long</li>
 * <li>java.sql.Time --&gt; java.util.Date</li>
 * <li>java.sql.Time --&gt; Long</li>
 * <li>java.math.BigInteger --&gt;
 * Short,Integer,Long,Float,Double,BigDecimal</li>
 * <li>java.math.BigDecimal --&gt;
 * Short,Integer,Long,Float,Double,BigInteger</li>
 * </ul>
 *
 * @param cls
 *            class to automap each row of the ResultSet to
 * @param <T>
 *            generic type of returned stream emissions
 * @return Flowable of T
 * 
 */
default <T> Flowable<Tx<T>> autoMap(@Nonnull Class<T> cls) {
    Preconditions.checkNotNull(cls, "cls cannot be null");
    return get(Util.autoMap(cls));
}