diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/ContainersStateFactory.java b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/ContainersStateFactory.java index 7ee01bbfb96..69c5da14663 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/ContainersStateFactory.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/ContainersStateFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2010, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -8,16 +8,22 @@ *******************************************************************************/ package org.eclipse.xtext.mwe; +import java.util.Collection; import java.util.List; +import java.util.Map; import org.eclipse.emf.common.util.URI; import org.eclipse.xtext.resource.containers.IAllContainersState; import org.eclipse.xtext.resource.containers.ResourceSetBasedAllContainersState; -import com.google.common.collect.Multimap; - public class ContainersStateFactory { - public IAllContainersState getContainersState(List paths, Multimap uris) { + /** Instead use {@link #getContainersState(List, Map)}. */ + @Deprecated(since = "2.35.0", forRemoval = true) + public IAllContainersState getContainersState(List paths, com.google.common.collect.Multimap uris) { + return getContainersState(paths, com.google.common.collect.Multimaps.asMap(uris)); + } + + public IAllContainersState getContainersState(List paths, Map> uris) { ResourceSetBasedAllContainersState containersState = new ResourceSetBasedAllContainersState(); containersState.configure(paths, uris); return containersState; diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/PathTraverser.java b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/PathTraverser.java index 422e2b8bda0..3b032dfe02e 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/PathTraverser.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/PathTraverser.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2010, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -9,9 +9,14 @@ package org.eclipse.xtext.mwe; import java.io.File; -import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -19,31 +24,42 @@ import org.eclipse.emf.common.util.URI; import org.eclipse.emf.common.util.WrappedException; -import com.google.common.base.Predicate; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; - /** * @author Sven Efftinge - Initial contribution and API */ public class PathTraverser { private final static Logger LOG = Logger.getLogger(PathTraverser.class); - public Multimap resolvePathes(List pathes, Predicate isValidPredicate) { - Multimap uris = HashMultimap.create(); + /** @deprecated Instead use {@link #resolvePathes(List, Predicate)} */ + @Deprecated(since = "2.35.0", forRemoval = true) + public com.google.common.collect.Multimap resolvePathes(List pathes, + com.google.common.base.Predicate isValidPredicate) { + Map> resolvedPaths = resolvePathes(pathes, (Predicate) isValidPredicate); + com.google.common.collect.Multimap multimap = com.google.common.collect.HashMultimap.create(); + resolvedPaths.forEach(multimap::putAll); + return multimap; + } + + public Map> resolvePathes(List pathes, Predicate isValidPredicate) { + Map> uris = new HashMap<>(); for (String path : pathes) { Set resourceUris = findAllResourceUris(path, isValidPredicate); - uris.putAll(path, resourceUris); + uris.computeIfAbsent(path, p -> new HashSet<>()).addAll(resourceUris); } return uris; } - + + /** @deprecated Instead use {@link #findAllResourceUris(String, Predicate)} */ + @Deprecated(since = "2.35.0", forRemoval = true) + public Set findAllResourceUris(String path, com.google.common.base.Predicate isValidPredicate) { + return findAllResourceUris(path, (Predicate) isValidPredicate); + } + public Set findAllResourceUris(String path, Predicate isValidPredicate) { File file = new File(path); if(!file.exists()) { LOG.debug("File under : " + path + " doesn't exist."); - return Sets.newHashSet(); + return new HashSet<>(); } else if (file.isDirectory()) { return traverseDir(file, isValidPredicate); } else if (file.isFile()) { @@ -52,22 +68,18 @@ public Set findAllResourceUris(String path, Predicate isValidPredicate throw new IllegalArgumentException("Unsupported path : " + path + " (only folders and archives are supported)."); } + /** @deprecated Instead use {@link #traverseArchive(File, Predicate)} */ + @Deprecated(since = "2.35.0", forRemoval = true) + protected Set traverseArchive(File file, com.google.common.base.Predicate isValidPredicate) { + return traverseArchive(file, (Predicate) isValidPredicate); + } + protected Set traverseArchive(File file, Predicate isValidPredicate) { try { - Set result = Sets.newHashSet(); - ZipFile zipFile = new ZipFile(file); - try { - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - URI uri = getUri(file, entry); - if (uri != null && isValidPredicate.apply(uri)) { - result.add(uri); - } - } - return result; - } finally { - zipFile.close(); + try (ZipFile zipFile = new ZipFile(file);) { + return zipFile.stream().map(entry -> getUri(file, entry)) // + .filter(Objects::nonNull).filter(isValidPredicate) // + .collect(Collectors.toSet()); } } catch (Exception e) { throw new WrappedException("Error traversing archive " + file, e); @@ -79,8 +91,14 @@ protected URI getUri(File file, ZipEntry entry) { return URI.createURI("archive:" + fileToArchive + "!/" + entry.getName()); } + /** @deprecated Instead use {@link #traverseDir(File, Predicate)} */ + @Deprecated(since = "2.35.0", forRemoval = true) + protected Set traverseDir(File file, final com.google.common.base.Predicate isValidPredicate) { + return traverseDir(file, (Predicate) isValidPredicate); + } + protected Set traverseDir(File file, final Predicate isValidPredicate) { - Set result = Sets.newHashSet(); + Set result = new HashSet<>(); File[] files = file.listFiles(); if (files == null) return result; @@ -89,7 +107,7 @@ protected Set traverseDir(File file, final Predicate isValidPredicate) result.addAll(traverseDir(f, isValidPredicate)); } else { URI uri = URI.createFileURI(f.getAbsolutePath()); - if (isValidPredicate.apply(uri)) { + if (isValidPredicate.test(uri)) { result.add(uri); } } diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/Reader.java b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/Reader.java index 8a50b59f35b..e9b7eb05120 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/Reader.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/Reader.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2010, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -9,8 +9,11 @@ package org.eclipse.xtext.mwe; import java.io.File; -import java.util.Collection; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Predicate; import org.apache.log4j.Logger; import org.eclipse.emf.common.util.URI; @@ -24,11 +27,6 @@ import org.eclipse.xtext.resource.containers.DelegatingIAllContainerAdapter; import org.eclipse.xtext.resource.containers.IAllContainersState; -import com.google.common.base.Predicate; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; - /** *

* A Reader used to read EMF resources from a set of pathes. @@ -44,7 +42,7 @@ *

* A {@link SlotEntry} is responsible for selecting certain EObjects from the loaded resources. * It supports selecting EObjects by their name (see {@link org.eclipse.xtext.resource.IEObjectDescription}) or by an EClass. - * In many cases such selction returns multiple EObjects, if you're only interested in one element set the firstOnly flag to true. + * In many cases such selection returns multiple EObjects, if you're only interested in one element set the firstOnly flag to true. *

*

* You might want to populate multiple workflow slots with model elements. @@ -78,7 +76,7 @@ public class Reader extends AbstractReader { protected final static Logger log = Logger.getLogger(Reader.class.getName()); - protected List pathes = Lists.newArrayList(); + protected List pathes = new ArrayList<>(); /** *

@@ -179,16 +177,15 @@ protected void checkConfigurationInternal(Issues issues) { @Override protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) { ResourceSet resourceSet = getResourceSet(); - Multimap uris = getPathTraverser().resolvePathes(pathes, new Predicate() { - @Override - public boolean apply(URI input) { - boolean result = true; - if (getUriFilter() != null) - result = getUriFilter().matches(input); - if (result) - result = getRegistry().getResourceServiceProvider(input) != null; - return result; + Map> uris = getPathTraverser().resolvePathes(pathes,(Predicate) input -> { + boolean result = true; + if (getUriFilter() != null) { + result = getUriFilter().matches(input); + } + if (result) { + result = getRegistry().getResourceServiceProvider(input) != null; } + return result; }); IAllContainersState containersState = containersStateFactory.getContainersState(pathes, uris); installAsAdapter(resourceSet, containersState); @@ -201,11 +198,8 @@ protected PathTraverser getPathTraverser() { return new PathTraverser(); } - protected void populateResourceSet(ResourceSet set, Multimap uris) { - Collection values = Sets.newHashSet(uris.values()); - for (URI uri : values) { - set.createResource(uri); - } + protected void populateResourceSet(ResourceSet set, Map> uris) { + uris.values().stream().flatMap(Set::stream).distinct().forEach(set::createResource); } protected void installAsAdapter(ResourceSet set, IAllContainersState containersState) diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/RuntimeResourceSetInitializer.java b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/RuntimeResourceSetInitializer.java index 312fe80bbbd..ab146753d8f 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/mwe/RuntimeResourceSetInitializer.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/mwe/RuntimeResourceSetInitializer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2010, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -8,7 +8,12 @@ *******************************************************************************/ package org.eclipse.xtext.mwe; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Predicate; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.ResourceSet; @@ -17,9 +22,6 @@ import org.eclipse.xtext.resource.containers.DelegatingIAllContainerAdapter; import org.eclipse.xtext.resource.containers.IAllContainersState; -import com.google.common.base.Predicate; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; import com.google.inject.Inject; import com.google.inject.Provider; @@ -37,46 +39,41 @@ public class RuntimeResourceSetInitializer { private IResourceServiceProvider.Registry registry; public List getClassPathEntries() { - List pathes = Lists.newArrayList(); + List pathes = new ArrayList<>(); String classPath = System.getProperty("java.class.path"); String separator = System.getProperty("path.separator"); String[] strings = classPath.split(separator); - for (String path : strings) { - pathes.add(path); - } + Collections.addAll(pathes, strings); return pathes; } - protected Multimap getPathToUriMap(List pathes) { + protected Map> getPathToUriMap(List pathes) { return getPathToUriMap(pathes, null); } - - protected Multimap getPathToUriMap(List pathes, final UriFilter filter) { - return traverser.resolvePathes(pathes, new Predicate() { - @Override - public boolean apply(URI input) { - boolean result = true; - if (filter != null) - result = filter.matches(input); - if (result) - result = registry.getResourceServiceProvider(input) != null; - return result; + + protected Map> getPathToUriMap(List pathes, UriFilter filter) { + return traverser.resolvePathes(pathes, (Predicate) input -> { + boolean result = true; + if (filter != null) { + result = filter.matches(input); + } + if (result) { + result = registry.getResourceServiceProvider(input) != null; } + return result; }); } public ResourceSet getInitializedResourceSet(List pathes) { return getInitializedResourceSet(pathes, null); } - + public ResourceSet getInitializedResourceSet(List pathes, UriFilter filter) { ResourceSet resourceSet = resourceSetProvider.get(); - Multimap pathToUriMap = getPathToUriMap(pathes, filter); + Map> pathToUriMap = getPathToUriMap(pathes, filter); IAllContainersState containersState = factory.getContainersState(pathes, pathToUriMap); resourceSet.eAdapters().add(new DelegatingIAllContainerAdapter(containersState)); - for (URI uri : pathToUriMap.values()) { - resourceSet.createResource(uri); - } + pathToUriMap.values().stream().flatMap(Set::stream).forEach(resourceSet::createResource); return resourceSet; } diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/resource/containers/ResourceSetBasedAllContainersState.java b/org.eclipse.xtext/src/org/eclipse/xtext/resource/containers/ResourceSetBasedAllContainersState.java index 8cd105c8aa4..8bd992eba5d 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/resource/containers/ResourceSetBasedAllContainersState.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/resource/containers/ResourceSetBasedAllContainersState.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2010, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -9,36 +9,46 @@ package org.eclipse.xtext.resource.containers; import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; import org.eclipse.emf.common.util.URI; -import com.google.common.base.Joiner; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Multimaps; -import com.google.common.collect.SetMultimap; -import com.google.common.collect.Sets; - /** * This implementation {@link IAllContainersState} associates resource (e.g. their URIs) to containers. It assumes that - * all URIs and their containers are known when {@link #configure(List, Multimap)} is called. + * all URIs and their containers are known when {@link #configure(List, Map)} is called. * * @see FlatResourceSetBasedAllContainersState * * @author Sven Efftinge - Initial contribution and API */ public class ResourceSetBasedAllContainersState implements IAllContainersState { - - private SetMultimap container2URIs; - private SetMultimap uri2container; + + private Map> container2URIs; + private Map> uri2container; private List containers; - - public void configure(List containers, Multimap container2Uris) { + + /** @deprecated Instead use {@link #configure(List, Map)} */ + @Deprecated(since = "2.35.0", forRemoval = true) + public void configure(List containers, com.google.common.collect.Multimap container2Uris) { + configure(containers, com.google.common.collect.Multimaps.asMap(container2Uris)); + } + + public void configure(List containers, Map> container2Uris) { this.containers = containers; - this.container2URIs = HashMultimap.create(container2Uris); - this.uri2container = Multimaps.invertFrom(HashMultimap.create(container2Uris), HashMultimap.create()); + this.container2URIs = container2Uris.entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, e -> Set.copyOf(e.getValue()))); + this.uri2container = new HashMap<>(); + container2Uris.forEach((container, uris) -> { + for (URI uri : uris) { + uri2container.computeIfAbsent(uri, u -> new HashSet<>()).add(container); + } + }); } @Override @@ -48,7 +58,7 @@ public List getVisibleContainerHandles(String handle) { @Override public Collection getContainedURIs(String containerHandle) { - return container2URIs.get(containerHandle); + return container2URIs.getOrDefault(containerHandle, Set.of()); } @Override @@ -69,14 +79,14 @@ public String toString() { StringBuilder result = new StringBuilder(); result.append("["); result.append(getClass().getSimpleName()); - Set invisibleContainers = Sets.newHashSet(container2URIs.keySet()); + Set invisibleContainers = new HashSet<>(container2URIs.keySet()); invisibleContainers.removeAll(containers); if (!invisibleContainers.isEmpty()) { result.append("\n WARNING: invisible containers: "); - result.append(Joiner.on(", ").join(invisibleContainers)); + result.append(String.join(", ", invisibleContainers)); } for (String container : containers) { - Collection uris = container2URIs.get(container); + Collection uris = getContainedURIs(container); result.append("\n container "); result.append(container); result.append(" = "); @@ -84,7 +94,7 @@ public String toString() { result.append("(empty)"); else { result.append("{\n "); - result.append(Joiner.on("\n ").join(uris)); + result.append(uris.stream().map(URI::toString).collect(Collectors.joining("\n "))); result.append("\n }"); } }