AppJars / commons-frontend

Commons utilities for front-end enterprise features
Apache License 2.0
1 stars 1 forks source link

Vaadin utils - compose value providers #4

Open javier-godoy opened 2 years ago

javier-godoy commented 2 years ago

ValueProvider extends SerializableFunction. However, one cannot use Function.compose because the return type of Function.compose is Function instead of ValueProvider

Consider the following helper method:

public static <R, T, V> ValueProvider<T, V> compose(final ValueProvider<? super T, ? extends R> this_,
            final ValueProvider<? super R, ? extends V> after) {
        Objects.requireNonNull(this_);
        Objects.requireNonNull(after);
        return (T t) -> Optional.ofNullable(this_.apply(t)).map(after).orElse(null);
}

it allows you to specifiy a ValueProvider by composing method references grid.addColumn(compose(Person::getAddress, Address::getCity)) instead of grid.addColumn(p->Optional.ofNullable(p.getAddress()).map(Address::getCity))


This contribution contains Personal Intellectual Property that is and remains owned by its Author. To the fullest extent possible, the Appjars Project and its controlling shareholder are granted a non-exclusive, irrevocable and transferable license under the terms stated in section 1(D) of the Amendment to the Agreement on Intellectual Property in effect.