脊髄反射だけでやってみている。

昨日は、コードが、重複してて、アレだな…とか、思ったりしたりしなかったり。
やはり、javaは処理構造を再利用する為には、結構冗長なコードが必要になるですな。

public class StreamUtil {
  public interface _<STREAM, T extends Throwable> {
    STREAM open() throws T;
    void handle(STREAM in) throws T;
    void happen(T exception);
  }

  private interface D<STREAM> {
    void dispose(STREAM stream);
  }

  @SuppressWarnings("unchecked")
  private static <STREAM, T extends Throwable> void $(_<STREAM, T> _,
      D<STREAM> c) {
    STREAM in = null;
    try {
      in = _.open();
      _.handle(in);
    } catch (Throwable e) {
      _.happen((T) e);
    } finally {
      c.dispose(in);
    }
  }

  public static <STREAM extends InputStream, T extends Throwable> void is(
      _<STREAM, T> _) {
    $(_, new D<STREAM>() {
      public void dispose(STREAM stream) {
        close(stream);
      };
    });
  }

  public static <STREAM extends OutputStream, T extends Throwable> void os(
      _<STREAM, T> _) {
    $(_, new D<STREAM>() {
      public void dispose(STREAM stream) {
        close(stream);
      };
    });
  }

  public static void close(InputStream in) {
    try {
      if (in != null) {
        in.close();
      }
    } catch (IOException e) {
      throw new IORuntimeException(e);
    }
  }

  public static void close(OutputStream out) {
    try {
      if (out != null) {
        out.close();
      }
    } catch (IOException e) {
      throw new IORuntimeException(e);
    }
  }
}