From c42ba34568b688417bea63afadef7b3402a74234 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Mon, 19 Nov 2018 14:39:31 +0800 Subject: [PATCH] Add basic helper functional interface Signed-off-by: Eric Zhao --- .../csp/sentinel/util/function/Function.java | 30 ++++++++++++++ .../csp/sentinel/util/function/Predicate.java | 31 ++++++++++++++ .../csp/sentinel/util/function/Tuple2.java | 41 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Function.java create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Predicate.java create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Tuple2.java diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Function.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Function.java new file mode 100644 index 00000000..edcaa76c --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Function.java @@ -0,0 +1,30 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding 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.alibaba.csp.sentinel.util.function; + +/** + * Function functional interface from JDK 8. + */ +public interface Function { + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + */ + R apply(T t); +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Predicate.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Predicate.java new file mode 100644 index 00000000..19cb1c97 --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Predicate.java @@ -0,0 +1,31 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding 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.alibaba.csp.sentinel.util.function; + +/** + * Predicate functional interface from JDK 8. + */ +public interface Predicate { + + /** + * Evaluates this predicate on the given argument. + * + * @param t the input argument + * @return {@code true} if the input argument matches the predicate, + * otherwise {@code false} + */ + boolean test(T t); +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Tuple2.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Tuple2.java new file mode 100644 index 00000000..2b8ad3cd --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/function/Tuple2.java @@ -0,0 +1,41 @@ +package com.alibaba.csp.sentinel.util.function; + +/** + * A tuple of 2 elements. + */ +public class Tuple2 { + + public final R1 r1; + public final R2 r2; + + public Tuple2(R1 r1, R2 r2) { + this.r1 = r1; + this.r2 = r2; + } + + /** + * Factory method for creating a Tuple. + * + * @return new Tuple + */ + public static Tuple2 of(C1 c1, C2 c2) { + return new Tuple2(c1, c2); + } + + /** + * Swaps the element of this Tuple. + * + * @return a new Tuple where the first element is the second element of this Tuple and the second element is the first element of this Tuple. + */ + public Tuple2 swap() { + return new Tuple2(this.r2, this.r1); + } + + @Override + public String toString() { + return "Tuple2{" + + "r1=" + r1 + + ", r2=" + r2 + + '}'; + } +} \ No newline at end of file