]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add ValueProvider interface, use where appropriate
authorAleksi Hietanen <aleksi@vaadin.com>
Thu, 15 Dec 2016 08:47:32 +0000 (10:47 +0200)
committerIlia Motornyi <elmot@vaadin.com>
Thu, 15 Dec 2016 08:47:32 +0000 (10:47 +0200)
server/src/main/java/com/vaadin/data/BeanBinder.java
server/src/main/java/com/vaadin/data/Binder.java
server/src/main/java/com/vaadin/data/HasValue.java
server/src/main/java/com/vaadin/data/StatusChangeEvent.java
server/src/main/java/com/vaadin/data/ValueProvider.java [new file with mode: 0644]
server/src/main/java/com/vaadin/ui/DeclarativeValueProvider.java
server/src/main/java/com/vaadin/ui/Grid.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridDefaultHeaderTest.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridHeaderFooterTest.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java

index 690c2eb849abbe7955778af64a5706b18de7fa6a..56219ff6bf6820df40c0b683bc1089e6ec837c44 100644 (file)
@@ -160,7 +160,7 @@ public class BeanBinder<BEAN> extends Binder<BEAN> {
          * @throws IllegalArgumentException
          *             if the property has no accessible getter
          *
-         * @see BindingBuilder#bind(SerializableFunction,
+         * @see BindingBuilder#bind(ValueProvider,
          *      SerializableBiConsumer)
          */
         public Binding<BEAN, TARGET> bind(String propertyName);
