Discussion:
LambdaModel use
Илья Нарыжный
2018-11-09 07:30:21 UTC
Permalink
Hello,

How it was expected to use LambdaModel with setters?
Right now it seems to be not quite usable, because, of the following code
inside:

@Override
public void setObject(R r)
{
X x = target.getObject();
if (x != null)
{
setter.accept(x, r);
}
}

As you can see, if previous value of target model is null, there is no way
to change it. Why?
And also it will be interesting to know why SerializableBiConsumer was used
for a setter?

Thanks,
Ilia

---------------------------------------------
Orienteer(http://orienteer.org) - open source Business Application Platform
Bas Gooren
2018-11-09 07:41:43 UTC
Permalink
Hi,

The LambdaModel method you are referring to is dependant on another model:

public static <X, R> IModel<R> of(IModel<X> target, SerializableFunction<X,
R> getter, SerializableBiConsumer<X, R> setter)

Since the setter depends on the target model, the target model object needs
to be non-null for the setter to be able to do anything (you can’t set a
property on a null object reference).

If I look at the current master branch [1], I see that the functionality
you probably are looking for is present:

public static <R> IModel<R> of(SerializableSupplier<R> getter,
SerializableConsumer<R> setter)


 which is simply a model which delegates getting and setting the value to
the specified supplier and consumer, without having a target model.

1)
https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 9 november 2018 bij 08:30:39, Илья НарыжМый (***@ydn.ru) schreef:

Hello,

How it was expected to use LambdaModel with setters?
Right now it seems to be not quite usable, because, of the following code
inside:

@Override
public void setObject(R r)
{
X x = target.getObject();
if (x != null)
{
setter.accept(x, r);
}
}

As you can see, if previous value of target model is null, there is no way
to change it. Why?
And also it will be interesting to know why SerializableBiConsumer was used
for a setter?

Thanks,
Ilia

---------------------------------------------
Orienteer(http://orienteer.org) - open source Business Application Platform
Илья Нарыжный
2018-11-10 23:42:25 UTC
Permalink
Thank you, Bas!
Fully agree with you that if setters is to set something on a main target
object. In my particular case, I tried to use LambdaModel for "conversion"
between one type and another. So targetModel contain original object and
LambdaModel just wrap and convert correspondingly. Also I tried to use
exactly that method, because if targetModel is provided to LambdaModel - it
manages lifecycle and invoke onDetach when it's applicable. Use of `public
static <R> IModel<R> of(SerializableSupplier<R> getter,
SerializableConsumer<R> setter)` is not so convenient in this case, because
target model is out of required lifecycle.

Thanks,
Ilia

---------------------------------------------
Orienteer(http://orienteer.org) - open source Business Application Platform
Post by Bas Gooren
Hi,
public static <X, R> IModel<R> of(IModel<X> target,
SerializableFunction<X, R> getter, SerializableBiConsumer<X, R> setter)
Since the setter depends on the target model, the target model object
needs to be non-null for the setter to be able to do anything (you can’t
set a property on a null object reference).
If I look at the current master branch [1], I see that the functionality
public static <R> IModel<R> of(SerializableSupplier<R> getter,
SerializableConsumer<R> setter)

 which is simply a model which delegates getting and setting the value to
the specified supplier and consumer, without having a target model.
1)
https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java
Met vriendelijke groet,
Kind regards,
Bas Gooren
Hello,
How it was expected to use LambdaModel with setters?
Right now it seems to be not quite usable, because, of the following code
@Override
public void setObject(R r)
{
X x = target.getObject();
if (x != null)
{
setter.accept(x, r);
}
}
As you can see, if previous value of target model is null, there is no way
to change it. Why?
And also it will be interesting to know why SerializableBiConsumer was used
for a setter?
Thanks,
Ilia
---------------------------------------------
Orienteer(http://orienteer.org) - open source Business Application Platform
Bas Gooren
2018-11-14 20:34:05 UTC
Permalink
Hi!


Well, that’s a different use-case :-)


We have written a specialized ConversionModel for the exact same reason:
convert an underlying model to a target type.

And of course a specialization of that called LambdaConversionModel, which
also allows conversion of null values.

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 11 november 2018 bij 00:42:38, Илья НарыжМый (***@ydn.ru) schreef:

Thank you, Bas!
Fully agree with you that if setters is to set something on a main target
object. In my particular case, I tried to use LambdaModel for "conversion"
between one type and another. So targetModel contain original object and
LambdaModel just wrap and convert correspondingly. Also I tried to use
exactly that method, because if targetModel is provided to LambdaModel - it
manages lifecycle and invoke onDetach when it's applicable. Use of `public
static <R> IModel<R> of(SerializableSupplier<R> getter,
SerializableConsumer<R> setter)` is not so convenient in this case, because
target model is out of required lifecycle.

Thanks,
Ilia

---------------------------------------------
Orienteer(http://orienteer.org) - open source Business Application Platform
Post by Bas Gooren
Hi,
public static <X, R> IModel<R> of(IModel<X> target,
SerializableFunction<X, R> getter, SerializableBiConsumer<X, R> setter)
Since the setter depends on the target model, the target model object
needs to be non-null for the setter to be able to do anything (you can’t
set a property on a null object reference).
If I look at the current master branch [1], I see that the functionality
public static <R> IModel<R> of(SerializableSupplier<R> getter,
SerializableConsumer<R> setter)

 which is simply a model which delegates getting and setting the value to
the specified supplier and consumer, without having a target model.
1)
https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java
Met vriendelijke groet,
Kind regards,
Bas Gooren
Hello,
How it was expected to use LambdaModel with setters?
Right now it seems to be not quite usable, because, of the following code
@Override
public void setObject(R r)
{
X x = target.getObject();
if (x != null)
{
setter.accept(x, r);
}
}
As you can see, if previous value of target model is null, there is no way
to change it. Why?
And also it will be interesting to know why SerializableBiConsumer was used
for a setter?
Thanks,
Ilia
---------------------------------------------
Orienteer(http://orienteer.org) - open source Business Application Platform
Loading...