Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Eureka.</p> <p>The fact that there is no declaration time variance declarations in Java means that deeply-nested generic types like this cause no end of headaches. The solution is to use a stupid wrapper class to force the inheritance to work.</p> <pre><code>public static class PairCallback&lt; A, B &gt; implements Callback&lt; Pair&lt; A, B &gt; &gt; { private final Callback&lt; Pair&lt; A, B &gt; &gt; cbDelegate; public PairCallback( Callback&lt; Pair&lt; A, B &gt; &gt; cbDelegate ) { this.cbDelegate = cbDelegate; } public void onSuccess( A a, B b ) { onSuccess( new Pair&lt; A, B &gt;( a, b ) ); } @Override public void onSuccess( Pair&lt; A, B &gt; p ) { cbDelegate.onSuccess( p ); } @Override public void onFailure( Throwable caught ) { cbDelegate.onFailure( caught ); } } public static &lt; A extends Entity, B extends Entity &gt; void doubleRequestExecute( Request&lt; A &gt; reqA, Request&lt; B &gt; reqB, final PairCallback&lt; ? super A, ? super B &gt; callback ) { final Holder&lt; A &gt; result1 = new Holder&lt; A &gt;(); final Holder&lt; B &gt; result2 = new Holder&lt; B &gt;(); reqA.execute(new Callback&lt;A&gt;() { @Override public void onSuccess(A result) { if (result2.value != null) { callback.onSuccess(result, result2.value); } else { result1.value = result; } } @Override public void onFailure(Throwable caught) { callback.onFailure(caught); } }); reqB.execute(new Callback&lt;B&gt;() { @Override public void onSuccess(B result) { if (result1.value != null) { callback.onSuccess(result1.value, result); } else { result2.value = result; } } @Override public void onFailure(Throwable caught) { callback.onFailure(caught); } }); } private static class Entity1 extends Entity {} private static class Entity2 extends Entity1 {} public static void main( String... args ) { doubleRequestExecute( new Request&lt; Entity &gt;(), new Request&lt; Entity &gt;(), new PairCallback&lt; Object, Object &gt;( new Callback&lt; Pair&lt; Object, Object &gt; &gt;() { @Override public void onSuccess( Pair&lt; Object, Object&gt; result ) {} @Override public void onFailure( Throwable caught ) {} } ) ); doubleRequestExecute( new Request&lt; Entity2 &gt;(), new Request&lt; Entity1 &gt;(), new PairCallback&lt; Entity1, Entity &gt;( new Callback&lt; Pair&lt; Entity1, Entity &gt; &gt;() { @Override public void onSuccess( Pair&lt; Entity1, Entity &gt; result ) {} @Override public void onFailure( Throwable caught ) {} } ) ); } </code></pre> <p>I'm going to up-vote your question because of how challenging and relevant it is.</p>
 

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