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

org.eclipse.xtext.resource.impl.EObjectDescriptionLookUp Maven / Gradle / Ivy

There is a newer version: 2.4.3
Show newest version
/*******************************************************************************
 * Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others.
 * 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
 *******************************************************************************/
package org.eclipse.xtext.resource.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.ISelectable;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author Sebastian Zarnekow - Initial contribution and API
 * @author Sven Efftinge
 */
public class EObjectDescriptionLookUp implements ISelectable {
	
	private volatile Map> nameToObjects;
	
	private volatile List allDescriptions;

	public EObjectDescriptionLookUp(List allDescriptions) {
		setExportedObjects(allDescriptions);
	}
	
	public boolean isEmpty() {
		return allDescriptions.isEmpty();
	}
	
	public Iterable getExportedObjectsByType(final EClass type) {
		if (allDescriptions.isEmpty())
			return Collections.emptyList();
		return Iterables.filter(allDescriptions, new Predicate() {
			public boolean apply(IEObjectDescription input) {
				return EcoreUtil2.isAssignableFrom(type, input.getEClass());
			}
		});
	}
	
	public Iterable getExportedObjectsByObject(final EObject object) {
		if (allDescriptions.isEmpty())
			return Collections.emptyList();
		final URI uri = EcoreUtil2.getNormalizedURI(object);
		return Iterables.filter(allDescriptions, new Predicate() {
			public boolean apply(IEObjectDescription input) {
				if (input.getEObjectOrProxy() == object)
					return true;
				if (uri.equals(input.getEObjectURI())) {
					return true;
				}
				return false;
			}
		});
	}
	
	public Iterable getExportedObjects(final EClass type, final QualifiedName name, boolean ignoreCase) {
		if (allDescriptions.isEmpty())
			return Collections.emptyList();
		QualifiedName lowerCase = name.toLowerCase();
		List values = getNameToObjects().get(lowerCase);
		if (values == null)
			return Collections.emptyList();
		Predicate predicate = ignoreCase 
			?	new Predicate() {
					public boolean apply(IEObjectDescription input) {
						return EcoreUtil2.isAssignableFrom(type, input.getEClass());
					}
				}
			:	new Predicate() {
				public boolean apply(IEObjectDescription input) {
					return name.equals(input.getName()) && EcoreUtil2.isAssignableFrom(type, input.getEClass());
				}
			};
		return Iterables.filter(values, predicate);
	}
	
	public Iterable getExportedObjects() {
		return allDescriptions;
	}

	public void setExportedObjects(List allDescriptions) {
		synchronized (this) {
			this.allDescriptions = allDescriptions;
			this.nameToObjects = null;			
		}
	}

	protected Map> getNameToObjects() {
		if (nameToObjects == null) {
			synchronized (this) {
				if (nameToObjects == null) {
					Map> nameToObjects = Maps.newHashMapWithExpectedSize(allDescriptions.size());
					if (allDescriptions instanceof RandomAccess) {
						for(int i = 0; i < allDescriptions.size(); i++) {
							IEObjectDescription description = allDescriptions.get(i);
							putIntoMap(nameToObjects, description);
						}
					} else {
						for(IEObjectDescription description: allDescriptions) {
							putIntoMap(nameToObjects, description);
						}
					}
					this.nameToObjects = nameToObjects; 
				}
			}
		}
		return this.nameToObjects;
	}

	protected void putIntoMap(Map> nameToObjects,
			IEObjectDescription description) {
		QualifiedName indexKey = description.getName().toLowerCase();
		List values = nameToObjects.get(indexKey);
		if (values == null) {
			values = Lists.newArrayListWithExpectedSize(2);
			nameToObjects.put(indexKey, values);
		}
		values.add(description);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy