You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by da...@apache.org on 2016/11/11 15:30:19 UTC
[1/3] wicket git commit: Makes LoadableDetachableModel factory return
LDM
Repository: wicket
Updated Branches:
refs/heads/master 3165f34f0 -> fbd6615eb
Makes LoadableDetachableModel factory return LDM
This way users can utilize LDM#isAttached() if needed without having to use a
cast.
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/331fc689
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/331fc689
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/331fc689
Branch: refs/heads/master
Commit: 331fc68979e87d43de12a7c24012d9db8dc83e9f
Parents: 3165f34
Author: Martijn Dashorst <ma...@topicus.nl>
Authored: Fri Nov 11 16:13:53 2016 +0100
Committer: Martijn Dashorst <ma...@topicus.nl>
Committed: Fri Nov 11 16:28:00 2016 +0100
----------------------------------------------------------------------
.../java/org/apache/wicket/model/LoadableDetachableModel.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/331fc689/wicket-core/src/main/java/org/apache/wicket/model/LoadableDetachableModel.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/model/LoadableDetachableModel.java b/wicket-core/src/main/java/org/apache/wicket/model/LoadableDetachableModel.java
index d4fcd5e..0c294d0 100644
--- a/wicket-core/src/main/java/org/apache/wicket/model/LoadableDetachableModel.java
+++ b/wicket-core/src/main/java/org/apache/wicket/model/LoadableDetachableModel.java
@@ -200,9 +200,9 @@ public abstract class LoadableDetachableModel<T> implements IModel<T>
*
* @param <T>
* @param getter Used for the getObject() method.
- * @return Model
+ * @return the model
*/
- public static <T> IModel<T> of(SerializableSupplier<T> getter)
+ public static <T> LoadableDetachableModel<T> of(SerializableSupplier<T> getter)
{
return new LoadableDetachableModel<T>()
{
[2/3] wicket git commit: Adds read-only Lambda.of(supplier) factory
method
Posted by da...@apache.org.
Adds read-only Lambda.of(supplier) factory method
And applies the formatting as specified in our coding style.
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/e91599b5
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/e91599b5
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/e91599b5
Branch: refs/heads/master
Commit: e91599b50ffde994297b16505831aa67520c84b1
Parents: 331fc68
Author: Martijn Dashorst <ma...@topicus.nl>
Authored: Fri Nov 11 16:26:07 2016 +0100
Committer: Martijn Dashorst <ma...@topicus.nl>
Committed: Fri Nov 11 16:29:37 2016 +0100
----------------------------------------------------------------------
.../org/apache/wicket/model/LambdaModel.java | 141 ++++++++++++-------
1 file changed, 92 insertions(+), 49 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/e91599b5/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java b/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java
index 408b271..2e52330 100644
--- a/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java
+++ b/wicket-core/src/main/java/org/apache/wicket/model/LambdaModel.java
@@ -25,13 +25,14 @@ import org.danekja.java.util.function.serializable.SerializableFunction;
import org.danekja.java.util.function.serializable.SerializableSupplier;
/**
- * <code>LambdaModel</code> is a basic implementation of an <code>IModel</code>
- * that uses a serializable {@link SerializableSupplier} to get the object
- * and {@link SerializableConsumer} to set it.
+ * <code>LambdaModel</code> is a basic implementation of an <code>IModel</code> that uses a
+ * serializable {@link java.util.function.Supplier} to get the object and
+ * {@link java.util.function.Consumer} to set it.
*
* The {@link #detach()} method by default does nothing.
*
- * @param <T> The type of the Model Object
+ * @param <T>
+ * The type of the Model Object
*/
public class LambdaModel<T> implements IModel<T>
{
@@ -41,11 +42,13 @@ public class LambdaModel<T> implements IModel<T>
private final SerializableConsumer<T> setter;
/**
- * Construct the model, using the given supplier and consumer as
- * implementation for getObject and setObject.
+ * Construct the model, using the given supplier and consumer as implementation for getObject
+ * and setObject.
*
- * @param getter Used for the getObject() method.
- * @param setter Used for the setObject(T object) method.
+ * @param getter
+ * Used for the getObject() method.
+ * @param setter
+ * Used for the setObject(T object) method.
*/
public LambdaModel(SerializableSupplier<T> getter, SerializableConsumer<T> setter)
{
@@ -82,7 +85,7 @@ public class LambdaModel<T> implements IModel<T>
{
return false;
}
- final LambdaModel<?> other = (LambdaModel<?>) obj;
+ final LambdaModel<?> other = (LambdaModel<?>)obj;
if (!Objects.equals(this.getter, other.getter))
{
return false;
@@ -96,17 +99,21 @@ public class LambdaModel<T> implements IModel<T>
/**
* Create a {@link LambdaModel}. Usage:
+ *
* <pre>
* {@code
* LambdaModel.of(person::getName, person::setName)
* }
* </pre>
*
- * @param getter used to get value
- * @param setter used to set value
+ * @param getter
+ * used to get value
+ * @param setter
+ * used to set value
* @return model
*
- * @param <T> model object type
+ * @param <T>
+ * model object type
*/
public static <T> IModel<T> of(SerializableSupplier<T> getter, SerializableConsumer<T> setter)
{
@@ -114,18 +121,48 @@ public class LambdaModel<T> implements IModel<T>
}
/**
+ * Create a read-only {@link IModel}. Usage:
+ *
+ * <pre>
+ * {@code
+ * LambdaModel.of(person::getName)
+ * }
+ * </pre>
+ *
+ * Note that {@link IModel} is a {@code FunctionalInterface} and you can also use a lambda
+ * directly as a model.
+ *
+ * @param getter
+ * used to get value
+ * @return model
+ *
+ * @param <T>
+ * model object type
+ */
+ public static <T> IModel<T> of(SerializableSupplier<T> getter)
+ {
+ return getter::get;
+ }
+
+ /**
* Create a {@link LambdaModel} for a given target. Usage:
+ *
* <pre>
* {@code
* LambdaModel.of(personModel, Person::getName)
* }
* </pre>
+ *
* The target model will be detached automatically.
*
- * @param target target for getter and setter
- * @param getter used to get a value
- * @param <X> target model object type
- * @param <T> model object type
+ * @param target
+ * target for getter and setter
+ * @param getter
+ * used to get a value
+ * @param <X>
+ * target model object type
+ * @param <T>
+ * model object type
*
* @return model
*/
@@ -134,25 +171,24 @@ public class LambdaModel<T> implements IModel<T>
Args.notNull(target, "target");
Args.notNull(getter, "getter");
- return new LambdaModel<T>(
- () ->
+ return new LambdaModel<T>(() -> {
+ X x = target.getObject();
+ if (x == null)
{
- X x = target.getObject();
- if (x == null) {
- return null;
- }
- return getter.apply(x);
- },
+ return null;
+ }
+ return getter.apply(x);
+ },
- (t) ->
- {
+ (t) -> {
throw new UnsupportedOperationException("setObject(Object) not supported");
- }
- ) {
+ })
+ {
private static final long serialVersionUID = 1L;
@Override
- public void detach() {
+ public void detach()
+ {
target.detach();
}
};
@@ -160,20 +196,27 @@ public class LambdaModel<T> implements IModel<T>
/**
* Create a {@link LambdaModel} for a given target. Usage:
+ *
* <pre>
* {@code
* LambdaModel.of(personModel, Person::getName, Person::setName)
* }
* </pre>
+ *
* The target model will be detached automatically.
*
- * @param target target for getter and setter
- * @param getter used to get a value
- * @param setter used to set a value
+ * @param target
+ * target for getter and setter
+ * @param getter
+ * used to get a value
+ * @param setter
+ * used to set a value
*
- * @param <X> target model object type
- * @param <T> model object type
-
+ * @param <X>
+ * target model object type
+ * @param <T>
+ * model object type
+ *
* @return model
*/
public static <X, T> IModel<T> of(IModel<X> target, SerializableFunction<X, T> getter,
@@ -183,28 +226,28 @@ public class LambdaModel<T> implements IModel<T>
Args.notNull(getter, "getter");
Args.notNull(setter, "setter");
- return new LambdaModel<T>(
- () ->
+ return new LambdaModel<T>(() -> {
+ X x = target.getObject();
+ if (x == null)
{
- X x = target.getObject();
- if (x == null) {
- return null;
- }
- return getter.apply(x);
- },
+ return null;
+ }
+ return getter.apply(x);
+ },
- (t) ->
- {
+ (t) -> {
X x = target.getObject();
- if (x != null) {
+ if (x != null)
+ {
setter.accept(x, t);
}
- }
- ) {
+ })
+ {
private static final long serialVersionUID = 1L;
@Override
- public void detach() {
+ public void detach()
+ {
target.detach();
}
};
[3/3] wicket git commit: Adds tests for serialization of lambda models
Posted by da...@apache.org.
Adds tests for serialization of lambda models
And applies code style formatting
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/fbd6615e
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/fbd6615e
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/fbd6615e
Branch: refs/heads/master
Commit: fbd6615eb1291c96ad3b39342e9483eaa87ac4d0
Parents: e91599b
Author: Martijn Dashorst <ma...@topicus.nl>
Authored: Fri Nov 11 16:27:37 2016 +0100
Committer: Martijn Dashorst <ma...@topicus.nl>
Committed: Fri Nov 11 16:29:37 2016 +0100
----------------------------------------------------------------------
.../org/apache/wicket/model/IModelTest.java | 71 ++++++++++++++------
1 file changed, 50 insertions(+), 21 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/fbd6615e/wicket-core/src/test/java/org/apache/wicket/model/IModelTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/model/IModelTest.java b/wicket-core/src/test/java/org/apache/wicket/model/IModelTest.java
index d76d117..c2efe29 100644
--- a/wicket-core/src/test/java/org/apache/wicket/model/IModelTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/model/IModelTest.java
@@ -18,8 +18,10 @@ package org.apache.wicket.model;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
+import org.apache.wicket.core.util.lang.WicketObjects;
import org.apache.wicket.model.lambda.Address;
import org.apache.wicket.model.lambda.Person;
import org.danekja.java.util.function.serializable.SerializableBiFunction;
@@ -51,8 +53,7 @@ public class IModelTest extends Assert
@Test
public void filterMatch()
{
- IModel<Person> johnModel = Model.of(person)
- .filter((p) -> p.getName().equals(name));
+ IModel<Person> johnModel = Model.of(person).filter((p) -> p.getName().equals(name));
assertThat(johnModel.getObject(), is(person));
}
@@ -60,8 +61,7 @@ public class IModelTest extends Assert
@Test
public void filterNoMatch()
{
- IModel<Person> johnModel = Model.of(person)
- .filter((p) -> p.getName().equals("Jane"));
+ IModel<Person> johnModel = Model.of(person).filter((p) -> p.getName().equals("Jane"));
assertThat(johnModel.getObject(), is(nullValue()));
}
@@ -82,7 +82,9 @@ public class IModelTest extends Assert
@Test
public void map2()
{
- IModel<String> streetModel = Model.of(person).map(Person::getAddress).map(Address::getStreet);
+ IModel<String> streetModel = Model.of(person)
+ .map(Person::getAddress)
+ .map(Address::getStreet);
assertThat(streetModel.getObject(), is(equalTo(street)));
}
@@ -96,9 +98,8 @@ public class IModelTest extends Assert
public void combineWith()
{
IModel<String> janeModel = Model.of("Jane");
- SerializableBiFunction<Person, String, String> function =
- (SerializableBiFunction<Person, String, String>) (person1, other) ->
- person1.getName() + " is in relationship with " + other;
+ SerializableBiFunction<Person, String, String> function = (SerializableBiFunction<Person, String, String>)(
+ person1, other) -> person1.getName() + " is in relationship with " + other;
IModel<String> relationShipModel = Model.of(person).combineWith(janeModel, function);
assertThat(relationShipModel.getObject(), is(equalTo("John is in relationship with Jane")));
}
@@ -107,9 +108,8 @@ public class IModelTest extends Assert
public void combineWithNullObject()
{
IModel<String> janeModel = Model.of((String)null);
- SerializableBiFunction<Person, String, String> function =
- (SerializableBiFunction<Person, String, String>) (person1, other) ->
- person1.getName() + " is in relationship with " + other;
+ SerializableBiFunction<Person, String, String> function = (SerializableBiFunction<Person, String, String>)(
+ person1, other) -> person1.getName() + " is in relationship with " + other;
IModel<String> relationShipModel = Model.of(person).combineWith(janeModel, function);
assertThat(relationShipModel.getObject(), is(nullValue()));
}
@@ -118,9 +118,8 @@ public class IModelTest extends Assert
public void combineWithNullModel()
{
IModel<String> janeModel = null;
- SerializableBiFunction<Person, String, String> function =
- (SerializableBiFunction<Person, String, String>) (person1, other) ->
- person1.getName() + " is in relationship with " + other;
+ SerializableBiFunction<Person, String, String> function = (SerializableBiFunction<Person, String, String>)(
+ person1, other) -> person1.getName() + " is in relationship with " + other;
Model.of(person).combineWith(janeModel, function);
}
@@ -134,12 +133,7 @@ public class IModelTest extends Assert
public void flatMap()
{
IModel<String> heirModel = Model.of(person)
- .flatMap(john ->
- LambdaModel.of(
- () -> john.getName() + " is my parent",
- john::setName
- )
- );
+ .flatMap(john -> LambdaModel.of(() -> john.getName() + " is my parent", john::setName));
assertThat(heirModel.getObject(), is(equalTo("John is my parent")));
String newValue = "Matthias";
@@ -168,7 +162,9 @@ public class IModelTest extends Assert
{
person.setName(null);
String defaultName = "Default name";
- IModel<String> defaultNameModel = Model.of(person).map(Person::getName).orElseGet(() -> defaultName);
+ IModel<String> defaultNameModel = Model.of(person)
+ .map(Person::getName)
+ .orElseGet(() -> defaultName);
assertThat(defaultNameModel.getObject(), is(equalTo(defaultName)));
}
@@ -178,4 +174,37 @@ public class IModelTest extends Assert
{
Model.of(person).map(Person::getName).orElseGet(null);
}
+
+ @Test
+ public void serializableMethodReference()
+ {
+ Person p = new Person();
+ IModel<String> m = p::getName;
+ assertThat(WicketObjects.cloneObject(m), is(not(nullValue())));
+ }
+
+ static class Account
+ {
+ private Person person = new Person();
+ {
+ person.setName("Some Name");
+ }
+
+ public Person getPerson()
+ {
+ return person;
+ }
+ }
+
+ @Test
+ public void serializableMethodChainReference()
+ {
+ IModel<Account> accountModel = LoadableDetachableModel.of(Account::new);
+ IModel<Person> personModel = accountModel.map(Account::getPerson);
+ IModel<String> nameModel = personModel.map(Person::getName);
+
+ IModel<String> clone = WicketObjects.cloneObject(nameModel);
+ assertThat(clone, is(not(nullValue())));
+ assertThat(clone.getObject(), is("Some Name"));
+ }
}