All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.nasdanika.models.rules.NotifierInspector Maven / Gradle / Ivy
package org.nasdanika.models.rules;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.nasdanika.common.Context;
import org.nasdanika.common.ProgressMonitor;
import org.nasdanika.common.Util;
import org.nasdanika.ncore.Tree;
import org.nasdanika.ncore.TreeItem;
/**
* Binding to {@link Notifier}
*/
public interface NotifierInspector extends Inspector {
@Override
default boolean isForType(Class> targetType) {
return Notifier.class.isAssignableFrom(targetType);
}
/**
* Inspector which is aware of {@link ResourceSet}, {@link Resource}, and {@link EObject} contents as well as of {@link TreeItem} semantics - referencing another resource.
* @param parallel If true contents is inspected using parallel stream
* @param predicate contents predicate, can be null.
* @return
*/
default NotifierInspector asContentsInspector(boolean parallel, Predicate predicate) {
return new NotifierInspector() {
private URI resolve(EObject eObj) {
EObject eContainer = eObj.eContainer();
if (eContainer == null) {
Resource eResource = eObj.eResource();
return eResource == null ? null : eResource.getURI();
}
URI containerURI = resolve(eContainer);
if (containerURI == null) {
return null;
}
if (eObj instanceof TreeItem) {
if (Util.isBlank(containerURI.lastSegment())) {
containerURI = containerURI.trimSegments(1);
}
return containerURI.appendSegment(((TreeItem) eObj).getName());
}
return containerURI;
}
@Override
public void inspect(Notifier target, BiConsumer super Notifier, ? super InspectionResult> inspectionResultConsumer, Context context, ProgressMonitor progressMonitor) {
if (predicate == null || predicate.test(target)) {
if (NotifierInspector.this.isForType(target.getClass())) {
NotifierInspector.this.inspect(target, inspectionResultConsumer, context, progressMonitor);
}
if (target instanceof ResourceSet) {
Stream rStream = ((ResourceSet) target).getResources().stream();
if (parallel) {
rStream = rStream.parallel();
}
rStream.forEach(resource -> inspect(resource, inspectionResultConsumer, context, progressMonitor));
} else if (target instanceof Resource) {
Stream contentsStream = ((Resource) target).getContents().stream();
if (parallel) {
contentsStream = contentsStream.parallel();
}
contentsStream.forEach(root -> inspect(root, inspectionResultConsumer, context, progressMonitor));
} else if (target instanceof EObject) {
if (target instanceof TreeItem && ((TreeItem) target).eContainer() != null) {
URI targetURI = resolve((TreeItem) target);
if (targetURI != null) {
if (target instanceof Tree && !Util.isBlank(targetURI.lastSegment())) {
targetURI = targetURI.appendSegment("");
}
Resource eResource = ((TreeItem) target).eResource();
if (eResource != null) {
ResourceSet resourceSet = eResource.getResourceSet();
if (resourceSet != null) {
Resource targetResource = resourceSet.getResource(targetURI, true);
inspect(targetResource, inspectionResultConsumer, context, progressMonitor);
}
}
}
}
Stream contentsStream = ((EObject) target).eContents().stream();
if (parallel) {
contentsStream = contentsStream.parallel();
}
contentsStream.forEach(contents -> inspect(contents, inspectionResultConsumer, context, progressMonitor));
}
}
}
@Override
public boolean isForType(Class> targetType) {
return Notifier.class.isAssignableFrom(targetType);
}
};
}
static NotifierInspector adapt(Inspector super Notifier> inspector) {
return new NotifierInspector() {
@Override
public void inspect(
Notifier target,
BiConsumer super Notifier, ? super InspectionResult> inspectionResultConsumer,
Context context,
ProgressMonitor progressMonitor) {
inspector.inspect(target, (t,v) -> inspectionResultConsumer.accept((Notifier) t, v), context, progressMonitor);
}
@Override
public boolean isForType(Class> targetType) {
return inspector.isForType(targetType);
}
};
}
}