Bug 572228 - [DataBinding] Provide convert(Function) for IValueProperty
Summary: [DataBinding] Provide convert(Function) for IValueProperty
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.20   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-23 13:01 EDT by Christoph Laeubrich CLA
Modified: 2022-01-09 08:05 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christoph Laeubrich CLA 2021-03-23 13:01:00 EDT
IValueProperty should contain a (default implemented) method

IValueProperty#convert(Function<T, C> conversionFunction)

that return a IValueProperty that is the result of applying the given conversion function to it.

Similar to Streams/Optionals this would allow to take a basic property, modify it and the operate on the converted value.
Comment 1 Jens Lideström CLA 2021-12-29 11:45:28 EST
(In reply to Christoph Laeubrich from comment #0)
> IValueProperty should contain a (default implemented) method
> 
> IValueProperty#convert(Function<T, C> conversionFunction)
> 
> that return a IValueProperty that is the result of applying the given
> conversion function to it.

This is the same functionality as the following code:

var convertedProp = valueProp.value(Properties.value(convertFunc));


So it is pretty easy to achieve this already.

Maybe it would be a good idea to add a shortcut to do this directly on IValueProperty, maybe not...

If so, there has to be methods added to IListProperty, ISetProperty and IMapProperty also.

On the one hand it is nice with convenient shortcuts, on the other hand it is good to keep the top level property interfaces simple...
Comment 2 Christoph Laeubrich CLA 2021-12-29 11:53:09 EST
(In reply to Jens Lideström from comment #1)
> This is the same functionality as the following code:
> 
> var convertedProp = valueProp.value(Properties.value(convertFunc));

org.eclipse.core.databinding.property.Properties does not contain such a method, do you mean a different class?
Comment 3 Jens Lideström CLA 2021-12-29 12:10:39 EST
Oh, sorry my mistake, I got the wrong method name!

The method I mean is Properties.convertedValue.

(eclipse.core.databinding.property.Properties)
Comment 4 Christoph Laeubrich CLA 2021-12-30 01:32:09 EST
(In reply to Jens Lideström from comment #3)
> Oh, sorry my mistake, I got the wrong method name!
> 
> The method I mean is Properties.convertedValue.
> 
> (eclipse.core.databinding.property.Properties)

Thanks for clarification, this seems to work but is far from obvious from a users point of view.

adding

> /**
> * Returns an {@link IValueProperty} whose value results from applying the given
> * conversion function on this value property. Setting a value on the property
> * is not supported.
> *
> * @param converter converter to apply to the source object of the value
> *                  property; not null
> * @return new instance of a value property, whose value is the result of
> *         applying the given converter to this value property.
> * @since 1.9
> */
> default <C> IValueProperty<S, C> converted(Function<? super T, C> converter) {
>	return value(Properties.convertedValue(converter));
> }

won't hurt much and would give much more clearer access to this function without adding to the burden of implementors.
Comment 5 Jens Lideström CLA 2021-12-31 06:22:47 EST
Having thought about this a little I think it is a good idea!

Do you want to provide an implementation and tests for values, lists and sets?

I posted a suggestion to make IConverter into a functional interface: Bug 578006

If we do that we can use IConverter in the interface here.
Comment 6 Eclipse Genie CLA 2022-01-09 07:54:51 EST
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/189418
Comment 7 Jens Lideström CLA 2022-01-09 08:05:18 EST
I pushed a prototype implementation of this kinds of converter methods.

I still don't like the extra methods on the top-level property interfaces... But they seem to be very convenient and pretty for clients!

Using these shortcuts you can make properties for nested values like this:

IObservableValue<String> value = Properties.convertedValue(Catalog:::getItem) //
    .value(CatalogItem::getContent) //
    .value(Content::getText) //
    .observeDetail(cat);