Skip to content

Commit

Permalink
Various internal code simplifications and modernizations
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Aug 24, 2024
1 parent 6c31185 commit 0b4bcac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2021 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 @@ -12,6 +12,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.function.Function;

import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl.EObjectInputStream;
Expand All @@ -21,8 +22,6 @@
import org.junit.Assert;
import org.junit.Test;

import com.google.common.base.Function;

/**
* @author Jan Koehnlein - Initial contribution and API
*/
Expand Down Expand Up @@ -213,13 +212,7 @@ public void testAppendNull() {
}

@Test public void testWrapper() throws Exception {
Function<String, String> identity = new Function<String, String>() {
@Override
public String apply(String from) {
return from;
}
};
Function<String, QualifiedName> wrapper = QualifiedName.wrapper(identity);
Function<String, QualifiedName> wrapper = QualifiedName.wrapper(from -> from);
assertEquals(QualifiedName.create(""), wrapper.apply(""));
assertEquals(null, wrapper.apply(null));
assertEquals("foo", wrapper.apply("foo").getLastSegment());
Expand Down
29 changes: 10 additions & 19 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,9 +9,11 @@
package org.eclipse.xtext.mwe;

import java.io.File;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -22,7 +24,6 @@
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
Expand All @@ -43,7 +44,7 @@ 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 @@ -54,20 +55,10 @@ public Set<URI> findAllResourceUris(String path, 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.toCollection(HashSet::new));
}
} catch (Exception e) {
throw new WrappedException("Error traversing archive " + file, e);
Expand All @@ -80,7 +71,7 @@ protected URI getUri(File file, ZipEntry entry) {
}

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 Down
37 changes: 15 additions & 22 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,7 +9,7 @@
package org.eclipse.xtext.mwe;

import java.io.File;
import java.util.Collection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
Expand All @@ -24,16 +24,13 @@
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.
* A Reader used to read EMF resources from a set of paths.
* A path can point to a folder or an archive (zips and jars are supported).
* Those pathes are recursively scanned and all resources for which an {@link IResourceServiceProvider} is
* Those paths are recursively scanned and all resources for which an {@link IResourceServiceProvider} is
* registered in the {@link org.eclipse.xtext.resource.IResourceServiceProvider.Registry} will be available.
* </p>
*
Expand All @@ -44,7 +41,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 section 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 +75,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 +176,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;
Multimap<String, URI> uris = getPathTraverser().resolvePathes(pathes, 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 @@ -202,10 +198,7 @@ protected PathTraverser getPathTraverser() {
}

protected void populateResourceSet(ResourceSet set, Multimap<String, URI> uris) {
Collection<URI> values = Sets.newHashSet(uris.values());
for (URI uri : values) {
set.createResource(uri);
}
uris.values().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,6 +8,8 @@
*******************************************************************************/
package org.eclipse.xtext.mwe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.util.URI;
Expand All @@ -17,8 +19,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,38 +37,29 @@ public class RuntimeResourceSetInitializer {
private IResourceServiceProvider.Registry registry;

public List<String> getClassPathEntries() {
List<String> pathes = Lists.newArrayList();
List<String> paths = 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);
}
return pathes;
Collections.addAll(paths, strings);
return paths;
}

protected Multimap<String, 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;
}
return traverser.resolvePathes(pathes, input -> {
boolean result = filter == null || filter.matches(input);
return result && registry.getResourceServiceProvider(input) != null;
});
}

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2018 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 Down Expand Up @@ -243,14 +243,12 @@ public static QualifiedName create(String singleSegment) {
* Wraps a name function to return a qualified name. Returns null if the name function returns null.
*/
public static <F> Function<F, QualifiedName> wrapper(final Function<F, String> nameFunction) {
return new Function<F, QualifiedName>() {
@Override
public QualifiedName apply(F from) {
String name = nameFunction.apply(from);
if (name == null)
return null;
return QualifiedName.create(name);
return from -> {
String name = nameFunction.apply(from);
if (name == null) {
return null;
}
return QualifiedName.create(name);
};
}

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 @@ -9,6 +9,7 @@
package org.eclipse.xtext.resource.containers;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand All @@ -19,7 +20,6 @@
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
Expand All @@ -38,7 +38,7 @@ public class ResourceSetBasedAllContainersState implements IAllContainersState {
public void configure(List<String> containers, Multimap<String, URI> container2Uris) {
this.containers = containers;
this.container2URIs = HashMultimap.create(container2Uris);
this.uri2container = Multimaps.invertFrom(HashMultimap.create(container2Uris), HashMultimap.<URI, String>create());
this.uri2container = Multimaps.invertFrom(this.container2URIs, HashMultimap.create());
}

@Override
Expand Down Expand Up @@ -69,11 +69,11 @@ public String toString() {
StringBuilder result = new StringBuilder();
result.append("[");
result.append(getClass().getSimpleName());
Set<String> invisibleContainers = Sets.newHashSet(container2URIs.keySet());
Set<String> 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<URI> uris = container2URIs.get(container);
Expand Down
Loading

0 comments on commit 0b4bcac

Please sign in to comment.