Skip to content

Commit

Permalink
Replace Guava-using APIs in ContainersStateFactory and its dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Apr 5, 2024
1 parent 77020bb commit 07be8f4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<String> paths, Multimap<String, URI> uris) {
/** Instead use {@link #getContainersState(List, Map)}. */
@Deprecated(since = "2.35.0", forRemoval = true)
public IAllContainersState getContainersState(List<String> paths, com.google.common.collect.Multimap<String, URI> uris) {
return getContainersState(paths, com.google.common.collect.Multimaps.asMap(uris));
}

public IAllContainersState getContainersState(List<String> paths, Map<String, ? extends Collection<URI>> uris) {
ResourceSetBasedAllContainersState containersState = new ResourceSetBasedAllContainersState();
containersState.configure(paths, uris);
return containersState;
Expand Down
73 changes: 45 additions & 28 deletions org.eclipse.xtext/src/org/eclipse/xtext/mwe/PathTraverser.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,41 +9,56 @@
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;

import org.apache.log4j.Logger;
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<String, URI> resolvePathes(List<String> pathes, Predicate<URI> isValidPredicate) {
Multimap<String, URI> uris = HashMultimap.create();
/** @deprecated Instead use {@link #resolvePathes(List, Predicate)} */
@Deprecated(since = "2.35.0", forRemoval = true)
@SuppressWarnings({ "unchecked", "rawtypes" })
public com.google.common.collect.Multimap<String, URI> resolvePathes(List<String> pathes,
com.google.common.base.Predicate<URI> isValidPredicate) {
Map<String, Set<URI>> resolvedPaths = resolvePathes(pathes, (Predicate<URI>) isValidPredicate);
return com.google.common.collect.Multimaps.newSetMultimap((Map) resolvedPaths, HashSet::new);
}

public Map<String, Set<URI>> resolvePathes(List<String> pathes, Predicate<URI> isValidPredicate) {
Map<String, Set<URI>> uris = new HashMap<>();
for (String path : pathes) {
Set<URI> 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<URI> findAllResourceUris(String path, com.google.common.base.Predicate<URI> isValidPredicate) {
return findAllResourceUris(path, (Predicate<URI>) isValidPredicate);
}

public Set<URI> findAllResourceUris(String path, Predicate<URI> 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()) {
Expand All @@ -52,22 +67,18 @@ public Set<URI> findAllResourceUris(String path, Predicate<URI> 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<URI> traverseArchive(File file, com.google.common.base.Predicate<URI> isValidPredicate) {
return traverseArchive(file, (Predicate<URI>) isValidPredicate);
}

protected Set<URI> traverseArchive(File file, Predicate<URI> isValidPredicate) {
try {
Set<URI> result = Sets.newHashSet();
ZipFile zipFile = new ZipFile(file);
try {
Enumeration<? extends ZipEntry> 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);
Expand All @@ -79,8 +90,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<URI> traverseDir(File file, final com.google.common.base.Predicate<URI> isValidPredicate) {
return traverseDir(file, (Predicate<URI>) isValidPredicate);
}

protected Set<URI> traverseDir(File file, final Predicate<URI> isValidPredicate) {
Set<URI> result = Sets.newHashSet();
Set<URI> result = new HashSet<>();
File[] files = file.listFiles();
if (files == null)
return result;
Expand All @@ -89,7 +106,7 @@ protected Set<URI> traverseDir(File file, final Predicate<URI> isValidPredicate)
result.addAll(traverseDir(f, isValidPredicate));
} else {
URI uri = URI.createFileURI(f.getAbsolutePath());
if (isValidPredicate.apply(uri)) {
if (isValidPredicate.test(uri)) {
result.add(uri);
}
}
Expand Down
40 changes: 17 additions & 23 deletions org.eclipse.xtext/src/org/eclipse/xtext/mwe/Reader.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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;

/**
* <p>
* A Reader used to read EMF resources from a set of pathes.
Expand All @@ -44,7 +42,7 @@
* <p>
* 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 <code>firstOnly</code> flag to <code>true</code>.
* In many cases such selection returns multiple EObjects, if you're only interested in one element set the <code>firstOnly</code> flag to <code>true</code>.
* </p>
* <p>
* You might want to populate multiple workflow slots with model elements.
Expand Down Expand Up @@ -78,7 +76,7 @@
public class Reader extends AbstractReader {

protected final static Logger log = Logger.getLogger(Reader.class.getName());
protected List<String> pathes = Lists.newArrayList();
protected List<String> pathes = new ArrayList<>();

/**
* <p>
Expand Down Expand Up @@ -179,16 +177,15 @@ protected void checkConfigurationInternal(Issues issues) {
@Override
protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) {
ResourceSet resourceSet = getResourceSet();
Multimap<String, URI> uris = getPathTraverser().resolvePathes(pathes, new Predicate<URI>() {
@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<String, Set<URI>> uris = getPathTraverser().resolvePathes(pathes,(Predicate<URI>) 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);
Expand All @@ -201,11 +198,8 @@ protected PathTraverser getPathTraverser() {
return new PathTraverser();
}

protected void populateResourceSet(ResourceSet set, Multimap<String, URI> uris) {
Collection<URI> values = Sets.newHashSet(uris.values());
for (URI uri : values) {
set.createResource(uri);
}
protected void populateResourceSet(ResourceSet set, Map<String, Set<URI>> uris) {
uris.values().stream().flatMap(Set::stream).distinct().forEach(set::createResource);
}

protected void installAsAdapter(ResourceSet set, IAllContainersState containersState)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -8,7 +8,11 @@
*******************************************************************************/
package org.eclipse.xtext.mwe;

import java.util.ArrayList;
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;
Expand All @@ -17,9 +21,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;

Expand All @@ -37,7 +38,7 @@ public class RuntimeResourceSetInitializer {
private IResourceServiceProvider.Registry registry;

public List<String> getClassPathEntries() {
List<String> pathes = Lists.newArrayList();
List<String> pathes = new ArrayList<>();
String classPath = System.getProperty("java.class.path");
String separator = System.getProperty("path.separator");
String[] strings = classPath.split(separator);
Expand All @@ -47,36 +48,33 @@ public List<String> getClassPathEntries() {
return pathes;
}

protected Multimap<String, URI> getPathToUriMap(List<String> pathes) {
protected Map<String, Set<URI>> getPathToUriMap(List<String> pathes) {
return getPathToUriMap(pathes, null);
}

protected Multimap<String, URI> getPathToUriMap(List<String> pathes, final UriFilter filter) {
return traverser.resolvePathes(pathes, new Predicate<URI>() {
@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<String, Set<URI>> getPathToUriMap(List<String> pathes, UriFilter filter) {
return traverser.resolvePathes(pathes, (Predicate<URI>) input -> {
boolean result = true;
if (filter != null) {
result = filter.matches(input);
}
if (result) {
result = registry.getResourceServiceProvider(input) != null;
}
return result;
});
}

public ResourceSet getInitializedResourceSet(List<String> pathes) {
return getInitializedResourceSet(pathes, null);
}

public ResourceSet getInitializedResourceSet(List<String> pathes, UriFilter filter) {
ResourceSet resourceSet = resourceSetProvider.get();
Multimap<String, URI> pathToUriMap = getPathToUriMap(pathes, filter);
Map<String, Set<URI>> 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;
}

Expand Down
Loading

0 comments on commit 07be8f4

Please sign in to comment.