diff options
author | Ilia Motornyi <elmot@vaadin.com> | 2016-12-14 16:29:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-14 16:29:41 +0200 |
commit | 7391e7cebf01665a58d0604144c6384ccc23356a (patch) | |
tree | a9ccdfacda8eca8d1df2bea05b6fdcec72bfea8c /server | |
parent | afcd95da1dba4abfd6158f38ab0a95755e2623e4 (diff) | |
download | vaadin-framework-7391e7cebf01665a58d0604144c6384ccc23356a.tar.gz vaadin-framework-7391e7cebf01665a58d0604144c6384ccc23356a.zip |
Miscellaneous Interface Refactoring
Diffstat (limited to 'server')
7 files changed, 127 insertions, 47 deletions
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<R> extends Serializable { * the function to provide the error message * @return the result of invoking the supplier */ - public static <R> Result<R> of(Supplier<R> supplier, - Function<Exception, String> onError) { + public static <R> Result<R> of(SerializableSupplier<R> supplier, + SerializableFunction<Exception, String> onError) { Objects.requireNonNull(supplier, "supplier cannot be null"); Objects.requireNonNull(onError, "onError cannot be null"); @@ -103,7 +104,7 @@ public interface Result<R> extends Serializable { * the mapping function * @return the mapped result */ - default <S> Result<S> map(Function<R, S> mapper) { + public default <S> Result<S> map(SerializableFunction<R, S> mapper) { return flatMap(value -> ok(mapper.apply(value))); } @@ -119,7 +120,7 @@ public interface Result<R> extends Serializable { * the mapping function * @return the mapped result */ - <S> Result<S> flatMap(Function<R, Result<S>> mapper); + public <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper); /** * Invokes either the first callback or the second one, depending on whether @@ -130,7 +131,7 @@ public interface Result<R> extends Serializable { * @param ifError * the function to call if failure */ - void handle(Consumer<R> ifOk, Consumer<String> ifError); + public void handle(SerializableConsumer<R> ifOk, SerializableConsumer<String> ifError); /** * Applies the {@code consumer} if result is not an error. @@ -138,7 +139,7 @@ public interface Result<R> extends Serializable { * @param consumer * consumer to apply in case it's not an error */ - default void ifOk(Consumer<R> consumer) { + public default void ifOk(SerializableConsumer<R> consumer) { handle(consumer, error -> { }); } @@ -149,7 +150,7 @@ public interface Result<R> extends Serializable { * @param consumer * consumer to apply in case it's an error */ - default void ifError(Consumer<String> consumer) { + public default void ifError(SerializableConsumer<String> consumer) { handle(value -> { }, consumer); } @@ -160,14 +161,14 @@ public interface Result<R> extends Serializable { * @return <code>true</code> if the result denotes an error, * <code>false</code> otherwise */ - boolean isError(); + public boolean isError(); /** * Returns an Optional of the result message, or an empty Optional if none. * * @return the optional message */ - Optional<String> getMessage(); + public Optional<String> getMessage(); /** * Return the value, if the result denotes success, otherwise throw an @@ -182,6 +183,6 @@ public interface Result<R> extends Serializable { * @throws X * if this result denotes an error */ - <X extends Throwable> R getOrThrow( - Function<String, ? extends X> exceptionProvider) throws X; + public <X extends Throwable> R getOrThrow( + SerializableFunction<String, ? extends X> 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<R> implements Result<R> { @Override @SuppressWarnings("unchecked") - public <S> Result<S> flatMap(Function<R, Result<S>> mapper) { + public <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper) { Objects.requireNonNull(mapper, "mapper cannot be null"); if (isError()) { @@ -65,7 +66,7 @@ class SimpleResult<R> implements Result<R> { } @Override - public void handle(Consumer<R> ifOk, Consumer<String> ifError) { + public void handle(SerializableConsumer<R> ifOk, SerializableConsumer<String> ifError) { Objects.requireNonNull(ifOk, "ifOk cannot be null"); Objects.requireNonNull(ifError, "ifError cannot be null"); if (isError()) { @@ -96,7 +97,7 @@ class SimpleResult<R> implements Result<R> { @Override public <X extends Throwable> R getOrThrow( - Function<String, ? extends X> exceptionSupplier) throws X { + SerializableFunction<String, ? extends X> 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 <code>eventType</code> 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 * <p> * 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. * </p> * * @param eventType @@ -916,7 +920,11 @@ public abstract class AbstractClientConnector * @param target * the target object that has registered to listen to events of * type <code>eventType</code> 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 <T> + * the type of the input to the function + */ +@FunctionalInterface +public interface SerializableSupplier<T> + extends Supplier<T>, 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<T> extends AbstractSingleSelect<T> * allowed mode is active. */ @FunctionalInterface - public interface NewItemHandler extends Consumer<String>, Serializable { + public interface NewItemHandler extends SerializableConsumer<String> { } /** @@ -238,17 +237,16 @@ public class ComboBox<T> extends AbstractSingleSelect<T> * 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<String, String> filterPredicate, + CaptionFilter captionFilter, Collection<T> items) { DataProvider<T, String> 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<T> extends AbstractSingleSelect<T> * 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<String, String> filterPredicate, + CaptionFilter captionFilter, @SuppressWarnings("unchecked") T... items) { DataProvider<T, String> 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<T> extends AbstractSingleSelect<T> * @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<T> extends AbstractSingleSelect<T> */ 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<String, String> { + + /** + * 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<T> extends AbstractListing<T> */ @FunctionalInterface public interface DetailsGenerator<T> - extends Function<T, Component>, Serializable { + extends SerializableFunction<T, Component> { } /** @@ -987,7 +986,7 @@ public class Grid<T> extends AbstractListing<T> private final SerializableFunction<T, ? extends V> valueProvider; - private SerializableFunction<SortDirection, Stream<SortOrder<String>>> sortOrderProvider; + private SortOrderProvider sortOrderProvider; private SerializableComparator<T> comparator; private StyleGenerator<T> styleGenerator = item -> null; private DescriptionGenerator<T> descriptionGenerator; @@ -1279,8 +1278,7 @@ public class Grid<T> extends AbstractListing<T> * given direction * @return this column */ - public Column<T, V> setSortOrderProvider( - SerializableFunction<SortDirection, Stream<SortOrder<String>>> provider) { + public Column<T, V> setSortOrderProvider(SortOrderProvider provider) { Objects.requireNonNull(provider, "Sort order provider can't be null"); sortOrderProvider = provider; @@ -2181,6 +2179,7 @@ public class Grid<T> extends AbstractListing<T> * @param <T> * the bean type */ + @FunctionalInterface public interface EditorErrorGenerator<T> extends Serializable, BiFunction<Map<Component, Column<T, ?>>, BinderValidationStatus<T>, String> { @@ -3680,4 +3679,25 @@ public class Grid<T> extends AbstractListing<T> 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<SortDirection, Stream<SortOrder<String>>> { + + /** + * Generates the sort orders when rows are sorted by a column. + * + * @param sortDirection desired sort direction + * + * @return sort information + */ + @Override + public Stream<SortOrder<String>> 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<String> result = new SimpleResult<String>("foo", null) { @Override - public <S> Result<S> flatMap(Function<String, Result<S>> mapper) { + public <S> Result<S> flatMap(SerializableFunction<String, Result<S>> mapper) { return mapper.apply("foo"); } }; @@ -90,7 +89,7 @@ public class ResultTest { Result<String> result = new SimpleResult<String>("foo", null) { @Override - public <S> Result<S> flatMap(Function<String, Result<S>> mapper) { + public <S> Result<S> flatMap(SerializableFunction<String, Result<S>> mapper) { return new SimpleResult<>(null, "bar"); } }; |