GEODE-641: Adding default methods to the Function interface Adding default methods to Function so that Function is now a functional interface and lambda expressions can be used for functions.
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/15a2a29d Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/15a2a29d Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/15a2a29d Branch: refs/heads/develop Commit: 15a2a29d996c60831ec67bb8f565660950e67a29 Parents: 8fcd9c0 Author: Dan Smith <upthewatersp...@apache.org> Authored: Wed Apr 27 17:53:18 2016 -0700 Committer: Dan Smith <upthewatersp...@apache.org> Committed: Thu May 5 17:17:01 2016 -0700 ---------------------------------------------------------------------- .../gemfire/cache/execute/Function.java | 17 +++- .../gemfire/cache/execute/FunctionAdapter.java | 87 +------------------- .../cache/execute/FunctionAdapterJUnitTest.java | 65 +++++++++++++++ 3 files changed, 80 insertions(+), 89 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/15a2a29d/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/Function.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/Function.java b/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/Function.java index 553248e..f32fcad 100755 --- a/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/Function.java +++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/Function.java @@ -35,6 +35,7 @@ import com.gemstone.gemfire.lang.Identifiable; * * @since 6.0 */ +@FunctionalInterface public interface Function extends Identifiable<String> { /** @@ -53,7 +54,9 @@ public interface Function extends Identifiable<String> { * @return whether this function returns a Result back to the caller. * @since 6.0 */ - public boolean hasResult(); + public default boolean hasResult() { + return true; + } /** * The method which contains the logic to be executed. This method should be @@ -75,7 +78,9 @@ public interface Function extends Identifiable<String> { * @return string identifying this function * @since 6.0 */ - public String getId(); + public default String getId() { + return getClass().getCanonicalName(); + } /** * <p>Return true to indicate to GemFire the method @@ -94,7 +99,9 @@ public interface Function extends Identifiable<String> { * @since 6.0 * @see FunctionService */ - public boolean optimizeForWrite(); + public default boolean optimizeForWrite() { + return false; + } /** * Specifies whether the function is eligible for re-execution (in case of @@ -105,6 +112,8 @@ public interface Function extends Identifiable<String> { * * @since 6.5 */ - public boolean isHA(); + public default boolean isHA() { + return true; + } } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/15a2a29d/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/FunctionAdapter.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/FunctionAdapter.java b/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/FunctionAdapter.java index c4cfeed..674120c 100755 --- a/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/FunctionAdapter.java +++ b/geode-core/src/main/java/com/gemstone/gemfire/cache/execute/FunctionAdapter.java @@ -32,92 +32,9 @@ package com.gemstone.gemfire.cache.execute; * * @since 6.0 * @see Function + * @deprecated Use {@link Function} instead. Function has default + * methods that now mimic the behavior of FunctionAdapter. * */ public abstract class FunctionAdapter implements Function { - /** - * The method which contains the logic to be executed. This method should be - * thread safe and may be invoked more than once on a given member for a - * single {@link Execution}. The context provided to this function is the one - * which was built using {@linkplain Execution}. The contexts can be data - * dependent or data-independent so user should check to see if the context - * provided in parameter is instance of {@link RegionFunctionContext}. - * - * @param context - * as created by {@link Execution} - * @since 6.0 - */ - public abstract void execute(FunctionContext context); - - /** - * Return a unique function identifier, used to register the function with - * {@link FunctionService} - * - * @return string identifying this function - * @since 6.0 - */ - public abstract String getId(); - - /** - * Specifies whether the function sends results while executing. The method - * returns false if no result is expected.<br> - * <p> - * If {@link Function#hasResult()} returns false, - * {@link ResultCollector#getResult()} throws {@link FunctionException}. - * </p> - * <p> - * If {@link Function#hasResult()} returns true, - * {@link ResultCollector#getResult()} blocks and waits for the result of - * function execution - * </p> - * - * @return whether this function returns a Result back to the caller. - * @since 6.0 - */ - public boolean hasResult() { - return true; - } - - /** - * <p> - * Return true to indicate to GemFire the method requires optimization for - * writing the targeted - * {@link FunctionService#onRegion(com.gemstone.gemfire.cache.Region)} and any - * associated {@linkplain Execution#withFilter(java.util.Set) routing objects} - * . - * </p> - * - * <p> - * Returning false will optimize for read behavior on the targeted - * {@link FunctionService#onRegion(com.gemstone.gemfire.cache.Region)} and any - * associated {@linkplain Execution#withFilter(java.util.Set) routing objects} - * . - * </p> - * - * <p> - * This method is only consulted when invoked as a - * {@linkplain FunctionService#onRegion(com.gemstone.gemfire.cache.Region) - * data dependent function} - * </p> - * - * @return false if the function is read only, otherwise returns true - * @since 6.0 - * @see FunctionService - */ - public boolean optimizeForWrite() { - return false; - } - - /** - * Specifies whether the function is eligible for re-execution (in case of - * failure). - * - * @return whether the function is eligible for re-execution. - * @see RegionFunctionContext#isPossibleDuplicate() - * - * @since 6.5 - */ - public boolean isHA() { - return true; - } } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/15a2a29d/geode-core/src/test/java/com/gemstone/gemfire/cache/execute/FunctionAdapterJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/cache/execute/FunctionAdapterJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/cache/execute/FunctionAdapterJUnitTest.java new file mode 100644 index 0000000..6bb5aeb --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/execute/FunctionAdapterJUnitTest.java @@ -0,0 +1,65 @@ +/* + * 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 com.gemstone.gemfire.cache.execute; + +import static org.junit.Assert.*; + +import com.gemstone.gemfire.test.junit.categories.UnitTest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(UnitTest.class) +public class FunctionAdapterJUnitTest { + + private FunctionAdapter adapter; + + @Before + public void createDefaultAdapter() { + adapter = new MyFunctionAdapter(); + } + + @Test + public void optimizeForWriteDefaultsFalse() { + assertFalse(adapter.optimizeForWrite()); + } + + @Test + public void idDefaultsToClassName() { + assertEquals(MyFunctionAdapter.class.getCanonicalName(), adapter.getId()); + } + + @Test + public void hasResultDefaultsTrue() { + assertTrue(adapter.hasResult()); + + } + + @Test + public void isHADefaultsTrue() { + assertTrue(adapter.isHA()); + } + + private static class MyFunctionAdapter extends FunctionAdapter { + + @Override + public void execute(final FunctionContext context) { + } + + } +}