Javaでtimes

最近++に関する話題が多いので、Javaで++の隠ぺいを試みる。

public class CountUtils {
    
    public static abstract class InnerProc<E> {
        public abstract proc(int i);
        protected E getResult() { return null; }
    }
    
    public static <E> E times(int times, InnerProc<E> proc) {
        for (int i = 0; i < times; i++) proc.proc(i);
        return proc.getResult();
    }
}

とりあえずこんな感じかな。例外は全く無視してるけど、proc内部でRuntimeExceptionでラップしてくださいね、みたいな。それか、times内部でExceptionを全部キャッチ( ! )して、RuntimeExceptionを継承したTimesExceptionとか作って再スローとか。こういう場合のExceptionのキャッチって何か問題あるのかな・・・

public static <E> E times(int times, InnerProc<E> proc) {
    for (int i = 0; i < times; i++) {
        try {
            proc.proc(i);
        } catch (Exception e) {
            throws TimesException(e);
        }
    }
    return proc.getResult();
}

これの一番の問題は、例外処理が必要ない時の効率の悪さかも。