ossgang / ossgang-commons

Apache License 2.0
4 stars 0 forks source link

Bidirectional Property-mapping/binding - is it desirable? - if so, how? #16

Open kaifox opened 5 years ago

kaifox commented 5 years ago

Thinking a bit about mapping (binding) ... Similar aspects apply to both, so I will focus on mapping for the moment.

At a first glance some operator on a property, like

<T, U> Property<U> mapBidi(Function<T,U> downstreamMapper, Function<U, T> upstreamMapper);

looks very tempting;-)

... however, very soon there arise questions, in particular when we think of the totally asynchronous concept, we applied ....

Consider, we have a property

Property<Double> dblProp;

If we now would e.g. have two derived properties:

Property<Double> negative = intProp.mapBidi(v -> -v; v -> -v);
Property<Double> tenfold = intProp.mapBidi(v -> 10.0*v; v -> v/10.0);

If we consider a starting value of 1.0 (negative=-1.0; tenfold=10.0) ... and then we would apply roughly at the same time, then it simply is random what the outcome is ....

Questions are: 1) Would this be accaptible? 2) Can we find a more predictive solution? 3) Could e.g. update-functions, instead of absolute functions help? e.g. something like:

<T, U> Property<U> mapBidi(BiFunction<T,U,U> downstreamMapper, BiFunction<U, T, T> upstreamMapper);

(where the second arg is always the current val of actual prop...) ... this of course would still need consequence of the user to implement the functions correctly....

... or could it be solved by always putting up- and downstream processing into one synchronize block ... (I did not fully think it through yet ....)

kaifox commented 5 years ago

... one thing we could probably also do is, to somehow test in the mapBidi() method, if the two given functions are the inverse of each other ...