From: Ilia Motornyi Date: Wed, 14 Dec 2016 14:29:41 +0000 (+0200) Subject: Miscellaneous Interface Refactoring X-Git-Tag: 8.0.0.alpha10~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7391e7cebf01665a58d0604144c6384ccc23356a;p=vaadin-framework.git Miscellaneous Interface Refactoring --- diff --git a/server/src/main/java/com/vaadin/data/Result.java b/server/src/main/java/com/vaadin/data/Result.java index e2e7c61e84..312701584f 100644 --- a/server/src/main/java/com/vaadin/data/Result.java +++ b/server/src/main/java/com/vaadin/data/Result.java @@ -16,12 +16,13 @@ package com.vaadin.data; +import com.vaadin.server.SerializableConsumer; +import com.vaadin.server.SerializableFunction; +import com.vaadin.server.SerializableSupplier; + import java.io.Serializable; import java.util.Objects; import java.util.Optional; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; /** * Represents the result of an operation that might fail, such as type @@ -79,8 +80,8 @@ public interface Result extends Serializable { * the function to provide the error message * @return the result of invoking the supplier */ - public static Result of(Supplier supplier, - Function onError) { + public static Result of(SerializableSupplier supplier, + SerializableFunction onError) { Objects.requireNonNull(supplier, "supplier cannot be null"); Objects.requireNonNull(onError, "onError cannot be null"); @@ -103,7 +104,7 @@ public interface Result extends Serializable { * the mapping function * @return the mapped result */ - default Result map(Function mapper) { + public default Result map(SerializableFunction mapper) { return flatMap(value -> ok(mapper.apply(value))); } @@ -119,7 +120,7 @@ public interface Result extends Serializable { * the mapping function * @return the mapped result */ - Result flatMap(Function> mapper); + public Result flatMap(SerializableFunction> mapper); /** * Invokes either the first callback or the second one, depending on whether @@ -130,7 +131,7 @@ public interface Result extends Serializable { * @param ifError * the function to call if failure */ - void handle(Consumer ifOk, Consumer ifError); + public void handle(SerializableConsumer ifOk, SerializableConsumer ifError); /** * Applies the {@code consumer} if result is not an error. @@ -138,7 +139,7 @@ public interface Result extends Serializable { * @param consumer * consumer to apply in case it's not an error */ - default void ifOk(Consumer consumer) { + public default void ifOk(SerializableConsumer consumer) { handle(consumer, error -> { }); } @@ -149,7 +150,7 @@ public interface Result extends Serializable { * @param consumer * consumer to apply in case it's an error */ - default void ifError(Consumer consumer) { + public default void ifError(SerializableConsumer consumer) { handle(value -> { }, consumer); } @@ -160,14 +161,14 @@ public interface Result extends Serializable { * @return true if the result denotes an error, * false otherwise */ - boolean isError(); + public boolean isError(); /** * Returns an Optional of the result message, or an empty Optional if none. * * @return the optional message */ - Optional getMessage(); + public Optional getMessage(); /** * Return the value, if the result denotes success, otherwise throw an @@ -182,6 +183,6 @@ public interface Result extends Serializable { * @throws X * if this result denotes an error */ - R getOrThrow( - Function exceptionProvider) throws X; + public R getOrThrow( + SerializableFunction exceptionProvider) throws X; } diff --git a/server/src/main/java/com/vaadin/data/SimpleResult.java b/server/src/main/java/com/vaadin/data/SimpleResult.java index 935fb545e3..61df0ca6f9 100644 --- a/server/src/main/java/com/vaadin/data/SimpleResult.java +++ b/server/src/main/java/com/vaadin/data/SimpleResult.java @@ -15,10 +15,11 @@ */ package com.vaadin.data; +import com.vaadin.server.SerializableConsumer; +import com.vaadin.server.SerializableFunction; + import java.util.Objects; import java.util.Optional; -import java.util.function.Consumer; -import java.util.function.Function; /** * An internal implementation of {@code Result}. @@ -53,7 +54,7 @@ class SimpleResult implements Result { @Override @SuppressWarnings("unchecked") - public Result flatMap(Function> mapper) { + public Result flatMap(SerializableFunction> mapper) { Objects.requireNonNull(mapper, "mapper cannot be null"); if (isError()) { @@ -65,7 +66,7 @@ class SimpleResult implements Result { } @Override - public void handle(Consumer ifOk, Consumer ifError) { + public void handle(SerializableConsumer ifOk, SerializableConsumer ifError) { Objects.requireNonNull(ifOk, "ifOk cannot be null"); Objects.requireNonNull(ifError, "ifError cannot be null"); if (isError()) { @@ -96,7 +97,7 @@ class SimpleResult implements Result { @Override public R getOrThrow( - Function exceptionSupplier) throws X { + SerializableFunction exceptionSupplier) throws X { Objects.requireNonNull(exceptionSupplier, "Exception supplier cannot be null"); if (isError()) { diff --git a/server/src/main/java/com/vaadin/server/AbstractClientConnector.java b/server/src/main/java/com/vaadin/server/AbstractClientConnector.java index 5c905e2f84..a9709f3826 100644 --- a/server/src/main/java/com/vaadin/server/AbstractClientConnector.java +++ b/server/src/main/java/com/vaadin/server/AbstractClientConnector.java @@ -808,7 +808,11 @@ public abstract class AbstractClientConnector * type eventType with one or more methods. * * @since 6.2 + * @deprecated use a {@link Registration} from + * {@link #addListener(Class, Object, Method)} to remove a + * listener */ + @Deprecated protected void removeListener(String eventIdentifier, Class eventType, Object target) { if (eventRouter != null) { @@ -874,7 +878,7 @@ public abstract class AbstractClientConnector *

* Note: Using this method is discouraged because it cannot be checked * during compilation. Use {@link #addListener(Class, Object, Method)} or - * {@link #addListener(com.vaadin.ui.Component.Listener)} instead. + * {@link #addListener(String, Class, Object, Method) instead. *

* * @param eventType @@ -916,7 +920,11 @@ public abstract class AbstractClientConnector * @param target * the target object that has registered to listen to events of * type eventType with one or more methods. + * @deprecated use a {@link Registration} from + * {@link #addListener} to remove a + * listener */ + @Deprecated @Override public void removeListener(Class eventType, Object target) { if (eventRouter != null) { diff --git a/server/src/main/java/com/vaadin/server/SerializableSupplier.java b/server/src/main/java/com/vaadin/server/SerializableSupplier.java new file mode 100644 index 0000000000..4b7cfa1305 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/SerializableSupplier.java @@ -0,0 +1,35 @@ +/* + * 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.server; + +import java.io.Serializable; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * A {@link Supplier} that is also {@link Serializable}. + * + * @see Supplier + * @author Vaadin Ltd + * @since 8.0 + * @param + * the type of the input to the function + */ +@FunctionalInterface +public interface SerializableSupplier + extends Supplier, Serializable { + // Only method inherited from Supplier +} diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 3e04f26d97..3bebc78410 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -16,15 +16,18 @@ package com.vaadin.ui; -import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.function.Consumer; +import com.vaadin.server.KeyMapper; +import com.vaadin.server.Resource; +import com.vaadin.server.ResourceReference; +import com.vaadin.server.SerializableBiPredicate; +import com.vaadin.server.SerializableConsumer; import org.jsoup.nodes.Element; import com.vaadin.data.HasValue; @@ -35,10 +38,6 @@ import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; -import com.vaadin.server.KeyMapper; -import com.vaadin.server.Resource; -import com.vaadin.server.ResourceReference; -import com.vaadin.server.SerializableBiPredicate; import com.vaadin.server.data.DataCommunicator; import com.vaadin.server.data.DataKeyMapper; import com.vaadin.server.data.DataProvider; @@ -72,7 +71,7 @@ public class ComboBox extends AbstractSingleSelect * allowed mode is active. */ @FunctionalInterface - public interface NewItemHandler extends Consumer, Serializable { + public interface NewItemHandler extends SerializableConsumer { } /** @@ -238,17 +237,16 @@ public class ComboBox extends AbstractSingleSelect * Note that unlike {@link #setItems(Collection)}, no automatic case * conversion is performed before the comparison. * - * @param filterPredicate - * predicate for comparing the item string (first parameter) and - * the filter string (second parameter) + * @param captionFilter + * filter to check if an item is shown when user typed some text into the ComboBox * @param items * the data items to display */ public void setItems( - SerializableBiPredicate filterPredicate, + CaptionFilter captionFilter, Collection items) { DataProvider provider = DataProvider.create(items) - .convertFilter(filterText -> item -> filterPredicate.test( + .convertFilter(filterText -> item -> captionFilter.test( getItemCaptionGenerator().apply(item), filterText)); setDataProvider(provider); } @@ -260,17 +258,16 @@ public class ComboBox extends AbstractSingleSelect * Note that unlike {@link #setItems(Collection)}, no automatic case * conversion is performed before the comparison. * - * @param filterPredicate - * predicate for comparing the item string (first parameter) and - * the filter string (second parameter) + * @param captionFilter + * filter to check if an item is shown when user typed some text into the ComboBox * @param items * the data items to display */ public void setItems( - SerializableBiPredicate filterPredicate, + CaptionFilter captionFilter, @SuppressWarnings("unchecked") T... items) { DataProvider provider = DataProvider.create(items) - .convertFilter(filterText -> item -> filterPredicate.test( + .convertFilter(filterText -> item -> captionFilter.test( getItemCaptionGenerator().apply(item), filterText)); setDataProvider(provider); } @@ -399,7 +396,6 @@ public class ComboBox extends AbstractSingleSelect * @see #isEmptySelectionAllowed() * @see #setEmptySelectionCaption(String) * @see #isSelected(Object) - * @see #select(Object) * * @return the empty selection caption, not {@code null} */ @@ -678,4 +674,24 @@ public class ComboBox extends AbstractSingleSelect */ updateDiffstate("selectedItemKey", Json.create(0)); } + + /** + * Predicate to check {@link ComboBox} item captions against user typed strings. + * + * @see #setItems(CaptionFilter, Collection) + * @see #setItems(CaptionFilter, Object[]) + */ + @FunctionalInterface + public interface CaptionFilter extends SerializableBiPredicate { + + /** + * Check item caption against entered text + * + * @param itemCaption + * @param filterText + * @return {@code true} if item passes the filter and should be listed, {@code false} otherwise + */ + @Override + public boolean test(String itemCaption, String filterText); + } } diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 5a6135a1bc..f528221e48 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -77,7 +77,6 @@ import com.vaadin.shared.ui.grid.GridStaticCellType; import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.shared.ui.grid.SectionState; import com.vaadin.shared.util.SharedUtil; -import com.vaadin.ui.Grid.FooterRow; import com.vaadin.ui.components.grid.AbstractSelectionModel; import com.vaadin.ui.components.grid.EditorImpl; import com.vaadin.ui.components.grid.Footer; @@ -675,7 +674,7 @@ public class Grid extends AbstractListing */ @FunctionalInterface public interface DetailsGenerator - extends Function, Serializable { + extends SerializableFunction { } /** @@ -987,7 +986,7 @@ public class Grid extends AbstractListing private final SerializableFunction valueProvider; - private SerializableFunction>> sortOrderProvider; + private SortOrderProvider sortOrderProvider; private SerializableComparator comparator; private StyleGenerator styleGenerator = item -> null; private DescriptionGenerator descriptionGenerator; @@ -1279,8 +1278,7 @@ public class Grid extends AbstractListing * given direction * @return this column */ - public Column setSortOrderProvider( - SerializableFunction>> provider) { + public Column setSortOrderProvider(SortOrderProvider provider) { Objects.requireNonNull(provider, "Sort order provider can't be null"); sortOrderProvider = provider; @@ -2181,6 +2179,7 @@ public class Grid extends AbstractListing * @param * the bean type */ + @FunctionalInterface public interface EditorErrorGenerator extends Serializable, BiFunction>, BinderValidationStatus, String> { @@ -3680,4 +3679,25 @@ public class Grid extends AbstractListing return result; } + /** + * Generates the sort orders when rows are sorted by a column. + * @see Column#setSortOrderProvider + * + * @since 8.0 + * @author Vaadin Ltd + */ + @FunctionalInterface + public interface SortOrderProvider extends SerializableFunction>> { + + /** + * Generates the sort orders when rows are sorted by a column. + * + * @param sortDirection desired sort direction + * + * @return sort information + */ + @Override + public Stream> apply(SortDirection sortDirection); + + } } diff --git a/server/src/test/java/com/vaadin/data/ResultTest.java b/server/src/test/java/com/vaadin/data/ResultTest.java index 756746a4ff..1dcda58a4d 100644 --- a/server/src/test/java/com/vaadin/data/ResultTest.java +++ b/server/src/test/java/com/vaadin/data/ResultTest.java @@ -15,8 +15,7 @@ */ package com.vaadin.data; -import java.util.function.Function; - +import com.vaadin.server.SerializableFunction; import org.junit.Assert; import org.junit.Test; @@ -71,7 +70,7 @@ public class ResultTest { Result result = new SimpleResult("foo", null) { @Override - public Result flatMap(Function> mapper) { + public Result flatMap(SerializableFunction> mapper) { return mapper.apply("foo"); } }; @@ -90,7 +89,7 @@ public class ResultTest { Result result = new SimpleResult("foo", null) { @Override - public Result flatMap(Function> mapper) { + public Result flatMap(SerializableFunction> mapper) { return new SimpleResult<>(null, "bar"); } };