Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I find the code honestly hard to follow and understand. It's full of <code>static</code> which is usually a bad sign in OO design and it's hard to follow with the generics. </p> <p>Wouldn't something simpler like this work as well?</p> <pre><code>private static &lt;T&gt; T getter(Future&lt;T&gt; src) { try { return src.get(); } catch (InterruptedException e) { handle( "some text"); } catch (ExecutionException e) { handle( e ) } return null; } </code></pre> <p>You can implement as many <code>handle</code> method as necessary in a base class (or in a static utility class) and use them in the catch block as necessary. Methods will be selected based on the signature, so if you want to print the text, you pass the string, if you want the stack trace you pass the exception (or both). Which leads to the combinations:</p> <pre><code>handle( String msg ) handle( Exception e ) handle( Exception e, String msg ) </code></pre> <p>This solution has less <code>if</code>, which is usually a good sign as well.</p> <p>But I have maybe missed a point, given that the code you published is just an excerpt of the whole code.</p> <p>Have a look otherwise at this question, which is also related: <a href="https://stackoverflow.com/questions/2236063/pluggable-error-handling-strategy/2236437#2236437">Pluggable Error Handling Strategy</a></p> <p><strong>EDIT</strong></p> <p>If the solution I proposed above is too simple for your need, here are two other ways:</p> <pre><code>public class AbstractGetter&lt;From&gt; implements Function&lt;Future&lt;? extends From&gt;, From&gt; { private abstract handleInterrupt( Exception e ); private abstract handleExecution( Exception e ); private static &lt;T&gt; T getter(Future&lt;T&gt; src ) { try { return src.get(); } catch (InterruptedException e) { handleInterrupt(e) } catch (ExecutionException e) { handleExecution(e) } return null; } } </code></pre> <p>And you implement the X concrete class that correspond the various exception handling strategies. That's essentially the <a href="http://en.wikipedia.org/wiki/Template_method_pattern" rel="nofollow noreferrer">template</a> pattern.</p> <p>You can still use delegation, but at a more coarse-grained level. Instead of providing individual handler, you provide a handler strategy. That's kind of variation of the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow noreferrer">strategy</a> pattern then.</p> <pre><code> public interface ErrorStrategy { public void handleInterrupt(Exception e); public void handleExecution(Exception e); } public class Getter&lt;From&gt; implements Function&lt;Future&lt;? extends From&gt;, From&gt; { ErrorStrategy handler = new DefaultErrorStrategy(). // default one public Getter&lt;From&gt;() { } public Getter&lt;From&gt;( ErrorStrategy h ) { this.handler = h. } private static &lt;T&gt; T getter(Future&lt;T&gt; src ) { try { return src.get(); } catch (InterruptedException e) { handler.handleInterrupt(e) } catch (ExecutionException e) { handler.handleExecution(e) } return null; } } </code></pre> <p>You can create the X error handling strategies that you need. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload