WellBehavedFX icon indicating copy to clipboard operation
WellBehavedFX copied to clipboard

Add `postProcess` from InputMapTemplateTest to `InputMapTemplate`

Open JordanMartinez opened this issue 9 years ago • 3 comments

See TomasMikula/RichTextFX#288

JordanMartinez avatar Apr 14 '16 23:04 JordanMartinez

So, I don't really get most of this code (so don't be surprised if this is way off), but by following the test as a model, I came up with this:

public static <S, T extends Event> InputMapTemplate<S, T> postProcess(
        InputMapTemplate<S, T> imt, BiConsumer<? extends S, ? extends T> postConsumption) {
    return new InputMapTemplate<S, T>() {
        @Override
        protected InputHandlerTemplateMap<S, T> getInputHandlerTemplateMap() {
            return imt.getInputHandlerTemplateMap().map(iht -> {
                return  (s, evt) -> {
                    Result res = iht.process(s, evt);
                    if (res == Result.CONSUME) {
                        // Here's the issue: 
                        //  's': capture<? extends S> cannot be applied to 'S'
                        //  'evt': capture<? extends T> cannot be applied to 'T' 
                        postConsumption.accept(s, evt);
                    }
                    return res;
                };
            });
        }
    };
}

If I change the BiConsumer<? extends S, ? extends T> to BiConsumer<S, T>, the above issue goes away, but I wasn't sure if that was correct or not.

JordanMartinez avatar Apr 14 '16 23:04 JordanMartinez

Looks good. The bounds on type parameters should be

<? super S, ? super T>

i.e. super instead of extends.

TomasMikula avatar Apr 15 '16 03:04 TomasMikula

Ok that's easy to fix (turns out the method below the place where I positioned this actually uses ? super S, ? super U as well... Should have known :smile: )

Edit: the test I wrote didn't work, but that was because I removed the lines that only applied postConsumption if res == Result.CONSUME. Now that these lines are back in place, the test runs fine.

JordanMartinez avatar Apr 15 '16 05:04 JordanMartinez