package com.annimon.stream.internal;

import java.io.Closeable;

public final class Compose {

    private Compose() { }

    public static Runnable runnables(final Runnable a, final Runnable b) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    a.run();
                } catch (Throwable e1) {
                    try {
                        b.run();
                    } catch (Throwable ignore) { }
                    if (e1 instanceof RuntimeException) {
                        throw (RuntimeException) e1;
                    }
                    throw (Error) e1;
                }
                b.run();
            }
        };
    }

    public static Runnable closeables(final Closeable a, final Closeable b) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    a.close();
                } catch (Throwable e1) {
                    try {
                        b.close();
                    } catch (Throwable ignore) { }
                    if (e1 instanceof RuntimeException) {
                        throw (RuntimeException) e1;
                    }
                    throw (Error) e1;
                }
                try {
                    b.close();
                } catch (Throwable e2) {
                    if (e2 instanceof RuntimeException) {
                        throw (RuntimeException) e2;
                    } else if (e2 instanceof Error) {
                        throw (Error) e2;
                    } else {
                        throw new RuntimeException(e2);
                    }
                }
            }
        };
    }
}

@@ -502,97 +502,27 @@
     
-if (e1 instanceof RuntimeException) %7B%0A                        throw (RuntimeException) 
+handleException(
 e1
+)
 ;%0A  
@@ -543,46 +543,14 @@
     
-%7D%0A                    throw (Error) e1
+return
 ;%0A  
@@ -1016,32 +1016,13 @@
     
-if (e1 instanceof Runtim
+handl
 eExc
@@ -1019,38 +1019,38 @@
  handleException
-) %7B%0A  
+(e1);%0A
                 
@@ -1057,43 +1057,16 @@
     
-  throw (RuntimeException) e1;%0A    
+return;%0A
     
@@ -1099,30 +1099,19 @@
     
-    throw (Error) e1;%0A
+try %7B%0A     
     
@@ -1113,34 +1113,42 @@
                 
- %7D
+b.close();
 %0A               
@@ -1140,35 +1140,54 @@
                 
-try
+%7D catch (Throwable e2)
  %7B%0A             
@@ -1189,32 +1189,42 @@
             
-b.close(
+handleException(e2
 );%0A         
@@ -1223,39 +1223,104 @@
                %7D
- catch 
+%0A            %7D%0A        %7D;%0A    %7D%0A%0A    private static void handleException
 (Throwable e2) %7B
@@ -1307,33 +1307,32 @@
 tion(Throwable e
-2
 ) %7B%0A            
@@ -1331,26 +1331,13 @@
     
-     
-       
 if (e
-2
  ins
@@ -1372,36 +1372,24 @@
             
-   
-         
 throw (Runti
@@ -1402,31 +1402,18 @@
 ption) e
-2
 ;%0A
-            
         
@@ -1424,17 +1424,16 @@
 se if (e
-2
  instanc
@@ -1453,36 +1453,24 @@
             
-        
-    
 throw (Error
@@ -1472,17 +1472,16 @@
 Error) e
-2
 ;%0A      
@@ -1482,28 +1482,16 @@
         
-      
-      
 %7D else %7B
@@ -1503,28 +1503,16 @@
         
-     
-       
 throw ne
@@ -1523,33 +1523,32 @@
 ntimeException(e
-2
 );%0A             
@@ -1546,64 +1546,9 @@
     
-            %7D%0A                %7D%0A            %7D%0A        %7D;
+%7D
 %0A   

Fix possible ClassCastException in closeables composer