From 334fd4bcef760930651308ec94aeceaac6563c87 Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Thu, 15 Dec 2016 10:47:32 +0200 Subject: [PATCH] Add ValueProvider interface, use where appropriate --- .../main/java/com/vaadin/data/BeanBinder.java | 4 +- .../src/main/java/com/vaadin/data/Binder.java | 18 +++--- .../main/java/com/vaadin/data/HasValue.java | 2 +- .../com/vaadin/data/StatusChangeEvent.java | 3 +- .../java/com/vaadin/data/ValueProvider.java | 59 +++++++++++++++++++ .../vaadin/ui/DeclarativeValueProvider.java | 4 +- server/src/main/java/com/vaadin/ui/Grid.java | 11 ++-- .../component/grid/GridDefaultHeaderTest.java | 6 +- .../component/grid/GridHeaderFooterTest.java | 6 +- .../tests/server/component/grid/GridTest.java | 6 +- .../grid/GridApplyFilterWhenScrolledDown.java | 4 +- 11 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 server/src/main/java/com/vaadin/data/ValueProvider.java diff --git a/server/src/main/java/com/vaadin/data/BeanBinder.java b/server/src/main/java/com/vaadin/data/BeanBinder.java index 690c2eb849..56219ff6bf 100644 --- a/server/src/main/java/com/vaadin/data/BeanBinder.java +++ b/server/src/main/java/com/vaadin/data/BeanBinder.java @@ -160,7 +160,7 @@ public class BeanBinder extends Binder { * @throws IllegalArgumentException * if the property has no accessible getter * - * @see BindingBuilder#bind(SerializableFunction, + * @see BindingBuilder#bind(ValueProvider, * SerializableBiConsumer) */ public Binding bind(String propertyName); @@ -361,7 +361,7 @@ public class BeanBinder extends Binder { * @throws IllegalArgumentException * if the property has no accessible getter * - * @see #bind(HasValue, SerializableFunction, SerializableBiConsumer) + * @see #bind(HasValue, ValueProvider, SerializableBiConsumer) */ public Binding bind( HasValue field, String propertyName) { diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 2d0ec613e0..9f496d04d2 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -172,7 +172,7 @@ public class Binder implements Serializable { * if {@code bind} has already been called on this binding */ public Binding bind( - SerializableFunction getter, + ValueProvider getter, com.vaadin.server.SerializableBiConsumer setter); /** @@ -255,7 +255,7 @@ public class Binder implements Serializable { * which must match the current target data type of the binding, and a * model type, which can be any data type and becomes the new target * type of the binding. When invoking - * {@link #bind(SerializableFunction, SerializableBiConsumer)}, the + * {@link #bind(ValueProvider, SerializableBiConsumer)}, the * target type of the binding must match the getter/setter types. *

* For instance, a {@code TextField} can be bound to an integer-typed @@ -281,7 +281,7 @@ public class Binder implements Serializable { * type, which must match the current target data type of the binding, * and a model type, which can be any data type and becomes the new * target type of the binding. When invoking - * {@link #bind(SerializableFunction, SerializableBiConsumer)}, the + * {@link #bind(ValueProvider, SerializableBiConsumer)}, the * target type of the binding must match the getter/setter types. *

* For instance, a {@code TextField} can be bound to an integer-typed @@ -316,7 +316,7 @@ public class Binder implements Serializable { * type, which must match the current target data type of the binding, * and a model type, which can be any data type and becomes the new * target type of the binding. When invoking - * {@link #bind(SerializableFunction, SerializableBiConsumer)}, the + * {@link #bind(ValueProvider, SerializableBiConsumer)}, the * target type of the binding must match the getter/setter types. *

* For instance, a {@code TextField} can be bound to an integer-typed @@ -533,7 +533,7 @@ public class Binder implements Serializable { @Override public Binding bind( - SerializableFunction getter, + ValueProvider getter, SerializableBiConsumer setter) { checkUnbound(); Objects.requireNonNull(getter, "getter cannot be null"); @@ -976,7 +976,7 @@ public class Binder implements Serializable { /** * Creates a new binding for the given field. The returned builder may be * further configured before invoking - * {@link BindingBuilder#bind(SerializableFunction, SerializableBiConsumer)} + * {@link BindingBuilder#bind(ValueProvider, SerializableBiConsumer)} * which completes the binding. Until {@code Binding.bind} is called, the * binding has no effect. *

@@ -993,7 +993,7 @@ public class Binder implements Serializable { * the field to be bound, not null * @return the new binding * - * @see #bind(HasValue, SerializableFunction, SerializableBiConsumer) + * @see #bind(HasValue, ValueProvider, SerializableBiConsumer) */ public BindingBuilder forField( HasValue field) { @@ -1060,7 +1060,7 @@ public class Binder implements Serializable { */ public Binding bind( HasValue field, - SerializableFunction getter, + ValueProvider getter, SerializableBiConsumer setter) { return forField(field).bind(getter, setter); } @@ -1473,7 +1473,7 @@ public class Binder implements Serializable { *

  • {@link #setBean(Object)} is called *
  • {@link #removeBean()} is called *
  • - * {@link BindingBuilder#bind(SerializableFunction, SerializableBiConsumer)} + * {@link BindingBuilder#bind(ValueProvider, SerializableBiConsumer)} * is called *
  • {@link Binder#validate()} or {@link Binding#validate()} is called * diff --git a/server/src/main/java/com/vaadin/data/HasValue.java b/server/src/main/java/com/vaadin/data/HasValue.java index fe5820839c..22cc1c30bc 100644 --- a/server/src/main/java/com/vaadin/data/HasValue.java +++ b/server/src/main/java/com/vaadin/data/HasValue.java @@ -200,7 +200,7 @@ public interface HasValue extends Serializable { * values. Specific implementations might not support this. * * @return empty value - * @see Binder#bind(HasValue, java.util.function.Function, BiConsumer) + * @see Binder#bind(HasValue, ValueProvider, BiConsumer) */ public default V getEmptyValue() { return null; diff --git a/server/src/main/java/com/vaadin/data/StatusChangeEvent.java b/server/src/main/java/com/vaadin/data/StatusChangeEvent.java index ded3ed7955..2c4e87d897 100644 --- a/server/src/main/java/com/vaadin/data/StatusChangeEvent.java +++ b/server/src/main/java/com/vaadin/data/StatusChangeEvent.java @@ -20,7 +20,6 @@ import java.util.EventObject; import com.vaadin.data.Binder.Binding; import com.vaadin.data.Binder.BindingBuilder; import com.vaadin.server.SerializableBiConsumer; -import com.vaadin.server.SerializableFunction; /** * Binder status change event. @@ -33,7 +32,7 @@ import com.vaadin.server.SerializableFunction; *
  • {@link Binder#readBean(Object)} is called *
  • {@link Binder#setBean(Object)} is called *
  • {@link Binder#removeBean()} is called - *
  • {@link BindingBuilder#bind(SerializableFunction, SerializableBiConsumer)} + *
  • {@link BindingBuilder#bind(ValueProvider, SerializableBiConsumer)} * is called *
  • {@link Binder#validate()} or {@link Binding#validate()} is called * diff --git a/server/src/main/java/com/vaadin/data/ValueProvider.java b/server/src/main/java/com/vaadin/data/ValueProvider.java new file mode 100644 index 0000000000..9e4acc466f --- /dev/null +++ b/server/src/main/java/com/vaadin/data/ValueProvider.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed 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 com.vaadin.data; + +import com.vaadin.server.SerializableFunction; + +/** + * A callback interface for providing values from a given source. + *

    + * For example this interface can be implemented to simply extract a value with + * a getter, or to create a composite value based on the fields of the source + * object. + * + * @author Vaadin Ltd. + * @since 8.0 + * + * @param + * the type of the object used to provide the value + * @param + * the type of the provided value + */ +@FunctionalInterface +public interface ValueProvider + extends SerializableFunction { + + /** + * Returns a value provider that always returns its input argument. + * + * @param + * the type of the input and output objects to the function + * @return a function that always returns its input argument + */ + public static ValueProvider identity() { + return t -> t; + } + + /** + * Provides a value from the given source object. + * + * @param source + * the source to retrieve the value from + * @return the value provided by the source + */ + @Override + public TARGET apply(SOURCE source); +} diff --git a/server/src/main/java/com/vaadin/ui/DeclarativeValueProvider.java b/server/src/main/java/com/vaadin/ui/DeclarativeValueProvider.java index 9e2975b637..e769e7fd94 100644 --- a/server/src/main/java/com/vaadin/ui/DeclarativeValueProvider.java +++ b/server/src/main/java/com/vaadin/ui/DeclarativeValueProvider.java @@ -18,7 +18,7 @@ package com.vaadin.ui; import java.util.IdentityHashMap; import java.util.Map; -import com.vaadin.server.SerializableFunction; +import com.vaadin.data.ValueProvider; /** * Value provider class for declarative support. @@ -28,7 +28,7 @@ import com.vaadin.server.SerializableFunction; * @param * item type */ -class DeclarativeValueProvider implements SerializableFunction { +class DeclarativeValueProvider implements ValueProvider { private final Map values = new IdentityHashMap<>(); diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index f528221e48..e8c621d877 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -47,6 +47,7 @@ import com.vaadin.data.Binder; import com.vaadin.data.BinderValidationStatus; import com.vaadin.data.Listing; import com.vaadin.data.SelectionModel; +import com.vaadin.data.ValueProvider; import com.vaadin.event.ConnectorEvent; import com.vaadin.event.ContextClickEvent; import com.vaadin.event.SerializableEventListener; @@ -1005,7 +1006,7 @@ public class Grid extends AbstractListing * the type of value */ protected Column(String caption, - SerializableFunction valueProvider, + ValueProvider valueProvider, Renderer renderer) { Objects.requireNonNull(caption, "Header caption can't be null"); Objects.requireNonNull(valueProvider, @@ -2448,7 +2449,7 @@ public class Grid extends AbstractListing * @see AbstractRenderer */ public Column addColumn(String identifier, - SerializableFunction valueProvider, + ValueProvider valueProvider, AbstractRenderer renderer) throws IllegalArgumentException { if (columnKeys.containsKey(identifier)) { @@ -2477,7 +2478,7 @@ public class Grid extends AbstractListing * if the same identifier is used for multiple columns */ public Column addColumn(String identifier, - SerializableFunction valueProvider) { + ValueProvider valueProvider) { return addColumn(identifier, valueProvider, new TextRenderer()); } @@ -2494,7 +2495,7 @@ public class Grid extends AbstractListing * @return the new column */ public Column addColumn( - SerializableFunction valueProvider) { + ValueProvider valueProvider) { return addColumn(getGeneratedIdentifier(), t -> String.valueOf(valueProvider.apply(t)), new TextRenderer()); @@ -2516,7 +2517,7 @@ public class Grid extends AbstractListing * @see AbstractRenderer */ public Column addColumn( - SerializableFunction valueProvider, + ValueProvider valueProvider, AbstractRenderer renderer) { return addColumn(getGeneratedIdentifier(), valueProvider, renderer); } diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridDefaultHeaderTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridDefaultHeaderTest.java index 0d677a8f04..b2ce1abd2f 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridDefaultHeaderTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridDefaultHeaderTest.java @@ -22,7 +22,7 @@ import static org.junit.Assert.assertSame; import org.junit.Before; import org.junit.Test; -import com.vaadin.server.SerializableFunction; +import com.vaadin.data.ValueProvider; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.HeaderRow; @@ -35,8 +35,8 @@ public class GridDefaultHeaderTest { public void setUp() { grid = new Grid<>(); - column1 = grid.addColumn("First", SerializableFunction.identity()); - column2 = grid.addColumn("Second", SerializableFunction.identity()); + column1 = grid.addColumn("First", ValueProvider.identity()); + column2 = grid.addColumn("Second", ValueProvider.identity()); } @Test diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridHeaderFooterTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridHeaderFooterTest.java index 1309297e36..055d4712cc 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridHeaderFooterTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridHeaderFooterTest.java @@ -23,7 +23,7 @@ import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Test; -import com.vaadin.server.SerializableFunction; +import com.vaadin.data.ValueProvider; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.HeaderRow; @@ -140,7 +140,7 @@ public class GridHeaderFooterTest { @Test public void addColumn_headerCellAdded() { Column column = grid.addColumn("Col", - SerializableFunction.identity()); + ValueProvider.identity()); assertNotNull(grid.getHeaderRow(0).getCell(column)); } @@ -148,7 +148,7 @@ public class GridHeaderFooterTest { @Test(expected = IllegalArgumentException.class) public void removeColumn_headerCellRemoved() { Column column = grid.addColumn("Col", - SerializableFunction.identity()); + ValueProvider.identity()); grid.removeColumn(column); grid.getHeaderRow(0).getCell(column); diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java index d477a731cf..56e8ad29f3 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java @@ -11,8 +11,8 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.data.ValueProvider; import com.vaadin.event.selection.SelectionEvent; -import com.vaadin.server.SerializableFunction; import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.SelectionMode; @@ -25,9 +25,9 @@ public class GridTest { @Before public void setUp() { grid = new Grid<>(); - grid.addColumn("foo", SerializableFunction.identity()); + grid.addColumn("foo", ValueProvider.identity()); grid.addColumn(String::length, new NumberRenderer()); - grid.addColumn("randomColumnId", SerializableFunction.identity()); + grid.addColumn("randomColumnId", ValueProvider.identity()); } @Test diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java index 8f92e431c4..72d5ade310 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java @@ -3,7 +3,7 @@ package com.vaadin.tests.components.grid; import java.util.ArrayList; import java.util.List; -import com.vaadin.server.SerializableFunction; +import com.vaadin.data.ValueProvider; import com.vaadin.server.VaadinRequest; import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.AbstractTestUI; @@ -16,7 +16,7 @@ public class GridApplyFilterWhenScrolledDown extends AbstractTestUI { protected void setup(VaadinRequest request) { Grid grid = new Grid<>(); - grid.addColumn("Name", SerializableFunction.identity()); + grid.addColumn("Name", ValueProvider.identity()); List data = new ArrayList<>(); for (int i = 0; i < 1000; i++) { -- 2.39.5