Repository: wicket Updated Branches: refs/heads/lambdas 6d7494022 -> ae70d7d02
some code for readonly models Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/ae70d7d0 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/ae70d7d0 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/ae70d7d0 Branch: refs/heads/lambdas Commit: ae70d7d02821695e165e3a36828792de8d21f14c Parents: 6d74940 Author: Michael Mosmann <[email protected]> Authored: Sun Nov 29 18:17:09 2015 +0100 Committer: Michael Mosmann <[email protected]> Committed: Sun Nov 29 18:17:09 2015 +0100 ---------------------------------------------------------------------- .../wicket/model/AbstractReadOnlyModel.java | 18 +----- .../org/apache/wicket/model/IReadOnlyModel.java | 36 +++++++++++ .../org/apache/wicket/model/lambda/Models.java | 63 ++++++++++++++++++++ .../wicket/model/lambda/LambdaModelTest.java | 28 +++++++++ .../apache/wicket/model/lambda/ModelsTest.java | 54 +++++++++++++++++ 5 files changed, 182 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/ae70d7d0/wicket-core/src/main/java/org/apache/wicket/model/AbstractReadOnlyModel.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/model/AbstractReadOnlyModel.java b/wicket-core/src/main/java/org/apache/wicket/model/AbstractReadOnlyModel.java index c7d9a5d..049d2bc 100644 --- a/wicket-core/src/main/java/org/apache/wicket/model/AbstractReadOnlyModel.java +++ b/wicket-core/src/main/java/org/apache/wicket/model/AbstractReadOnlyModel.java @@ -26,7 +26,7 @@ package org.apache.wicket.model; * @param <T> * The model object */ -public abstract class AbstractReadOnlyModel<T> implements IModel<T> +public abstract class AbstractReadOnlyModel<T> implements IReadOnlyModel<T> { private static final long serialVersionUID = 1L; @@ -37,22 +37,6 @@ public abstract class AbstractReadOnlyModel<T> implements IModel<T> public abstract T getObject(); /** - * This default implementation of setObject unconditionally throws an - * UnsupportedOperationException. Since the method is final, any subclass is effectively a - * read-only model. - * - * @param object - * The object to set into the model - * @throws UnsupportedOperationException - */ - @Override - public final void setObject(final T object) - { - throw new UnsupportedOperationException("Model " + getClass() + - " does not support setObject(Object)"); - } - - /** * {@inheritDoc} */ @Override http://git-wip-us.apache.org/repos/asf/wicket/blob/ae70d7d0/wicket-core/src/main/java/org/apache/wicket/model/IReadOnlyModel.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/model/IReadOnlyModel.java b/wicket-core/src/main/java/org/apache/wicket/model/IReadOnlyModel.java new file mode 100644 index 0000000..b4af8e0 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/model/IReadOnlyModel.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.model; + +public interface IReadOnlyModel<T> extends IModel<T> +{ + /** + * This default implementation of setObject unconditionally throws an + * UnsupportedOperationException. + * + * @param object + * The object to set into the model + * @throws UnsupportedOperationException + */ + @Override + @Deprecated + public default void setObject(final T object) + { + throw new UnsupportedOperationException("Model " + getClass() + + " does not support setObject(Object)"); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/ae70d7d0/wicket-core/src/main/java/org/apache/wicket/model/lambda/Models.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/model/lambda/Models.java b/wicket-core/src/main/java/org/apache/wicket/model/lambda/Models.java new file mode 100644 index 0000000..3f457a5 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/model/lambda/Models.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.model.lambda; + +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.IReadOnlyModel; + +public class Models +{ + public static <S, T> IModel<T> of(IModel<S> source, WicketFunction<S, WicketConsumer<T>> setter, WicketFunction<S, WicketSupplier<T>> getter) { + return new IModel<T>() { + @Override + public void detach() + { + source.detach(); + } + + @Override + public T getObject() + { + return getter.apply(source.getObject()).get(); + } + + @Override + public void setObject(T object) + { + setter.apply(source.getObject()).accept(object); + } + }; + } + + public static <S, T> IReadOnlyModel<T> of(IReadOnlyModel<S> source, WicketFunction<S, T> getter) { + return new IReadOnlyModel<T>() + { + + @Override + public void detach() + { + source.detach(); + } + + @Override + public T getObject() + { + return getter.apply(source.getObject()); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/ae70d7d0/wicket-core/src/test/java/org/apache/wicket/model/lambda/LambdaModelTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/model/lambda/LambdaModelTest.java b/wicket-core/src/test/java/org/apache/wicket/model/lambda/LambdaModelTest.java index 8bf21ae..b8e64f9 100644 --- a/wicket-core/src/test/java/org/apache/wicket/model/lambda/LambdaModelTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/model/lambda/LambdaModelTest.java @@ -88,4 +88,32 @@ public class LambdaModelTest assertThat(clone.getObject(), is(personName)); } + + static class Foo<T> { + + public void callT(T instance) { + + } + + static <T> Foo<T> of(Class<? extends T> type) { + return new Foo<>(); + } + } + + @Test + public void testMe() { + String s="foo"; + Class<? extends String> class1 = s.getClass(); + Foo<String> foo = Foo.of(class1); + foo.callT(s); + + + } + + static <X> void again(X s, Class<X> clazz) { + Class<? extends X> class1 = clazz; + s.getClass(); + Foo<X> foo = Foo.of(class1); + foo.callT(s); + } } http://git-wip-us.apache.org/repos/asf/wicket/blob/ae70d7d0/wicket-core/src/test/java/org/apache/wicket/model/lambda/ModelsTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/model/lambda/ModelsTest.java b/wicket-core/src/test/java/org/apache/wicket/model/lambda/ModelsTest.java new file mode 100644 index 0000000..b77b8c7 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/model/lambda/ModelsTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.model.lambda; + +import static org.junit.Assert.*; + +import java.io.Serializable; + +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; +import org.junit.Test; + +public class ModelsTest +{ + @Test + public void propertyModel() { + Foo instance = new Foo(); + Model<Foo> fooModel = Model.of(instance); + IModel<String> nameModel = Models.of(fooModel, (x) -> x::setName, (x) -> x::getName); + instance.setName("blub"); + assertEquals("blub", nameModel.getObject()); + nameModel.setObject("bar"); + assertEquals("bar", instance.getName()); + } + + static class Foo implements Serializable { + + String name; + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + } +}
