public static String buildUri(final boolean procedure, final int functionParamCount) { final int paramCount = Math.min(6, functionParamCount); if (procedure) { StringConcatenation _builder = new StringConcatenation(); _builder.append("java:/Objects/"); String _canonicalName = Procedures.class.getCanonicalName(); _builder.append(_canonicalName); _builder.append("#"); String _canonicalName_1 = Procedures.class.getCanonicalName(); _builder.append(_canonicalName_1); _builder.append("$Procedure"); _builder.append(paramCount); return _builder.toString(); } StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("java:/Objects/"); String _canonicalName_2 = Functions.class.getCanonicalName(); _builder_1.append(_canonicalName_2); _builder_1.append("#"); String _canonicalName_3 = Functions.class.getCanonicalName(); _builder_1.append(_canonicalName_3); _builder_1.append("$Function"); _builder_1.append(paramCount); return _builder_1.toString(); }
@Test public void testFindFirst_exceptionInFilter() { final RuntimeException expectedException = new RuntimeException(); Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { throw expectedException; } }; for(IterableOrIterator testMe: testData(first, second, third)) { try { findFirst(testMe, filter); fail("expected exception"); } catch(RuntimeException e) { assertSame(expectedException, e); } } }
@Test public void testFindLast_exceptionInFilter() { final RuntimeException expectedException = new RuntimeException(); Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { throw expectedException; } }; for(IterableOrIterator testMe: testData(first, second, third)) { try { findLast(testMe, filter); fail("expected exception"); } catch(RuntimeException e) { assertSame(expectedException, e); } } }
public ClosureClient2(Functions.Function0<String>... functions) { StringBuilder builder = new StringBuilder("varArgs:"); for(Functions.Function0<String> function: functions) { builder.append(function.apply()); } value = builder.toString(); }
public <Obj> Functions.Function1<Obj, Obj> getIdentityFunction() { return new Functions.Function1<Obj, Obj>() { @Override public Obj apply(Obj p) { return p; } }; }
/** * @since 2.3 */ public String concatStrings(Functions.Function0<String>... functions) { StringBuilder result = new StringBuilder("varArgs:"); for(Functions.Function0<String> function: functions) { result.append(function.apply()); } return result.toString(); }
/** * @since 2.3 */ public String concatStrings(Functions.Function0<String> function1, Functions.Function0<String> function2) { StringBuilder result = new StringBuilder("twoArgs:"); result.append(function1.apply()); result.append(function2.apply()); return result.toString(); }
@SuppressWarnings("unchecked") public <RT, T1, T2> Functions.Function2<T1, T2, RT> createFunction( String body, Class<RT> returnType, Class<T1> paramType1, Class<T2> paramType2) { return (Functions.Function2<T1, T2, RT>) internalCreateFunction(body, returnType, Tuples.pair((Type) paramType1, "p1"), Tuples.pair((Type) paramType2, "p2")); }
private Class<?> loadFunctionClass(String simpleFunctionName, boolean procedure) { try { if (!procedure) { return Functions.class.getClassLoader().loadClass( Functions.class.getCanonicalName() + "$" + simpleFunctionName); } else { return Procedures.class.getClassLoader().loadClass( Procedures.class.getCanonicalName() + "$" + simpleFunctionName); } } catch (ClassNotFoundException e) { throw new WrappedException(e); } }
public FunctionTypeKind getFunctionTypeKind(ParameterizedTypeReference typeReference) { JvmType type = typeReference.getType(); if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) { JvmDeclaredType outerType = ((JvmGenericType) type).getDeclaringType(); if (outerType != null) { if (Procedures.class.getName().equals(outerType.getIdentifier())) { return FunctionTypeKind.PROCEDURE; } if (Functions.class.getName().equals(outerType.getIdentifier())) { return FunctionTypeKind.FUNCTION; } } } return FunctionTypeKind.NONE; }
protected Object _doEvaluate(XClosure closure, IEvaluationContext context, CancelIndicator indicator) { Class<?> functionIntf = null; switch (closure.getFormalParameters().size()) { case 0: functionIntf = getClass(Functions.Function0.class); break; case 1: functionIntf = getClass(Functions.Function1.class); break; case 2: functionIntf = getClass(Functions.Function2.class); break; case 3: functionIntf = getClass(Functions.Function3.class); break; case 4: functionIntf = getClass(Functions.Function4.class); break; case 5: functionIntf = getClass(Functions.Function5.class); break; case 6: functionIntf = getClass(Functions.Function6.class); break; default: throw new IllegalStateException("Closures with more then 6 parameters are not supported."); } ClosureInvocationHandler invocationHandler = new ClosureInvocationHandler(closure, context, this, indicator); Object proxy = Proxy.newProxyInstance(classLoader, new Class<?>[] { functionIntf }, invocationHandler); return proxy; }
protected Functions.Function0<Object> compile(String code) { String javaCode = compileToJavaCode(code); try { Function0<Object> function = javaCompiler.createFunction(javaCode, Object.class); return function; } catch (Exception e) { throw new RuntimeException("Java compilation failed. Java code was : \n" + javaCode, e); } }
@Test public void testTypeParamInference_04() throws Exception { XBlockExpression block = (XBlockExpression) expression("{ var Integer i = new testdata.ClosureClient().invoke1([e|null],'foo') }"); XVariableDeclaration var = (XVariableDeclaration) block.getExpressions().get(0); XMemberFeatureCall fc = (XMemberFeatureCall) var.getRight(); final XExpression closure = fc.getMemberCallArguments().get(0); assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String, java.lang.Integer>", closure); }
@Test public void testTypeParamInference_07() throws Exception { XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() invoke1([ String e|e.length],'foo') }"); XFeatureCall fc = (XFeatureCall) block.getExpressions().get(1); final XExpression closure = fc.getFeatureCallArguments().get(0); assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String, java.lang.Integer>", closure); }
@Test public void testTypeParamInference_07_b() throws Exception { XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() invoke1([ e|e.length],'foo') }"); XFeatureCall fc = (XFeatureCall) block.getExpressions().get(1); final XExpression closure = fc.getFeatureCallArguments().get(0); assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String, java.lang.Integer>", closure); }
@Test public void testTypeParamInference_09() throws Exception { XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() var Integer i = invoke1([e|null],'foo') }"); XVariableDeclaration var = (XVariableDeclaration) block.getExpressions().get(1); XFeatureCall fc = (XFeatureCall) var.getRight(); final XExpression closure = fc.getFeatureCallArguments().get(0); assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String, java.lang.Integer>", closure); }
@Test public void testRecursiveClosure_02() throws Exception { XBlockExpression block = (XBlockExpression) expression("{ val (int)=>int fun = [ self.apply(it) ] }"); XVariableDeclaration variable = (XVariableDeclaration) block.getExpressions().get(0); XClosure closure = (XClosure) variable.getRight(); XBlockExpression body = (XBlockExpression) closure.getExpression(); XMemberFeatureCall member = (XMemberFeatureCall) body.getExpressions().get(0); XFeatureCall recursive = (XFeatureCall) member.getMemberCallTarget(); assertEquals(Functions.Function1.class.getName(), recursive.getFeature().getQualifiedName('$')); }
@Before public void setUp() throws Exception { compiler.setTemporaryFolder(tempFolder); compiler.setParentClassLoader(OnTheFlyJavaCompilerTest.class.getClassLoader()); compiler.addClassPathOfClass(OnTheFlyJavaCompilerTest.class); compiler.addClassPathOfClass(Functions.class); }
@SuppressWarnings("unchecked") @Test public void testCompileToClass() throws Exception { Class<?> class1 = compiler.compileToClass("Foo", "public class Foo implements "+Functions.Function0.class.getCanonicalName()+"<String> {" + " public String apply() {\n" + " return \"foo\";\n" + " }" + "}"); assertEquals("foo", ((Functions.Function0<String>)class1.newInstance()).apply()); }
@Test public void testFlatMap () { ArrayList<String> list = newArrayList("foo", "bar"); final Functions.Function1<String, Iterator<String>> function = new Functions.Function1<String, Iterator<String>>() { @Override public Iterator<String> apply(String p) { return newArrayList("Hello", p).iterator(); } }; assertEquals(newArrayList("Hello", "foo", "Hello", "bar"), newArrayList(IteratorExtensions.flatMap(list.iterator(), function))); }
@Test public void testSortBy() throws Exception { List<? extends CharSequence> list = newArrayList("foo","bar","baz"); List<? extends CharSequence> sorted = IterableExtensions.sortBy(list, new Functions.Function1<CharSequence, String>() { @Override public String apply(CharSequence p) { return p.toString(); } }); assertNotSame(list, sorted); assertEquals(sorted, newArrayList("bar","baz","foo")); }
@Test public void testJoinWithBeforeAndAfter() throws Exception { ArrayList<String> list = newArrayList("foo", "bar"); ArrayList<String> singletonList = newArrayList("foo"); ArrayList<String> emptylist = new ArrayList<String>(); final Functions.Function1<String, String> function = new Functions.Function1<String, String>() { @Override public String apply(String p) { return p; } }; assertEquals("<foo,bar>", IterableExtensions.join(list, "<", ",", ">", function)); assertEquals("<foo>", IterableExtensions.join(singletonList, "<", ",", ">", function)); assertEquals("", IterableExtensions.join(emptylist, "<", ",", ">", function)); assertEquals("foo,bar>", IterableExtensions.join(list, null, ",", ">", function)); assertEquals("foo>", IterableExtensions.join(singletonList, null, ",", ">", function)); assertEquals("", IterableExtensions.join(emptylist, null, ",", ">", function)); assertEquals("<foobar>", IterableExtensions.join(list, "<", null, ">", function)); assertEquals("<foo>", IterableExtensions.join(singletonList, "<", null, ">", function)); assertEquals("", IterableExtensions.join(emptylist, "<", null, ">", function)); assertEquals("<foo,bar", IterableExtensions.join(list, "<", ",", null, function)); assertEquals("<foo", IterableExtensions.join(singletonList, "<", ",", null, function)); assertEquals("", IterableExtensions.join(emptylist, "<", ",", null, function)); }
@Test public void testFlatMap () { ArrayList<String> list = newArrayList("foo", "bar"); final Functions.Function1<String, Iterable<String>> function = new Functions.Function1<String, Iterable<String>>() { @Override public Iterable<String> apply(String p) { return newArrayList("Hello", p); } }; assertEquals(newArrayList("Hello", "foo", "Hello", "bar"), newArrayList(IterableExtensions.flatMap(list, function))); }
@Test public void testFindFirst_empty() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return true; } }; for(IterableOrIterator testMe: testData()) { Integer last = findFirst(testMe, filter); assertNull("empty input yields null", last); } }
@Test public void testFindFirst_noMatch() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return false; } }; for(IterableOrIterator testMe: testData(first, second, third)) { Integer last = findFirst(testMe, filter); assertNull("unmatched input yields null", last); } }
@Test public void testFindFirst_allMatches() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return true; } }; for(IterableOrIterator testMe: testData(first, second, third)) { Integer last = findFirst(testMe, filter); assertEquals(first, last); } }
@Test public void testFindFirst_oneMatch() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return second.equals(p); } }; for(IterableOrIterator testMe: testData(first, second, third)) { Integer last = findFirst(testMe, filter); assertEquals(second, last); } }
@Test public void testFindFirst_exceptionInFilter_emptyInput() { final RuntimeException expectedException = new RuntimeException(); Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { throw expectedException; } }; for(IterableOrIterator testMe: testData()) { assertNull(findFirst(testMe, filter)); } }
@Test public void testFindFirst_NPE_noInput() { try { findLast(null, new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return true; } }); fail("Expected NPE"); } catch(NullPointerException npe) { // expected } }
@Test public void testFindLast_empty() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return true; } }; for(IterableOrIterator testMe: testData()) { Integer last = findLast(testMe, filter); assertNull("empty input yields null", last); } }
@Test public void testFindLast_noMatch() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return false; } }; for(IterableOrIterator testMe: testData(first, second, third)) { Integer last = findLast(testMe, filter); assertNull("unmatched input yields null", last); } }
@Test public void testFindLast_allMatches() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return true; } }; for(IterableOrIterator testMe: testData(first, second, third)) { Integer last = findLast(testMe, filter); assertEquals(third, last); } }
@Test public void testFindLast_oneMatch() { Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return second.equals(p); } }; for(IterableOrIterator testMe: testData(first, second, third)) { Integer last = findLast(testMe, filter); assertEquals(second, last); } }
@Test public void testFindLast_exceptionInFilter_emptyInput() { final RuntimeException expectedException = new RuntimeException(); Function1<Integer, Boolean> filter = new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { throw expectedException; } }; for(IterableOrIterator testMe: testData()) { Integer last = findLast(testMe, filter); assertEquals(null, last); } }
@Test public void testFindLast_NPE_noInput() { try { findLast(null, new Functions.Function1<Integer, Boolean>() { @Override public Boolean apply(Integer p) { return true; } }); fail("Expected NPE"); } catch(NullPointerException npe) { // expected } }
/** * @since 2.9 */ public static String getDefaultArgumentList(ParserRule rule) { String result = IterableExtensions.join(rule.getParameters(), "[", ", ", "]", new Functions.Function1<Parameter, String>() { @Override public String apply(Parameter p) { return Boolean.FALSE.toString(); } }); return result; }