From 5f63aed263bd5f63cfb7ea1e7e27f6a88e290fe4 Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Wed, 20 May 2015 17:17:51 +0300 Subject: [PATCH] introduce Functions --- .../com/intellij/openapi/util/Conditions.java | 19 ++++++ .../src/com/intellij/util/Functions.java | 67 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 platform/util-rt/src/com/intellij/util/Functions.java diff --git a/platform/util-rt/src/com/intellij/openapi/util/Conditions.java b/platform/util-rt/src/com/intellij/openapi/util/Conditions.java index f8940abaa6c8..b6b542602a56 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/Conditions.java +++ b/platform/util-rt/src/com/intellij/openapi/util/Conditions.java @@ -18,6 +18,7 @@ package com.intellij.openapi.util; import com.intellij.reference.SoftReference; import com.intellij.util.ArrayUtilRt; +import com.intellij.util.Function; import org.jetbrains.annotations.NotNull; import java.util.HashMap; @@ -48,6 +49,15 @@ public class Conditions { }; } + public static Condition assignableTo(final Class clazz) { + return new Condition() { + @Override + public boolean value(Class t) { + return clazz.isAssignableFrom(t); + } + }; + } + public static Condition instanceOf(final Class... clazz) { return new Condition() { @Override @@ -114,6 +124,15 @@ public class Conditions { return new Or(c1, c2); } + public static Condition compose(final Function fun, final Condition condition) { + return new Condition() { + @Override + public boolean value(A o) { + return condition.value(fun.fun(o)); + } + }; + } + public static Condition cached(Condition c) { return new SoftRefCache(c); } diff --git a/platform/util-rt/src/com/intellij/util/Functions.java b/platform/util-rt/src/com/intellij/util/Functions.java new file mode 100644 index 000000000000..818897b4a4f6 --- /dev/null +++ b/platform/util-rt/src/com/intellij/util/Functions.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * 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.intellij.util; + +import com.intellij.openapi.util.Pair; + +import java.util.Map; + +/** + * @author gregsh + */ +public class Functions { + + public static Function identity() { + return Function.ID; + } + + public static Function toString() { + return Function.TO_STRING; + } + + public static Function fromMap(final Map map) { + return new Function() { + @Override + public B fun(A a) { + return map.get(a); + } + }; + } + + private static final Function PAIR_FIRST = new Function, Object>() { + @Override + public Object fun(Pair pair) { + return Pair.getFirst(pair); + } + }; + + private static final Function PAIR_SECOND = new Function, Object>() { + @Override + public Object fun(Pair pair) { + return Pair.getSecond(pair); + } + }; + + public static Function, A> pairFirst() { + //noinspection unchecked + return (Function, A>)PAIR_FIRST; + } + + public static Function, B> pairSecond() { + //noinspection unchecked + return (Function, B>)PAIR_SECOND; + } +}