All Downloads are FREE. Search and download functionalities are using the official Maven repository.

testdata.ClosureClient Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2010, 2017 itemis AG (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package testdata;

import org.eclipse.xtext.xbase.lib.Functions;
import org.eclipse.xtext.xbase.lib.Procedures;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.inject.Provider;

/**
 * @author Sebastian Zarnekow - Initial contribution and API
 * @author Sven Efftinge
 */
public class ClosureClient {

	public  T invoke0(Functions.Function0 fun) {
		return fun.apply();
	}
	
	public void infiniteApply(Functions.Function0 fun) {
		while(true) {
			fun.apply();
		}
	}
	
	public  T invoke1(Functions.Function1 fun, P p1) {
		return fun.apply(p1);
	}
	
	public  T invoke1WithExtends(Functions.Function1 fun, P p1) {
		return fun.apply(p1);
	}
	
	public  T invoke1WithSuper(Functions.Function1 fun, P p1) {
		return fun.apply(p1);
	}
	
	public  T invoke1WithSuperAndExtends(Functions.Function1 fun, P p1) {
		return fun.apply(p1);
	}
	
	public  T invoke2(Functions.Function2 fun, P1 p1, P2 p2) {
		return fun.apply(p1, p2);
	}
	
	public  T useProvider(Provider provider) {
		return provider.get();
	}
	
	public  T useSupplier(Supplier supplier) {
		return supplier.get();
	}
	
	public  Out useGoogleCollectFunction(Function fun, In param) {
		return fun.apply(param);
	}
	
	public  Out useGoogleCollectFunction2(Function fun, In param) {
		return fun.apply(param);
	}
	
	public  boolean useGoogleCollectPredicate(Predicate predicate, In value) {
		return predicate.apply(value);
	}
	
	public  boolean useGoogleCollectPredicate2(Predicate predicate, In value) {
		return predicate.apply(value);
	}
	
	public void useRunnable(Runnable runnable) {
		runnable.run();
	}
	
	/**
	 * @since 2.3
	 */
	public String useProcedureForCharSequence(Procedures.Procedure1 proc) {
		proc.apply(null);
		return "done";
	}
	
	public Runnable asRunnable(final Procedures.Procedure0 procedure) {
		return new Runnable() {
			@Override
			public void run() {
				procedure.apply();
			}
		};
	}
	
	public Procedures.Procedure0 asProcedure(final Runnable runnable) {
		return new Procedures.Procedure0() {
			@Override
			public void apply() {
				runnable.run();
			}
		};
	}
	
	public  Functions.Function1 getIdentityFunction() {
		return new Functions.Function1() {
			@Override
			public Obj apply(Obj p) {
				return p;
			}
			
		};
	}
	
	/**
	 * @since 2.3
	 */
	@SafeVarargs
	public final String concatStrings(Functions.Function0... functions) {
		StringBuilder result = new StringBuilder("varArgs:");
		for(Functions.Function0 function: functions) {
			result.append(function.apply());
		}
		return result.toString();
	}
	
	/**
	 * @since 2.3
	 */
	public String concatStrings(Functions.Function0 function1, Functions.Function0 function2) {
		StringBuilder result = new StringBuilder("twoArgs:");
		result.append(function1.apply());
		result.append(function2.apply());
		return result.toString();
	}
	
}