Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38879

Re: Does this make sense?

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Does this make sense?
Date 2019-04-07 22:14 +0200
Organization A noiseless patient Spider
Message-ID <q8dloh$fh9$1@dont-email.me> (permalink)
References (1 earlier) <q84vam$lri$1@dont-email.me> <c001151d-044c-4d1b-ad88-0efd923e4433@googlegroups.com> <q85699$oq6$1@dont-email.me> <b8961ec3-eac5-45da-aa5d-11144bcae219@googlegroups.com> <q87t34$l7l$1@newsreader4.netcologne.de>

Show all headers | View raw


> Assume you implement the following:
> 
> @FunctionalInterface
> public interface CheckedFunction<I, O, E extends Exception> {
>   O apply(I input) throws E;
> }
> 
> @FunctionalInterface
> public interface CheckedSupplier<O, E extends Exception> {
>   O get() throws E;
> }
> 
> public static <I, O, E extends Exception> O tryAlternatives(
>     CheckedFunction<I, O, E> op,
>     I firstInput,
>     I... moreInputs
> ) throws E { ... }
> 
> public class CumulativeResource<T extends AutoCloseable>
>     implements AutoCloseable {
>   public final T resource;
> 
>   public interface CumulativeResourceBuilder<T extends AutoCloseable> {
>     <S extends AutoCloseable> CumulativeResourceBuilder<S>
>       cumulate(CheckedFunction<T, S, Exception> builder);
>     CumulativeResource<T> build() throws Exception;
>   }
> 
>   public static <T extends AutoCloseable> CumulativeResourceBuilder<T>
>     from(CheckedSupplier<T, Exception> factory) { ... }
> }
> 
> Then your actual code might become this:
> 
> CumulativeResource<ResultSet> fetchResult(String dbUrl)
>     throws Exception {
>   return
>     CumulativeResource
>       .from(() -> DriverManager.getConnection(dbUrl, user, password))
>       .cumulate(c ->
>         c.prepareStatement(
>           query,
>           ResultSet.TYPE_SCROLL_INSENSITIVE,
>           ResultSet.CONCUR_READ_ONLY
>         )
>       )
>       .cumulate(PreparedStatement::executeQuery)
>       .build();
>     }
> 
> void runQuery() throws Exception {
>   try(CumulativeResource<ResultSet> rsRes =
>       tryAlternatives(this::fetchResult, firstDbUrl, secondDbUrl)) {
>     ResultSet rs = rsRes.resource;
>     while (rs.next()) {
>       System.out.println(rs.getRow());
>     }
>   }
> }

Sexy.

-- 
DF.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 05:22 -0700
  Re: Does this make sense? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-04 09:03 -0400
    Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 07:02 -0700
      Re: Does this make sense? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-04-04 11:01 -0400
        Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 08:21 -0700
        Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-04 11:45 -0700
          Re: Does this make sense? Arne Vajhøj <arne@vajhoej.dk> - 2019-04-04 20:33 -0400
            Re: Does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2019-04-05 08:39 -0700
              Re: Does this make sense? Arne Vajhøj <arne@vajhoej.dk> - 2019-04-05 12:31 -0400
              Re: Does this make sense? bursejan@gmail.com - 2019-04-05 10:53 -0700
                Re: Does this make sense? bursejan@gmail.com - 2019-04-05 10:57 -0700
                Re: Does this make sense? bursejan@gmail.com - 2019-04-05 11:02 -0700
                Re: Does this make sense? bursejan@gmail.com - 2019-04-05 11:13 -0700
          Re: Does this make sense? Patrick Roemer <sangamon@netcologne.de> - 2019-04-05 17:43 +0200
            Re: Does this make sense? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-04-07 22:14 +0200
      Re: Does this make sense? Andreas Leitgeb <avl@logic.at> - 2019-04-04 15:15 +0000

csiph-web