blob: 9dce2bc5dbdf106ee716bb89aa62d6fc0c208801 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.xtext.datamartdsl.util;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.scoping.impl.SimpleScope;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
/**
* The Class DatamartScopes.
*/
public class DatamartScopes extends Scopes {
/**
* creates a scope using the passed function to compute the names and sets
* the passed scope as the parent scope.
*
* @param <T>
* the generic type
* @param elements
* the elements
* @return the i scope
*/
public static <T extends EObject> IScope scopeForEntity(Iterable<? extends T> elements) {
return scopeForString(elements, new DatamartMemberEntityNameComputation(), IScope.NULLSCOPE);
}
/**
* creates a scope using the passed function to compute the names and sets
* the passed scope as the parent scope.
*
* @param <T>
* the generic type
* @param elements
* the elements
* @return the i scope
*/
public static <T extends EObject> IScope scopeForLEntityRef(Iterable<? extends T> elements) {
return scopeFor(elements, new LEntityRefFQNComputation(), IScope.NULLSCOPE);
}
/**
* creates a scope using the passed function to compute the names and sets
* the passed scope as the parent scope.
*
* @param <T>
* the generic type
* @param elements
* the elements
* @param nameComputation
* the name computation
* @param outer
* the outer
* @return the i scope
*/
public static <T extends EObject> IScope scopeForString(Iterable<? extends T> elements,
final Function<T, String> nameComputation, IScope outer) {
return new SimpleScope(outer,scopedElementsForString(elements, nameComputation));
}
/**
* transforms an {@link Iterable} of {@link EObject}s into an
* {@link Iterable} of {@link IEObjectDescription}s computing the name of
* the elements using the passed {@link Function} If the passed function
* returns null the object is filtered out.
*
* @param <T>
* the generic type
* @param elements
* the elements
* @param nameComputation
* the name computation
* @return the iterable
*/
public static <T extends EObject> Iterable<IEObjectDescription> scopedElementsForString(Iterable<? extends T> elements,
final Function<T, String> nameComputation) {
Iterable<IEObjectDescription> transformed = Iterables.transform(elements,
new Function<T, IEObjectDescription>() {
public IEObjectDescription apply(T from) {
final QualifiedName qualifiedName = QualifiedName.create(nameComputation.apply(from));
if (qualifiedName != null)
return new EObjectDescription(qualifiedName, from, null);
return null;
}
});
return Iterables.filter(transformed, Predicates.notNull());
}
}