具有可关闭资源的Java Collector作为累加器

时间:2018-03-15 10:49:40

标签: java java-stream collectors

假设我正在尝试创建一个收集器,该收集器将数据聚合到一个必须在使用后关闭的资源中。有没有办法在finally中实现与Collector块类似的内容?在成功的情况下,这可以在finisher方法中完成,但是在异常情况下似乎没有调用任何方法。

目标是以干净的方式实现类似下面的操作,而不必先将流收集到内存列表中。

stream.collect(groupingBy(this::extractFileName, collectToFile()));

2 个答案:

答案 0 :(得分:1)

我认为您可以满足您的要求的唯一方法是通过提供给Stream.onClose方法的关闭处理程序。假设您有以下类:

class CloseHandler implements Runnable {
    List<Runnable> children = new ArrayList<>();

    void add(Runnable ch) { children.add(ch); }

    @Override
    public void run() { children.forEach(Runnable::run); }
}

现在,您需要按如下方式使用您的信息流:

CloseHandler closeAll = new CloseHandler();
try (Stream<Something> stream = list.stream().onClose(closeAll)) {
    // Now collect
    stream.collect(Collectors.groupingBy(
        this::extractFileName, 
        toFile(closeAll)));
}

这使用try-with-resources构造,以便在使用或发生错误时自动关闭流。请注意,我们将closeAll关闭处理程序传递给Stream.onClose方法。

这是下游收集器的草图,它将收集/写入/发送元素到Closeable资源(注意我们也将closeAll关闭处理程序传递给它:)

static Collector<Something, ?, Void> toFile(CloseHandler closeAll) {

    class Acc {

        SomeResource resource; // this is your closeable resource

        Acc() {
            try {
                resource = new SomeResource(...); // create closeable resource
                closeAll.add(this::close);        // this::close is a Runnable
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        void add(Something elem) {
            try {
                // TODO write/send to closeable resource here
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        Acc merge(Acc another) {
            // TODO left as an exercise
        }

        // This is the close handler for this particular closeable resource
        private void close() {
            try {
                // Here we close our closeable resource
                if (resource != null) resource.close();
            } catch (IOException ignored) {
            }
        }
    }
    return Collector.of(Acc::new, Acc::add, Acc::merge, a -> null);
}

因此,它使用本地类(名为Acc)来包装可关闭资源,并将方法声明到add流的元素到可关闭资源,并且merge两个Acc实例,以防流并行(作为练习,如果值得付出努力)。

Collector.of用于基于Acc类'方法创建一个收集器,其中一个返回null的终结器,因为我们不想在地图中创建任何内容Collectors.groupingBy

最后,有close方法,它会在已创建的情况下关闭包装的可关闭资源。

当通过try-with-resources构造隐式关闭流时,CloseHandler.run方法将自动执行,这将依次执行先前在{{1}时添加的所有子关闭处理程序实例已创建。

答案 1 :(得分:0)

好的,我已经看了Collectors实现,你需要CollectorImpl来创建自定义收集器,但它不公开。所以我使用它的副本(你可能感兴趣的最后2个方法)来实现新的:

public class CollectorUtils<T, A, R> implements Collector<T, A, R> {

    static final Set<Collector.Characteristics> CH_ID = Collections
            .unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));

    private final Supplier<A> supplier;
    private final BiConsumer<A, T> accumulator;
    private final BinaryOperator<A> combiner;
    private final Function<A, R> finisher;
    private final Set<Characteristics> characteristics;

    CollectorUtils(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
            Function<A, R> finisher, Set<Characteristics> characteristics) {
        this.supplier = supplier;
        this.accumulator = accumulator;
        this.combiner = combiner;
        this.finisher = finisher;
        this.characteristics = characteristics;
    }

    CollectorUtils(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
            Set<Characteristics> characteristics) {
        this(supplier, accumulator, combiner, castingIdentity(), characteristics);
    }

    @Override
    public BiConsumer<A, T> accumulator() {
        return accumulator;
    }

    @Override
    public Supplier<A> supplier() {
        return supplier;
    }

    @Override
    public BinaryOperator<A> combiner() {
        return combiner;
    }

    @Override
    public Function<A, R> finisher() {
        return finisher;
    }

    @Override
    public Set<Characteristics> characteristics() {
        return characteristics;
    }

    @SuppressWarnings("unchecked")
    private static <I, R> Function<I, R> castingIdentity() {
        return i -> (R) i;
    }

    public static <C extends Collection<File>> Collector<String, ?, C> toFile() {
        return new CollectorUtils<>((Supplier<List<File>>) ArrayList::new, (c, t) -> {
            c.add(toFile(t));
        }, (r1, r2) -> {
            r1.addAll(r2);
            return r1;
        }, CH_ID);
    }

    private static File toFile(String fileName) {
        try (Closeable type = () -> System.out.println("Complete! closing file " + fileName);) {
            // stuff
            System.out.println("Converting " + fileName);

            return new File(fileName);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        throw new RuntimeException("Failed to create file");

    }

}

然后我按如下方式调用流:

public static void main(String[] args) {
        Stream.of("x.txt", "y.txt","z.txt").collect(CollectorUtils.toFile());
    }

输出:

Convertingx.txt
closing filex.txt
Convertingy.txt
closing filey.txt
Convertingz.txt
closing filez.txt