Skip to content

Commit

Permalink
[146] Remove Guava dependency in oes.ecore.extender
Browse files Browse the repository at this point in the history
Bug: #146
Signed-off-by: Pierre-Charles David <[email protected]>
  • Loading branch information
pcdavid committed Jul 31, 2024
1 parent b24e22b commit e874a1a
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Bundle-Version: 7.4.3.qualifier
Bundle-Vendor: %providerName
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.8.0",
org.eclipse.emf.ecore.xmi;bundle-version="2.8.1",
org.eclipse.emf.edit;bundle-version="2.8.0",
com.google.guava;bundle-version="27.0"
org.eclipse.emf.edit;bundle-version="2.8.0"
Export-Package: org.eclipse.sirius.ecore.extender.business.api.accessor;version="3.0.0",
org.eclipse.sirius.ecore.extender.business.api.accessor.exception;version="3.0.0",
org.eclipse.sirius.ecore.extender.business.api.permission;version="2.0.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2010 THALES GLOBAL SERVICES.
* Copyright (c) 2007, 2024 THALES GLOBAL SERVICES and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -18,6 +18,9 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.StreamSupport;

import org.eclipse.emf.common.util.AbstractTreeIterator;
import org.eclipse.emf.common.util.TreeIterator;
Expand All @@ -27,10 +30,6 @@
import org.eclipse.sirius.ecore.extender.business.internal.common.ExtenderDescriptor;
import org.eclipse.sirius.ext.emf.EReferencePredicate;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;

