org.eclipse.xtext.xbase.lib.Functions Java Examples
The following examples show how to use
org.eclipse.xtext.xbase.lib.Functions.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompilerTest.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug362236_02() throws Exception { String code = "import java.util.List\n" + "class Z {\n"+ " def returnClosure() { \n" + " thing(Integer i|i.toString)\n" + " }\n" + " def thing(Object o) {" + " o\n" + " }\n" + "}"; String javaCode = compileToJavaCode(code); Class<?> class1 = compileToClass("Z", javaCode); Object object = class1.getDeclaredConstructor().newInstance(); Object closure = class1.getDeclaredMethod("returnClosure").invoke(object); @SuppressWarnings("unchecked") Functions.Function1<Object, Object> castedClosure = (Function1<Object, Object>) closure; assertEquals("123", castedClosure.apply(123)); }
Example #2
Source File: CompilerTest.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug362236_03() throws Exception { String code = "import java.util.List\n" + "class Z {\n"+ " def returnClosure() { \n" + " thing(i|i.toString)\n" + " }\n" + " def thing((Object)=>Object o) {" + " o\n" + " }\n" + "}"; String javaCode = compileToJavaCode(code); Class<?> class1 = compileToClass("Z", javaCode); Object object = class1.getDeclaredConstructor().newInstance(); Object closure = class1.getDeclaredMethod("returnClosure").invoke(object); @SuppressWarnings("unchecked") Functions.Function1<Object, Object> castedClosure = (Function1<Object, Object>) closure; assertEquals("123", castedClosure.apply(123)); }
Example #3
Source File: CompilerTest.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug362236_04() throws Exception { String code = "import java.util.List\n" + "class Z {\n"+ " def returnListOfClosures() { \n" + " val list = <(String)=>int>newArrayList()" + " list.add [ length ]" + " list\n" + " }\n" + "}"; String javaCode = compileToJavaCode(code); Class<?> class1 = compileToClass("Z", javaCode); Object object = class1.getDeclaredConstructor().newInstance(); Object list = class1.getDeclaredMethod("returnListOfClosures").invoke(object); @SuppressWarnings("unchecked") Functions.Function1<String, Integer> castedClosure = (Function1<String, Integer>)((List<?>)list).get(0); assertEquals(Integer.valueOf(4), castedClosure.apply("4444")); }
Example #4
Source File: OpenEditorTest.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected void assertActiveEditor(String expectedEditorID, String expectedEditorTitle, final String expectedSelection) { IEditorPart editorPart = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor(); assertEquals(expectedEditorTitle, editorPart.getTitle()); IEditorSite editorSite = editorPart.getEditorSite(); assertEquals(expectedEditorID, editorSite.getId()); final ISelectionProvider selectionProvider = editorSite.getSelectionProvider(); assertTrue(selectionProvider.getSelection() instanceof ITextSelection); // The selection may be updated asynchronously, so we may have to wait until the selection changes workbenchTestHelper.awaitUIUpdate(new Functions.Function0<Boolean>() { @Override public Boolean apply() { return expectedSelection.equals(((ITextSelection) selectionProvider.getSelection()).getText()); } }, SELECTION_TIMEOUT); assertEquals(expectedSelection, ((ITextSelection) selectionProvider.getSelection()).getText()); }
Example #5
Source File: BaseIterablesIteratorsTest.java From xtext-lib with Eclipse Public License 2.0 | 6 votes |
@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); } } }
Example #6
Source File: WorkbenchTestHelper.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
/** * Wait for an update in the UI. * * @param test * tester function that returns true if the target state of the UI has been reached * @param timeout * the time after which waiting is canceled */ public void awaitUIUpdate(Functions.Function0<Boolean> test, final long timeout) { long startTime = System.currentTimeMillis(); final Display display = Display.getCurrent(); new Thread("Display alarm") { @Override public void run() { try { Thread.sleep(timeout); if (!display.isDisposed()) display.wake(); } catch (InterruptedException e) { } } }.start(); while (!test.apply() && System.currentTimeMillis() - startTime < timeout) { boolean hasWork = display.sleep(); while (hasWork) { hasWork = display.readAndDispatch(); } } }
Example #7
Source File: WorkbenchTestHelper.java From sarl with Apache License 2.0 | 6 votes |
/** * Wait for an update in the UI. * * @param test * tester function that returns true if the target state of the UI has been reached * @param timeout * the time after which waiting is canceled */ public void awaitUIUpdate(Functions.Function0<Boolean> test, final long timeout) { long startTime = System.currentTimeMillis(); final Display display = Display.getCurrent(); new Thread("Display alarm") { //$NON-NLS-1$ @Override public void run() { try { Thread.sleep(timeout); display.wake(); } catch (InterruptedException e) { // } } }.start(); while (!test.apply() && System.currentTimeMillis() - startTime < timeout) { boolean hasWork = display.sleep(); while (hasWork) { hasWork = display.readAndDispatch(); } } }
Example #8
Source File: IterableExtensionsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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)); }
Example #9
Source File: BaseIterablesIteratorsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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); } }
Example #10
Source File: XbaseExpectedTypeProviderTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #11
Source File: IterableExtensionsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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))); }
Example #12
Source File: ClosureClient.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
public <Obj> Functions.Function1<Obj, Obj> getIdentityFunction() { return new Functions.Function1<Obj, Obj>() { @Override public Obj apply(Obj p) { return p; } }; }
Example #13
Source File: XbaseReferenceUpdater.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected Iterable<IReferenceDescription> getNotImportTypeReferences(Iterable<IReferenceDescription> referenceDescriptions) { return IterableExtensions.filter(referenceDescriptions, new Functions.Function1<IReferenceDescription, Boolean>() { @Override public Boolean apply(IReferenceDescription p) { return !isImportTypeReference(p); } }); }
Example #14
Source File: BaseIterablesIteratorsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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); } }
Example #15
Source File: BaseIterablesIteratorsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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 } }
Example #16
Source File: ClosureClient.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
/** * @since 2.3 */ @SuppressWarnings("unchecked") 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(); }
Example #17
Source File: FunctionTypes.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
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; }
Example #18
Source File: FunctionTypes.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
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); } }
Example #19
Source File: BaseIterablesIteratorsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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); } }
Example #20
Source File: IterableExtensionsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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")); }
Example #21
Source File: XbaseExpectedTypeProviderTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #22
Source File: IteratorExtensionsTest.java From xtext-lib with Eclipse Public License 2.0 | 5 votes |
@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))); }
Example #23
Source File: XbaseExpectedTypeProviderTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #24
Source File: ClosureClient.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
/** * @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(); }
Example #25
Source File: AbstractXbaseLinkingTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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('$')); }
Example #26
Source File: OnTheFlyJavaCompilerTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Before public void setUp() throws Exception { compiler.setTemporaryFolder(tempFolder); compiler.setParentClassLoader(OnTheFlyJavaCompilerTest.class.getClassLoader()); compiler.addClassPathOfClass(OnTheFlyJavaCompilerTest.class); compiler.addClassPathOfClass(Functions.class); }
Example #27
Source File: OnTheFlyJavaCompilerTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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()); }
Example #28
Source File: ClosureClient2.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@SafeVarargs public ClosureClient2(Functions.Function0<String>... functions) { StringBuilder builder = new StringBuilder("varArgs:"); for(Functions.Function0<String> function: functions) { builder.append(function.apply()); } value = builder.toString(); }
Example #29
Source File: ClosureClient.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
/** * @since 2.3 */ @SafeVarargs public final String concatStrings(Functions.Function0<String>... functions) { StringBuilder result = new StringBuilder("varArgs:"); for(Functions.Function0<String> function: functions) { result.append(function.apply()); } return result.toString(); }
Example #30
Source File: ClosureClient.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
public <Obj> Functions.Function1<Obj, Obj> getIdentityFunction() { return new Functions.Function1<Obj, Obj>() { @Override public Obj apply(Obj p) { return p; } }; }