Search in sources :

Example 1 with ForwardingMap

use of com.google.common.collect.ForwardingMap in project xtext-eclipse by eclipse.

the class Storage2UriMapperJavaImpl method clearCache.

protected void clearCache(IJavaProject project, Set<PackageFragmentRootData> toBeKept) {
    Collection<PackageFragmentRootData> values;
    synchronized (cachedPackageFragmentRootData) {
        values = newArrayList(cachedPackageFragmentRootData.values());
    }
    List<PackageFragmentRootData> toBeRemoved = newArrayList();
    for (PackageFragmentRootData data : values) {
        if (toBeKept.contains(data)) {
            continue;
        }
        Map<String, IPackageFragmentRoot> associatedRoots = data.associatedRoots;
        synchronized (associatedRoots) {
            Iterator<IPackageFragmentRoot> i = associatedRoots.values().iterator();
            IPackageFragmentRoot someRoot = null;
            boolean didChange = false;
            while (i.hasNext()) {
                IPackageFragmentRoot root = i.next();
                if (project.equals(root.getJavaProject())) {
                    i.remove();
                    didChange = true;
                } else if (someRoot == null) {
                    someRoot = root;
                }
            }
            if (associatedRoots.size() == 0) {
                toBeRemoved.add(data);
            } else if (didChange) {
                // get rid of cached storages that still point to roots / projects that are no longer available
                // and recompute them lazily on demand
                final IPackageFragmentRoot rootToProcess = someRoot;
                data.uri2Storage = new ForwardingMap<URI, IStorage>() {

                    Map<URI, IStorage> delegate;

                    @Override
                    protected Map<URI, IStorage> delegate() {
                        if (delegate == null) {
                            return delegate = initializeData(rootToProcess).uri2Storage;
                        }
                        return delegate;
                    }
                };
            }
        }
    }
    if (!toBeRemoved.isEmpty()) {
        synchronized (cachedPackageFragmentRootData) {
            cachedPackageFragmentRootData.values().removeAll(toBeRemoved);
        }
    }
}
Also used : ForwardingMap(com.google.common.collect.ForwardingMap) IStorage(org.eclipse.core.resources.IStorage) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ForwardingMap(com.google.common.collect.ForwardingMap) URI(org.eclipse.emf.common.util.URI) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 2 with ForwardingMap

use of com.google.common.collect.ForwardingMap in project xtext-eclipse by eclipse.

the class DefaultReferenceFinder method createExportedElementsMap.

@Deprecated
protected Map<EObject, URI> createExportedElementsMap(final Resource resource) {
    return new ForwardingMap<EObject, URI>() {

        private Map<EObject, URI> delegate;

        @Override
        protected Map<EObject, URI> delegate() {
            if (delegate != null) {
                return delegate;
            }
            URI uri = EcoreUtil2.getPlatformResourceOrNormalizedURI(resource);
            IResourceServiceProvider resourceServiceProvider = getServiceProviderRegistry().getResourceServiceProvider(uri);
            if (resourceServiceProvider == null) {
                return delegate = Collections.emptyMap();
            }
            IResourceDescription.Manager resourceDescriptionManager = resourceServiceProvider.getResourceDescriptionManager();
            if (resourceDescriptionManager == null) {
                return delegate = Collections.emptyMap();
            }
            IResourceDescription resourceDescription = resourceDescriptionManager.getResourceDescription(resource);
            Map<EObject, URI> exportedElementMap = newIdentityHashMap();
            if (resourceDescription != null) {
                for (IEObjectDescription exportedEObjectDescription : resourceDescription.getExportedObjects()) {
                    EObject eObject = resource.getEObject(exportedEObjectDescription.getEObjectURI().fragment());
                    if (eObject != null)
                        exportedElementMap.put(eObject, exportedEObjectDescription.getEObjectURI());
                }
            }
            return delegate = exportedElementMap;
        }
    };
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) ForwardingMap(com.google.common.collect.ForwardingMap) Map(java.util.Map) ForwardingMap(com.google.common.collect.ForwardingMap) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

ForwardingMap (com.google.common.collect.ForwardingMap)2 Map (java.util.Map)2 URI (org.eclipse.emf.common.util.URI)2 LinkedHashMap (java.util.LinkedHashMap)1 IStorage (org.eclipse.core.resources.IStorage)1 EObject (org.eclipse.emf.ecore.EObject)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1