Reconstructed too many codes
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* Copyright 2013 Netflix, Inc.
|
||||
*
|
||||
* <p>
|
||||
* 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
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
@@ -22,24 +22,18 @@ import org.jackhuang.hellominecraft.logging.logger.Logger;
|
||||
/**
|
||||
* Allows execution of functions from multiple different languages.
|
||||
* <p>
|
||||
* Language support is provided via implementations of {@link FunctionLanguageAdaptor}.
|
||||
* Language support is provided via implementations of
|
||||
* {@link FunctionLanguageAdaptor}.
|
||||
* <p>
|
||||
* This class will dynamically look for known language adaptors on the classpath at startup or new ones can be registered using {@link #registerLanguageAdaptor(Class[], FunctionLanguageAdaptor)}.
|
||||
* This class will dynamically look for known language adaptors on the classpath
|
||||
* at startup or new ones can be registered using
|
||||
* {@link #registerLanguageAdaptor(Class[], FunctionLanguageAdaptor)}.
|
||||
*/
|
||||
public class Functions {
|
||||
|
||||
private static final Logger logger = new Logger("Functions");
|
||||
|
||||
private final static ConcurrentHashMap<Class<?>, FunctionLanguageAdaptor> languageAdaptors = new ConcurrentHashMap<Class<?>, FunctionLanguageAdaptor>();
|
||||
|
||||
static {
|
||||
/* optimistically look for supported languages if they are in the classpath */
|
||||
loadLanguageAdaptor("Groovy");
|
||||
loadLanguageAdaptor("JRuby");
|
||||
loadLanguageAdaptor("Clojure");
|
||||
loadLanguageAdaptor("Scala");
|
||||
// as new languages arise we can add them here but this does not prevent someone from using 'registerLanguageAdaptor' directly
|
||||
}
|
||||
private final static ConcurrentHashMap<Class<?>, FunctionLanguageAdaptor> languageAdaptors = new ConcurrentHashMap<>();
|
||||
|
||||
private static boolean loadLanguageAdaptor(String name) {
|
||||
String className = "rx.lang." + name.toLowerCase() + "." + name + "Adaptor";
|
||||
@@ -60,9 +54,8 @@ public class Functions {
|
||||
|
||||
public static void registerLanguageAdaptor(Class<?>[] functionClasses, FunctionLanguageAdaptor adaptor) {
|
||||
for (Class<?> functionClass : functionClasses) {
|
||||
if (functionClass.getPackage().getName().startsWith("java.")) {
|
||||
if (functionClass.getPackage().getName().startsWith("java."))
|
||||
throw new IllegalArgumentException("FunctionLanguageAdaptor implementations can not specify java.lang.* classes.");
|
||||
}
|
||||
languageAdaptors.put(functionClass, adaptor);
|
||||
}
|
||||
}
|
||||
@@ -76,24 +69,24 @@ public class Functions {
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method for determining the type of closure/function and executing it.
|
||||
*
|
||||
* Utility method for determining the type of closure/function and executing
|
||||
* it.
|
||||
*
|
||||
* @param function
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public static FuncN from(final Object function) {
|
||||
if (function == null) {
|
||||
if (function == null)
|
||||
throw new RuntimeException("function is null. Can't send arguments to null function.");
|
||||
}
|
||||
|
||||
/* check for typed Rx Function implementation first */
|
||||
if (function instanceof Function) {
|
||||
if (function instanceof Function)
|
||||
return fromFunction((Function) function);
|
||||
} else {
|
||||
else
|
||||
/* not an Rx Function so try language adaptors */
|
||||
|
||||
// check for language adaptor
|
||||
for (final Class c : languageAdaptors.keySet()) {
|
||||
for (final Class c : languageAdaptors.keySet())
|
||||
if (c.isInstance(function)) {
|
||||
final FunctionLanguageAdaptor la = languageAdaptors.get(c);
|
||||
// found the language adaptor so wrap in FuncN and return
|
||||
@@ -105,10 +98,7 @@ public class Functions {
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
// no language adaptor found
|
||||
}
|
||||
} // no language adaptor found
|
||||
|
||||
// no support found
|
||||
throw new RuntimeException("Unsupported closure type: " + function.getClass().getSimpleName());
|
||||
@@ -213,338 +203,254 @@ public class Functions {
|
||||
//
|
||||
// throw new RuntimeException("Unknown implementation of Function: " + function.getClass().getSimpleName());
|
||||
// }
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private static FuncN fromFunction(Function function) {
|
||||
// check Func* classes
|
||||
if (function instanceof Func0) {
|
||||
if (function instanceof Func0)
|
||||
return fromFunc((Func0) function);
|
||||
} else if (function instanceof Func1) {
|
||||
else if (function instanceof Func1)
|
||||
return fromFunc((Func1) function);
|
||||
} else if (function instanceof Func2) {
|
||||
else if (function instanceof Func2)
|
||||
return fromFunc((Func2) function);
|
||||
} else if (function instanceof Func3) {
|
||||
else if (function instanceof Func3)
|
||||
return fromFunc((Func3) function);
|
||||
} else if (function instanceof Func4) {
|
||||
else if (function instanceof Func4)
|
||||
return fromFunc((Func4) function);
|
||||
} else if (function instanceof Func5) {
|
||||
else if (function instanceof Func5)
|
||||
return fromFunc((Func5) function);
|
||||
} else if (function instanceof Func6) {
|
||||
else if (function instanceof Func6)
|
||||
return fromFunc((Func6) function);
|
||||
} else if (function instanceof Func7) {
|
||||
else if (function instanceof Func7)
|
||||
return fromFunc((Func7) function);
|
||||
} else if (function instanceof Func8) {
|
||||
else if (function instanceof Func8)
|
||||
return fromFunc((Func8) function);
|
||||
} else if (function instanceof Func9) {
|
||||
else if (function instanceof Func9)
|
||||
return fromFunc((Func9) function);
|
||||
} else if (function instanceof FuncN) {
|
||||
else if (function instanceof FuncN)
|
||||
return (FuncN) function;
|
||||
} else if (function instanceof Action0) {
|
||||
else if (function instanceof Action0)
|
||||
return fromAction((Action0) function);
|
||||
} else if (function instanceof Action1) {
|
||||
else if (function instanceof Action1)
|
||||
return fromAction((Action1) function);
|
||||
} else if (function instanceof Action2) {
|
||||
else if (function instanceof Action2)
|
||||
return fromAction((Action2) function);
|
||||
} else if (function instanceof Action3) {
|
||||
else if (function instanceof Action3)
|
||||
return fromAction((Action3) function);
|
||||
}
|
||||
|
||||
throw new RuntimeException("Unknown implementation of Function: " + function.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <R> FuncN<R> fromFunc(final Func0<R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 0) {
|
||||
throw new RuntimeException("Func0 expecting 0 arguments.");
|
||||
}
|
||||
return f.call();
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 0)
|
||||
throw new RuntimeException("Func0 expecting 0 arguments.");
|
||||
return f.call();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, R> FuncN<R> fromFunc(final Func1<T0, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 1) {
|
||||
throw new RuntimeException("Func1 expecting 1 argument.");
|
||||
}
|
||||
return f.call((T0) args[0]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 1)
|
||||
throw new RuntimeException("Func1 expecting 1 argument.");
|
||||
return f.call((T0) args[0]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, R> FuncN<R> fromFunc(final Func2<T0, T1, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 2) {
|
||||
throw new RuntimeException("Func2 expecting 2 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 2)
|
||||
throw new RuntimeException("Func2 expecting 2 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, R> FuncN<R> fromFunc(final Func3<T0, T1, T2, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 3) {
|
||||
throw new RuntimeException("Func3 expecting 3 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 3)
|
||||
throw new RuntimeException("Func3 expecting 3 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, T3, R> FuncN<R> fromFunc(final Func4<T0, T1, T2, T3, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 4) {
|
||||
throw new RuntimeException("Func4 expecting 4 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 4)
|
||||
throw new RuntimeException("Func4 expecting 4 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, T3, T4, R> FuncN<R> fromFunc(final Func5<T0, T1, T2, T3, T4, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 5) {
|
||||
throw new RuntimeException("Func5 expecting 5 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 5)
|
||||
throw new RuntimeException("Func5 expecting 5 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, T3, T4, T5, R> FuncN<R> fromFunc(final Func6<T0, T1, T2, T3, T4, T5, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 6) {
|
||||
throw new RuntimeException("Func6 expecting 6 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 6)
|
||||
throw new RuntimeException("Func6 expecting 6 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, T3, T4, T5, T6, R> FuncN<R> fromFunc(final Func7<T0, T1, T2, T3, T4, T5, T6, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 7) {
|
||||
throw new RuntimeException("Func7 expecting 7 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 7)
|
||||
throw new RuntimeException("Func7 expecting 7 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, T3, T4, T5, T6, T7, R> FuncN<R> fromFunc(final Func8<T0, T1, T2, T3, T4, T5, T6, T7, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 8) {
|
||||
throw new RuntimeException("Func8 expecting 8 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6], (T7) args[7]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 8)
|
||||
throw new RuntimeException("Func8 expecting 8 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6], (T7) args[7]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2, T3, T4, T5, T6, T7, T8, R> FuncN<R> fromFunc(final Func9<T0, T1, T2, T3, T4, T5, T6, T7, T8, R> f) {
|
||||
return new FuncN<R>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public R call(Object... args) {
|
||||
if (args.length != 9) {
|
||||
throw new RuntimeException("Func9 expecting 9 arguments.");
|
||||
}
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6], (T7) args[7], (T8) args[8]);
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 9)
|
||||
throw new RuntimeException("Func9 expecting 9 arguments.");
|
||||
return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6], (T7) args[7], (T8) args[8]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static FuncN<Void> fromAction(final Action0 f) {
|
||||
return new FuncN<Void>() {
|
||||
|
||||
@Override
|
||||
public Void call(Object... args) {
|
||||
if (args.length != 0) {
|
||||
throw new RuntimeException("Action0 expecting 0 arguments.");
|
||||
}
|
||||
f.call();
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 0)
|
||||
throw new RuntimeException("Action0 expecting 0 arguments.");
|
||||
f.call();
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0> FuncN<Void> fromAction(final Action1<T0> f) {
|
||||
return new FuncN<Void>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Void call(Object... args) {
|
||||
if (args.length != 1) {
|
||||
throw new RuntimeException("Action1 expecting 1 argument.");
|
||||
}
|
||||
f.call((T0) args[0]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 1)
|
||||
throw new RuntimeException("Action1 expecting 1 argument.");
|
||||
f.call((T0) args[0]);
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1> FuncN<Void> fromAction(final Action2<T0, T1> f) {
|
||||
return new FuncN<Void>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Void call(Object... args) {
|
||||
if (args.length != 2) {
|
||||
throw new RuntimeException("Action3 expecting 2 arguments.");
|
||||
}
|
||||
f.call((T0) args[0], (T1) args[1]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 2)
|
||||
throw new RuntimeException("Action3 expecting 2 arguments.");
|
||||
f.call((T0) args[0], (T1) args[1]);
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions with different arities.
|
||||
*
|
||||
* Convert a function to FuncN to allow heterogeneous handling of functions
|
||||
* with different arities.
|
||||
*
|
||||
* @param f
|
||||
* @return {@link FuncN}
|
||||
*/
|
||||
public static <T0, T1, T2> FuncN<Void> fromAction(final Action3<T0, T1, T2> f) {
|
||||
return new FuncN<Void>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Void call(Object... args) {
|
||||
if (args.length != 3) {
|
||||
throw new RuntimeException("Action3 expecting 3 arguments.");
|
||||
}
|
||||
f.call((T0) args[0], (T1) args[1], (T2) args[2]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Object... args) -> {
|
||||
if (args.length != 3)
|
||||
throw new RuntimeException("Action3 expecting 3 arguments.");
|
||||
f.call((T0) args[0], (T1) args[1], (T2) args[2]);
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user