| /** |
| * |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| * |
| * This copyright notice shows up in the generated Java code |
| * |
| */ |
| package org.eclipse.osbp.xtext.oxtype.scoping.eobject |
| |
| import com.google.common.collect.Maps |
| import java.util.Collections |
| import java.util.Map |
| import org.eclipse.osbp.xtext.oxtype.scoping.ScopingMetrics |
| import org.eclipse.xtext.naming.QualifiedName |
| import org.eclipse.xtext.resource.IEObjectDescription |
| import org.eclipse.xtext.scoping.IScope |
| import org.eclipse.xtext.scoping.impl.AbstractScope |
| |
| class CachingEObjectScope extends AbstractScope { |
| |
| private final Map<QualifiedName, IEObjectDescription> cache; |
| |
| private Object cacheKey |
| |
| private ScopingMetrics metrics |
| |
| new(Object cacheKey, ScopingMetrics metrics, IScope parent, boolean ignoreCase) { |
| super(parent, ignoreCase) |
| this.cacheKey = cacheKey; |
| this.metrics = metrics; |
| this.cache = Maps.newHashMapWithExpectedSize(50); |
| } |
| |
| override IEObjectDescription getSingleElement(QualifiedName name) { |
| var IEObjectDescription cached = cache.get(name); |
| if (cached == null) { |
| if (cache.containsKey(name)) { |
| return null; |
| } |
| cached = super.getSingleElement(name); |
| |
| // install an indicator adapter to indicate the eobject, that is was scoped import scopings |
| cache.put(name, cached); |
| } else { |
| metrics.cacheHit(cacheKey, cached.name.toString) |
| } |
| return cached; |
| } |
| |
| override Iterable<IEObjectDescription> getElements(QualifiedName name) { |
| val Iterable<IEObjectDescription> elements = super.getElements(name) |
| elements.forEach [ |
| if (!cache.containsKey(it.name)) { |
| cache.put(it.name, it) |
| } |
| ] |
| return elements |
| } |
| |
| override protected Iterable<IEObjectDescription> getAllLocalElements() { |
| return Collections.emptyList |
| } |
| |
| } |