@@ -361,7 +361,7 @@ public class BeanBinder<BEAN> extends Binder<BEAN> {
      * @throws IllegalArgumentException
      *             if the property has no accessible getter
      *
-     * @see #bind(HasValue, SerializableFunction, SerializableBiConsumer)
+     * @see #bind(HasValue, ValueProvider, SerializableBiConsumer)
      */
     public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(
             HasValue<FIELDVALUE> field, String propertyName) {
index 2d0ec613e0cf14db91945865a15c4c839730a238..9f496d04d26b5c31720e857e990617122849d917 100644 (file)
@@ -172,7 +172,7 @@ public class Binder<BEAN> implements Serializable {
          *             if {@code bind} has already been called on this binding
          */
         public Binding<BEAN, TARGET> bind(
-                SerializableFunction<BEAN, TARGET> getter,
+                ValueProvider<BEAN, TARGET> getter,
                 com.vaadin.server.SerializableBiConsumer<BEAN, TARGET> setter);
 
         /**
@@ -255,7 +255,7 @@ public class Binder<BEAN> 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.
          * <p>
          * For instance, a {@code TextField} can be bound to an integer-typed
@@ -281,7 +281,7 @@ public class Binder<BEAN> 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.
          * <p>
          * For instance, a {@code TextField} can be bound to an integer-typed
@@ -316,7 +316,7 @@ public class Binder<BEAN> 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.
          * <p>
          * For instance, a {@code TextField} can be bound to an integer-typed
@@ -533,7 +533,7 @@ public class Binder<BEAN> implements Serializable {
 
         @Override
         public Binding<BEAN, TARGET> bind(
-                SerializableFunction<BEAN, TARGET> getter,
+                ValueProvider<BEAN, TARGET> getter,
                 SerializableBiConsumer<BEAN, TARGET> setter) {
             checkUnbound();
             Objects.requireNonNull(getter, "getter cannot be null");
@@ -976,7 +976,7 @@ public class Binder<BEAN> 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.
      * <p>
@@ -993,7 +993,7 @@ public class Binder<BEAN> implements Serializable {
      *            the field to be bound, not null
      * @return the new binding
      *
-     * @see #bind(HasValue, SerializableFunction, SerializableBiConsumer)
+     * @see #bind(HasValue, ValueProvider, SerializableBiConsumer)
      */
     public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forField(
             HasValue<FIELDVALUE> field) {
@@ -1060,7 +1060,7 @@ public class Binder<BEAN> implements Serializable {
      */
     public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(
             HasValue<FIELDVALUE> field,
-            SerializableFunction<BEAN, FIELDVALUE> getter,
+            ValueProvider<BEAN, FIELDVALUE> getter,
             SerializableBiConsumer<BEAN, FIELDVALUE> setter) {
         return forField(field).bind(getter, setter);
     }
@@ -1473,7 +1473,7 @@ public class Binder<BEAN> implements Serializable {
      * <li>{@link #setBean(Object)} is called
      * <li>{@link #removeBean()} is called
      * <li>
-     * {@link BindingBuilder#bind(SerializableFunction, SerializableBiConsumer)}
+     * {@link BindingBuilder#bind(ValueProvider, SerializableBiConsumer)}
      * is called
      * <li>{@link Binder#validate()} or {@link Binding#validate()} is called
      * </ul>
index fe5820839c375f6a71b15a2e1a8f75890603c7b5..22cc1c30bc964f8bb3acd4078ba090ed168afe03 100644 (file)
@@ -200,7 +200,7 @@ public interface HasValue<V> 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;
index ded3ed79556a3d7ee462f669f72ba1bbb5a9294d..2c4e87d897734339265a6205593dbe02da222d50 100644 (file)
@@ -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;
  * <li>{@link Binder#readBean(Object)} is called
  * <li>{@link Binder#setBean(Object)} is called
  * <li>{@link Binder#removeBean()} is called
- * <li>{@link BindingBuilder#bind(SerializableFunction, SerializableBiConsumer)}
+ * <li>{@link BindingBuilder#bind(ValueProvider, SerializableBiConsumer)}
  * is called
  * <li>{@link Binder#validate()} or {@link Binding#validate()} is called
  * </ul>
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 (file)
index 0000000..9e4acc4
--- /dev/null
@@ -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.
+ * <p>
+ * 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 <SOURCE>
+ *            the type of the object used to provide the value
+ * @param <TARGET>
+ *            the type of the provided value
+ */
+@FunctionalInterface
+public interface ValueProvider<SOURCE, TARGET>
+        extends SerializableFunction<SOURCE, TARGET> {
+
+    /**
+     * Returns a value provider that always returns its input argument.
+     *
+     * @param <T>
+     *            the type of the input and output objects to the function
+     * @return a function that always returns its input argument
+     */
+    public static <T> ValueProvider<T, T> 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);
+}
index 9e2975b6378595844fc3849b8678ecac7c39696d..e769e7fd94bc3fe231e876a37f7a7d4f2c3d1a19 100644 (file)
@@ -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 <T>
  *            item type
  */
-class DeclarativeValueProvider<T> implements SerializableFunction<T, String> {
+class DeclarativeValueProvider<T> implements ValueProvider<T, String> {
 
     private final Map<T, String> values = new IdentityHashMap<>();
 
index f528221e482f9010efd322b711c08f21c92934b6..e8c621d8775f2715331dea16170c578e8de70bcd 100644 (file)
@@ -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<T> extends AbstractListing<T>
          *            the type of value
          */
         protected Column(String caption,
-                SerializableFunction<T, ? extends V> valueProvider,
+                ValueProvider<T, ? extends V> valueProvider,
                 Renderer<V> renderer) {
             Objects.requireNonNull(caption, "Header caption can't be null");
             Objects.requireNonNull(valueProvider,
@@ -2448,7 +2449,7 @@ public class Grid<T> extends AbstractListing<T>
      * @see AbstractRenderer
      */
     public <V> Column<T, V> addColumn(String identifier,
-            SerializableFunction<T, ? extends V> valueProvider,
+            ValueProvider<T, ? extends V> valueProvider,
             AbstractRenderer<? super T, V> renderer)
             throws IllegalArgumentException {
         if (columnKeys.containsKey(identifier)) {
@@ -2477,7 +2478,7 @@ public class Grid<T> extends AbstractListing<T>
      *             if the same identifier is used for multiple columns
      */
     public Column<T, String> addColumn(String identifier,
-            SerializableFunction<T, String> valueProvider) {
+            ValueProvider<T, String> valueProvider) {
         return addColumn(identifier, valueProvider, new TextRenderer());
     }
 
@@ -2494,7 +2495,7 @@ public class Grid<T> extends AbstractListing<T>
      * @return the new column
      */
     public Column<T, String> addColumn(
-            SerializableFunction<T, ?> valueProvider) {
+            ValueProvider<T, String> valueProvider) {
         return addColumn(getGeneratedIdentifier(),
                 t -> String.valueOf(valueProvider.apply(t)),
                 new TextRenderer());
@@ -2516,7 +2517,7 @@ public class Grid<T> extends AbstractListing<T>
      * @see AbstractRenderer
      */
     public <V> Column<T, V> addColumn(
-            SerializableFunction<T, ? extends V> valueProvider,
+            ValueProvider<T, ? extends V> valueProvider,
             AbstractRenderer<? super T, V> renderer) {
         return addColumn(getGeneratedIdentifier(), valueProvider, renderer);
     }
index 0d677a8f04f13e2dbd0c3a2de13ff936489ef0f8..b2ce1abd2fa99ac2beaa921b58057c6e2053f3b8 100644 (file)
@@ -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
index 1309297e36e25c83bce5c246cf9c5ae72a8e4b02..055d4712cc6e8a092d2c1cff8be1e30dcd8c7a21 100644 (file)
@@ -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<String, ?> column = grid.addColumn("Col",
-                SerializableFunction.identity());
+                ValueProvider.identity());
         grid.removeColumn(column);
 
         grid.getHeaderRow(0).getCell(column);
index d477a731cf94cc04e70a72ffe42dfd1960095151..56e8ad29f33ec885ab18fcc7a29fc8ed49aa3367 100644 (file)
@@ -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
index 8f92e431c42f6736f5b3152fccf9f880d9952858..72d5ade3105378145fccbca9d08d2f953987f4ca 100644 (file)
@@ -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<String> grid = new Grid<>();
 
-        grid.addColumn("Name", SerializableFunction.identity());
+        grid.addColumn("Name", ValueProvider.identity());
 
         List<String> data = new ArrayList<>();
         for (int i = 0; i < 1000; i++) {