Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38879
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Does this make sense? |
| Date | Sun, 7 Apr 2019 22:14:51 +0200 |
| Organization | A noiseless patient Spider |
| Lines | 64 |
| Message-ID | <q8dloh$fh9$1@dont-email.me> (permalink) |
| References | <f9818c55-5eb6-47f8-8ec9-d512861ddee7@googlegroups.com> <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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Sun, 7 Apr 2019 20:15:14 -0000 (UTC) |
| Injection-Info | reader02.eternal-september.org; posting-host="acb4316d6da48adbd4d1afd1346ee54f"; logging-data="15913"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XE7C88x4poMHRZ/5HSQrj" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 |
| Cancel-Lock | sha1:NU+wy/EP6De5mMycLYC44yVC3do= |
| In-Reply-To | <q87t34$l7l$1@newsreader4.netcologne.de> |
| X-Antivirus-Status | Clean |
| Content-Language | en-US |
| X-Antivirus | AVG (VPS 190407-4, 04/07/2019), Outbound message |
| Xref | csiph.com comp.lang.java.programmer:38879 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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