From e43173ba7720b1f7d15389319785f02956ea35e6 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 14 Apr 2024 09:23:29 +0200 Subject: [PATCH] Various internal code simplifications and modernizations --- .../xtext/naming/QualifiedNameTest.java | 13 ++---- .../org/eclipse/xtext/mwe/PathTraverser.java | 29 ++++-------- .../src/org/eclipse/xtext/mwe/Reader.java | 37 ++++++--------- .../mwe/RuntimeResourceSetInitializer.java | 31 +++++-------- .../eclipse/xtext/naming/QualifiedName.java | 14 +++--- .../ResourceSetBasedAllContainersState.java | 10 ++-- .../src/org/eclipse/xtext/scoping/Scopes.java | 46 +++++++------------ 7 files changed, 66 insertions(+), 114 deletions(-) diff --git a/org.eclipse.xtext.tests/src/org/eclipse/xtext/naming/QualifiedNameTest.java b/org.eclipse.xtext.tests/src/org/eclipse/xtext/naming/QualifiedNameTest.java index fea101bfea3..90ee74c0c93 100644 --- a/org.eclipse.xtext.tests/src/org/eclipse/xtext/naming/QualifiedNameTest.java +++ b/org.eclipse.xtext.tests/src/org/eclipse/xtext/naming/QualifiedNameTest.java @@ -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. @@ -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; @@ -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 */ @@ -213,13 +212,7 @@ public void testAppendNull() { } @Test public void testWrapper() throws Exception { - Function identity = new Function() { - @Override - public String apply(String from) { - return from; - } - }; - Function wrapper = QualifiedName.wrapper(identity); + Function wrapper = QualifiedName.wrapper(from -> from); assertEquals(QualifiedName.create(""), wrapper.apply("")); assertEquals(null, wrapper.apply(null)); assertEquals("foo", wrapper.apply("foo").getLastSegment()); 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..9c9bae31a2f 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,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; @@ -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 @@ -43,7 +44,7 @@ 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()) { @@ -54,20 +55,10 @@ public Set findAllResourceUris(String path, 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.toCollection(HashSet::new)); } } catch (Exception e) { throw new WrappedException("Error traversing archive " + file, e); @@ -80,7 +71,7 @@ protected URI getUri(File file, ZipEntry entry) { } 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; 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..f4857897603 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,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; @@ -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; /** *

- * 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. *

* @@ -44,7 +41,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 section 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 +75,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 +176,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; + Multimap 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); @@ -202,10 +198,7 @@ protected PathTraverser getPathTraverser() { } protected void populateResourceSet(ResourceSet set, Multimap uris) { - Collection 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) 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..74348803305 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,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; @@ -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; @@ -37,38 +37,29 @@ public class RuntimeResourceSetInitializer { private IResourceServiceProvider.Registry registry; public List getClassPathEntries() { - List pathes = Lists.newArrayList(); + List 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 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; - } + return traverser.resolvePathes(pathes, input -> { + boolean result = filter == null || filter.matches(input); + return result && registry.getResourceServiceProvider(input) != null; }); } 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); diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/naming/QualifiedName.java b/org.eclipse.xtext/src/org/eclipse/xtext/naming/QualifiedName.java index de8ae22fd4e..7cc0874f03b 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/naming/QualifiedName.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/naming/QualifiedName.java @@ -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. @@ -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 Function wrapper(final Function nameFunction) { - return new Function() { - @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); }; } 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..2664084a3ba 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,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; @@ -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 @@ -38,7 +38,7 @@ public class ResourceSetBasedAllContainersState implements IAllContainersState { public void configure(List containers, Multimap container2Uris) { this.containers = containers; this.container2URIs = HashMultimap.create(container2Uris); - this.uri2container = Multimaps.invertFrom(HashMultimap.create(container2Uris), HashMultimap.create()); + this.uri2container = Multimaps.invertFrom(this.container2URIs, HashMultimap.create()); } @Override @@ -69,11 +69,11 @@ 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); diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/scoping/Scopes.java b/org.eclipse.xtext/src/org/eclipse/xtext/scoping/Scopes.java index 66a4d4864f6..4dbc49eb9fe 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/scoping/Scopes.java +++ b/org.eclipse.xtext/src/org/eclipse/xtext/scoping/Scopes.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2009, 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. @@ -10,6 +10,8 @@ package org.eclipse.xtext.scoping; import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import org.eclipse.emf.ecore.EAttribute; @@ -23,12 +25,9 @@ import org.eclipse.xtext.util.SimpleAttributeResolver; import com.google.common.base.Function; -import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Iterables; import com.google.common.collect.LinkedHashMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import com.google.common.collect.Multimap; /** @@ -40,12 +39,7 @@ public class Scopes { public static Iterable selectCompatible(Iterable exportedObjects, final EClass clazz) { - return Iterables.filter(exportedObjects, new Predicate() { - @Override - public boolean apply(IEObjectDescription input) { - return EcoreUtil2.isAssignableFrom(clazz,input.getEClass()); - } - }); + return Iterables.filter(exportedObjects, input -> EcoreUtil2.isAssignableFrom(clazz,input.getEClass())); } /** @@ -67,7 +61,7 @@ public static IScope scopeFor(Iterable elements, IScope outer * creates a scope using the passed function to compute the names and sets the passed scope as the parent scope */ public static IScope scopeFor(Iterable elements, - final Function nameComputation, IScope outer) { + Function nameComputation, IScope outer) { return new SimpleScope(outer,scopedElementsFor(elements, nameComputation)); } @@ -86,17 +80,13 @@ public static Iterable scopedElementsFor(Iterable Iterable scopedElementsFor(Iterable elements, - final Function nameComputation) { - Iterable transformed = Iterables.transform(elements, - new Function() { - @Override - public IEObjectDescription apply(T from) { - final QualifiedName qualifiedName = nameComputation.apply(from); - if (qualifiedName != null) - return new EObjectDescription(qualifiedName, from, null); - return null; - } - }); + Function nameComputation) { + Iterable transformed = Iterables.transform(elements, from -> { + QualifiedName qualifiedName = nameComputation.apply(from); + if (qualifiedName != null) + return new EObjectDescription(qualifiedName, from, null); + return null; + }); return Iterables.filter(transformed, Predicates.notNull()); } @@ -104,7 +94,8 @@ public IEObjectDescription apply(T from) { * indexes the IEObject description using the given */ public static Multimap index(Iterable descriptions, Function indexer) { - ArrayList list = Lists.newArrayList(descriptions); + List list = new ArrayList<>(); + descriptions.forEach(list::add); LinkedHashMultimap multimap = LinkedHashMultimap.create(list.size(),1); for (IEObjectDescription desc : list) { multimap.put(indexer.apply(desc), desc); @@ -116,16 +107,11 @@ public static Multimap index(Iterable index(Iterable descriptions) { - return index(descriptions, new Function() { - @Override - public QualifiedName apply(IEObjectDescription from) { - return from.getName().toLowerCase(); - } - }); + return index(descriptions, from -> from.getName().toLowerCase()); } public static Iterable filterDuplicates(Iterable filtered) { - Map result = Maps.newLinkedHashMap(); + Map result = new LinkedHashMap<>(); for (IEObjectDescription e : filtered) { QualifiedName qualifiedName = e.getName(); if (result.containsKey(qualifiedName)) {