Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic 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: References: 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: 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 > Assume you implement the following: > > @FunctionalInterface > public interface CheckedFunction { > O apply(I input) throws E; > } > > @FunctionalInterface > public interface CheckedSupplier { > O get() throws E; > } > > public static O tryAlternatives( > CheckedFunction op, > I firstInput, > I... moreInputs > ) throws E { ... } > > public class CumulativeResource > implements AutoCloseable { > public final T resource; > > public interface CumulativeResourceBuilder { > CumulativeResourceBuilder > cumulate(CheckedFunction builder); > CumulativeResource build() throws Exception; > } > > public static CumulativeResourceBuilder > from(CheckedSupplier factory) { ... } > } > > Then your actual code might become this: > > CumulativeResource 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 rsRes = > tryAlternatives(this::fetchResult, firstDbUrl, secondDbUrl)) { > ResultSet rs = rsRes.resource; > while (rs.next()) { > System.out.println(rs.getRow()); > } > } > } Sexy. -- DF.