/**
* Aggregate more {@link IMetamodelExtender}'s using a priority system.
*
Expand Down Expand Up @@ -185,6 +184,7 @@ public String eContainingFeatureName(final EObject instance) {
return null;
}


@Override
public Iterator<EObject> eContents(final EObject root) {
final List<Iterator<EObject>> iterators = new ArrayList<>();
Expand All @@ -195,11 +195,12 @@ public Iterator<EObject> eContents(final EObject root) {
}
}
if (!iterators.isEmpty()) {
return Iterators.concat(iterators.iterator());
return iterators.stream()
.flatMap(iterator -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false))
.iterator();
}
return Collections.<EObject> emptyList().iterator();
}

/**
* Check whether the given instance should block the eAllContent browsing or
* not.
Expand Down Expand Up @@ -238,7 +239,7 @@ public EObject eDelete(EObject objectToRemove, ECrossReferenceAdapter xref, ERef

@Override
public Collection<EObject> eRemoveInverseCrossReferences(EObject eObject, ECrossReferenceAdapter xref, EReferencePredicate isReferencesToIgnorePredicate) {
Collection<EObject> impactedEObjects = new LinkedHashSet<EObject>();
Collection<EObject> impactedEObjects = new LinkedHashSet<>();
for (final IMetamodelExtender extender : getActivatedExtenders()) {
impactedEObjects.addAll(extender.eRemoveInverseCrossReferences(eObject, xref, isReferencesToIgnorePredicate));
}
Expand Down Expand Up @@ -336,7 +337,9 @@ public Iterator<String> getContributedAttributeNames(final EObject next) {
for (final IMetamodelExtender extender : getActivatedExtenders()) {
iterators.add(extender.getContributedAttributeNames(next));
}
return Iterators.concat(iterators.iterator());
return iterators.stream()
.flatMap(iterator -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false))
.iterator();
}

@Override
Expand All @@ -345,8 +348,9 @@ public Iterator<String> getContributedReferenceNames(final EObject instance) {
for (final IMetamodelExtender extender : getActivatedExtenders()) {
iterators.add(extender.getContributedReferenceNames(instance));
}
return Iterators.concat(iterators.iterator());

return iterators.stream()
.flatMap(iterator -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false))
.iterator();
}

@Override
Expand Down Expand Up @@ -477,12 +481,7 @@ public void deactivate() {
*/
protected synchronized Iterable<IMetamodelExtender> getActivatedExtenders() {
if (activeExtenders == null) {
activeExtenders = Iterables.filter(extenders, new Predicate<IMetamodelExtender>() {
@Override
public boolean apply(IMetamodelExtender extender) {
return extender.isActive();
}
});
activeExtenders = () -> extenders.stream().filter(IMetamodelExtender::isActive).iterator();
}
return activeExtenders;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2012 THALES GLOBAL SERVICES.
* Copyright (c) 2007, 2024 THALES GLOBAL SERVICES and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,11 +12,11 @@
*******************************************************************************/
package org.eclipse.sirius.ecore.extender.business.api.accessor;

import java.util.Objects;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;

import com.google.common.base.Objects;

/**
* A descriptors for EPackages.
*
Expand Down Expand Up @@ -75,11 +75,6 @@ private boolean isInPlugin(final EPackage package1) {
return false;
}

/**
* {@inheritDoc}
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -89,25 +84,17 @@ public int hashCode() {
return result;
}

/**
* {@inheritDoc}
*
* @see java.lang.Object#equals(java.lang.Object)
*/
// CHECKSTYLE:OFF
@Override
public boolean equals(Object obj) {

if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

final EcoreMetamodelDescriptor other = (EcoreMetamodelDescriptor) obj;

return ePackage == other.ePackage && Objects.equal(nsURI, other.nsURI);
EcoreMetamodelDescriptor other = (EcoreMetamodelDescriptor) obj;
return ePackage == other.ePackage && Objects.equals(nsURI, other.nsURI);
// CHECKSTYLE:ON
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2011 THALES GLOBAL SERVICES.
* Copyright (c) 2007, 2024 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -15,12 +15,17 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
Expand All @@ -39,13 +44,6 @@
import org.eclipse.sirius.ecore.extender.business.api.accessor.MetamodelDescriptor;
import org.eclipse.sirius.ext.emf.EReferencePredicate;

import com.google.common.base.Predicates;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

/**
* This metamodel Extender accesses the intrinsic data of an EObject.
*
Expand All @@ -58,9 +56,9 @@ public class EcoreIntrinsicExtender extends AbstractMetamodelExtender {

private static final String AQL_SEPARATOR = "::"; //$NON-NLS-1$

private static PackageRegistryIndex platformIndex = new PackageRegistryIndex(EPackage.Registry.INSTANCE, Predicates.<EPackage> alwaysTrue());
private static PackageRegistryIndex platformIndex = new PackageRegistryIndex(EPackage.Registry.INSTANCE, p -> true);

private Multimap<String, EClass> viewpointIndex = HashMultimap.create();
private Map<String, List<EClass>> viewpointIndex = new HashMap<>();

private Collection<? extends MetamodelDescriptor> lastDescriptors;

Expand Down Expand Up @@ -377,20 +375,27 @@ public void init(final ResourceSet set) {
}

private void addTypesToSiriusIndex(final EPackage value) {
for (EClass cur : Iterables.filter(value.getEClassifiers(), EClass.class)) {
viewpointIndex.put(cur.getName(), cur);
viewpointIndex.put(value.getName() + EcoreIntrinsicExtender.SEPARATOR + cur.getName(), cur);
viewpointIndex.put(value.getName() + EcoreIntrinsicExtender.AQL_SEPARATOR + cur.getName(), cur);
for (EClassifier eClassifier : value.getEClassifiers()) {
if (eClassifier instanceof EClass cur) {
addToIndex(cur.getName(), cur);
addToIndex(value.getName() + EcoreIntrinsicExtender.SEPARATOR + cur.getName(), cur);
addToIndex(value.getName() + EcoreIntrinsicExtender.AQL_SEPARATOR + cur.getName(), cur);
}
}
}

private void addToIndex(String key, EClass eClass) {
viewpointIndex.putIfAbsent(key, new ArrayList<>());
viewpointIndex.get(key).add(eClass);
}

@Override
public boolean isExtension(final EObject next) {
return false;
}

private Iterator<EClass> getEClassesFromName(final String name) {
return Iterators.concat(viewpointIndex.get(name).iterator(), platformIndex.getEClassesFromName(name).iterator());
return Stream.concat(viewpointIndex.getOrDefault(name, List.of()).stream(), platformIndex.getEClassesFromName(name).stream()).iterator();
}

private EClass findFirstEClassFromName(final String name) {
Expand All @@ -400,7 +405,7 @@ private EClass findFirstEClassFromName(final String name) {
}
return null;
}

/**
* Create a new instance.
*
Expand Down Expand Up @@ -439,21 +444,22 @@ public boolean preventFromBrowsing(final EObject root) {

@Override
public void updateMetamodels(final Collection<? extends MetamodelDescriptor> metamodelDescriptors) {
final Collection<? extends MetamodelDescriptor> metamodelDescriptorsCopy = Sets.newLinkedHashSet(metamodelDescriptors);
final Collection<? extends MetamodelDescriptor> metamodelDescriptorsCopy = new LinkedHashSet<>(metamodelDescriptors);
if (lastDescriptors != null) {
metamodelDescriptorsCopy.removeAll(lastDescriptors);
}
final Iterator<? extends MetamodelDescriptor> it = metamodelDescriptorsCopy.iterator();
while (it.hasNext()) {
final Object obj = it.next();
if (obj instanceof EcoreMetamodelDescriptor) {
final EPackage pak = ((EcoreMetamodelDescriptor) obj).resolve();
if (obj instanceof EcoreMetamodelDescriptor descriptor) {
final EPackage pak = descriptor.resolve();
if (pak != null) {
addTypesToSiriusIndex(pak);
Iterator<EPackage> subPack = Iterators.filter(pak.eAllContents(), EPackage.class);
while (subPack.hasNext()) {
addTypesToSiriusIndex(subPack.next());
}
pak.eAllContents().forEachRemaining(eObject -> {
if (eObject instanceof EPackage subPack) {
addTypesToSiriusIndex(subPack);
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2010 THALES GLOBAL SERVICES.
* Copyright (c) 2009, 2024 THALES GLOBAL SERVICES and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -14,15 +14,15 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;

import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;

/**
* Index for all EClasses defined in a package registry.
*
Expand All @@ -37,7 +37,7 @@ public class PackageRegistryIndex {

private final Predicate<EPackage> packageFilter;

private final Multimap<String, EClass> index = HashMultimap.create();
private final Map<String, List<EClass>> index = new HashMap<>();

private boolean initializedDescriptors;

Expand Down Expand Up @@ -99,22 +99,30 @@ private void indexTypesFromRegistry(boolean descriptors) {
* @return all the indexed classes with a matching name.
*/
public Collection<EClass> getEClassesFromName(String name) {
Collection<EClass> result = index.get(name);
Collection<EClass> result = index.getOrDefault(name, List.of());
if (Boolean.valueOf(System.getProperty("org.eclipse.sirius.enableUnsafeOptimisations", "false")) && result.isEmpty() && !initializedDescriptors) { //$NON-NLS-1$ //$NON-NLS-2$
indexTypesFromRegistry(true);
initializedDescriptors = true;
result = index.get(name);
result = index.getOrDefault(name, List.of());
}
return result;
}

private void indexTypesFrom(EPackage value) {
if (packageFilter.apply(value)) {
for (EClass cur : Iterables.filter(value.getEClassifiers(), EClass.class)) {
index.put(cur.getName(), cur);
index.put(value.getName() + SEPARATOR + cur.getName(), cur);
index.put(value.getName() + AQL_SEPARATOR + cur.getName(), cur);
if (packageFilter.test(value)) {
for (EClassifier classifier : value.getEClassifiers()) {
if (classifier instanceof EClass cur) {
addToIndex(cur.getName(), cur);
addToIndex(value.getName() + SEPARATOR + cur.getName(), cur);
addToIndex(value.getName() + AQL_SEPARATOR + cur.getName(), cur);
}
}
}
}

private void addToIndex(String key, EClass eClass) {
index.putIfAbsent(key, new ArrayList<>());
index.get(key).add(eClass);
}

}
Loading

0 comments on commit e874a1a

Please sign in to comment.