org.eclipseday.xtext.example.entitydsl.utils.EntityDslScopeProviderUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipseday.xtext.example.entitydsl.utils.EntityDslScopeProviderUtil.java

Source

/*******************************************************************************
 * Copyright (c) 2013 Neeraj Bhusare.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Neeraj Bhusare - initial API and implementation 
 *******************************************************************************/
package org.eclipseday.xtext.example.entitydsl.utils;

import java.util.Collections;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

/**
 * @author Neeraj Bhusare
 */
public final class EntityDslScopeProviderUtil {

    /**
     * 
     * @param scope
     * @param resourceSet
     * @return
     */
    public static Set<EObject> unpack(final IScope scope, final ResourceSet resourceSet) {
        final Function<IEObjectDescription, EObject> function = new Function<IEObjectDescription, EObject>() {
            @Override
            public EObject apply(IEObjectDescription eObjectDescription) {
                return getEObject(eObjectDescription, resourceSet);
            }
        };

        final Iterable<EObject> unpackaged = Iterables.transform(scope.getAllElements(), function);
        return Collections.unmodifiableSet(Sets.newLinkedHashSet(unpackaged));
    }

    /**
     * 
     * @param eObjectDescription
     * @param resourceSet
     * @return
     */
    public static EObject getEObject(final IEObjectDescription eObjectDescription, final ResourceSet resourceSet) {
        final EObject eObject = eObjectDescription.getEObjectOrProxy();
        return resolve(eObject, resourceSet);
    }

    /**
     * 
     * @param eObject
     * @param resourceSet
     * @return
     */
    private static EObject resolve(final EObject eObject, final ResourceSet resourceSet) {
        return eObject.eIsProxy() ? EcoreUtil.resolve(eObject, resourceSet) : eObject;
    }

}