0

In the jdk8u source code for the ReduceOps class, lines 155-190 (reproduced below) constitute the makeRef method:

public static <T, I> TerminalOp<T, I>
makeRef(Collector<? super T, I, ?> collector) {
    Supplier<I> supplier = Objects.requireNonNull(collector).supplier();
    BiConsumer<I, ? super T> accumulator = collector.accumulator();
    BinaryOperator<I> combiner = collector.combiner();
    class ReducingSink extends Box<I>
            implements AccumulatingSink<T, I, ReducingSink> {
        @Override
        public void begin(long size) {
            state = supplier.get();
        }

        @Override
        public void accept(T t) {
            accumulator.accept(state, t);
        }

        @Override
        public void combine(ReducingSink other) {
            state = combiner.apply(state, other.state);
        }
    }
    return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }

        @Override
        public int getOpFlags() {
            return collector.characteristics().contains(Collector.Characteristics.UNORDERED)
                   ? StreamOpFlag.NOT_ORDERED
                   : 0;
        }
    };
}

I am asking explicitly about lines 177-189:

    return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }

        @Override
        public int getOpFlags() {
            return collector.characteristics().contains(Collector.Characteristics.UNORDERED)
                   ? StreamOpFlag.NOT_ORDERED
                   : 0;
        }
    };

What does the code after the return statement do (and why is it not unreachable)? I'm guessing it's doing something like overriding some methods from an interface called ReduceOp, but it appears that such an interface (if it exists) is not in the same place as many of the other relevant interfaces.

alphacapture
  • 109
  • 2
  • 1
    It's called an anonymous inner class, and it both declares and instantiates a subclass of `ReduceOp`. – Radiodef Jul 10 '18 at 02:42
  • 1
    @Radiodef, I think this question would have deserved an answer that explained how things work with regards to `ReduceOps`. @OP, `ReduceOps` has a private abstract class declared as `private static abstract class ReduceOp` which is what the code is referring to. You declare anonymous classes based on abstract classes or interfaces in the same way: with the `return new MyAbstractClass() {//implementation details}` format, and this can be inserted into any statement that evaluates to an instance of said interface/class. – Mick Mnemonic Jul 10 '18 at 02:52
  • @MickMnemonic I suppose I don't mind reopening it if you or somebody else wants to explain what the `ReduceOp` is doing, but it seems likely to me that it will be closed again by somebody else. – Radiodef Jul 10 '18 at 02:55
  • @Radiodef, hadn't actually looked at this class before, but the `makeX()` methods look like quite interesting examples of pairing a local class (`ReducingSink`) with an anonymous class (`ReduceOp` / `TerminalOp` implementation). This is something you don't encounter too often and might be worth explaining. Having said that, I don't think I can give a valuable explanation to the inner workings of the class. – Mick Mnemonic Jul 10 '18 at 03:05
  • @MickMnemonic, according to the comment of the OP on the answer, “_I was unaware that a statement could take that form at the time._”, I think the question is really “what is an anonymous class?”. I suppose you initially closed it as a duplicate of a similar question? I guess this was indeed correct. – Didier L Jul 17 '18 at 14:44
  • @DidierL I don't think this should be called a duplicate; for example, if someone had asked "Why can't I name a variable `true'?", would that be a duplicate of https://stackoverflow.com/questions/26194289/what-are-keywords-in-java ? Someone who has the former question would not be able to ask the latter. – alphacapture Jul 17 '18 at 19:07

0 Answers0