Java Code Examples for org.eclipse.xtext.xbase.lib.Functions#Function1

The following examples show how to use org.eclipse.xtext.xbase.lib.Functions#Function1 . 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 vote down vote up
@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: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
@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 3
Source File: CompilerTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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 4
Source File: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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 5
Source File: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 6
Source File: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 7
Source File: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 8
Source File: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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 9
Source File: ClosureClient.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public <Obj> Functions.Function1<Obj, Obj> getIdentityFunction() {
	return new Functions.Function1<Obj, Obj>() {
		@Override
		public Obj apply(Obj p) {
			return p;
		}
		
	};
}
 
Example 10
Source File: IteratorExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@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 11
Source File: ClosureClient.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1WithExtends(Functions.Function1<P, ? extends T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 12
Source File: ClosureClient.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1(Functions.Function1<P, T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 13
Source File: ClosureClient.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1WithExtends(Functions.Function1<P, ? extends T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 14
Source File: OnTheFlyJavaCompiler.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <RT, T> Functions.Function1<T, RT> createFunction(String body,
		Class<RT> returnType, Class<T> paramType) {
	return (Functions.Function1<T, RT>) internalCreateFunction(body,
			returnType, Tuples.pair((Type) paramType, "p"));
}
 
Example 15
Source File: ClosureClient.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1WithSuperAndExtends(Functions.Function1<? super P,? extends T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 16
Source File: ClosureClient.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1WithSuper(Functions.Function1<? super P,T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 17
Source File: ClosureClient.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1WithSuper(Functions.Function1<? super P,T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 18
Source File: ClosureClient.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1(Functions.Function1<P, T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 19
Source File: ClosureClient.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public <P,T> T invoke1WithSuperAndExtends(Functions.Function1<? super P,? extends T> fun, P p1) {
	return fun.apply(p1);
}
 
Example 20
Source File: BaseIterablesIteratorsTest.java    From xtext-lib with Eclipse Public License 2.0 votes vote down vote up
protected abstract IterableOrIterator takeWhile(IterableOrIterator input, Functions.Function1<Integer, Boolean> filter);