All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bndtools.utils.DeltaAccumulator Maven / Gradle / Ivy

The newest version!
package bndtools.utils;

import java.io.File;
import java.io.FileFilter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;

public class DeltaAccumulator implements IResourceDeltaVisitor {

	private final int								acceptKinds;
	private final Collection				collector;
	private final Map			deltaKinds	= new HashMap<>();
	private final IFilter				filter;
	private final IResourceConverter	converter;

	public static final DeltaAccumulator fileAccumulator(int acceptKinds, Collection collector,
		final FileFilter filter) {
		return new DeltaAccumulator<>(acceptKinds, collector, file -> filter.accept(file),
			resource -> resource.getLocation()
				.toFile());
	}

	public static final DeltaAccumulator resourceAccumulator(int acceptKinds,
		Collection collector, IFilter filter) {
		return new DeltaAccumulator<>(acceptKinds, collector, filter, resource -> resource);
	}

	public DeltaAccumulator(int acceptKinds, Collection collector, IFilter filter,
		IResourceConverter converter) {
		this.acceptKinds = acceptKinds;
		this.collector = collector;
		this.filter = filter;
		this.converter = converter;
	}

	@Override
	public boolean visit(IResourceDelta delta) throws CoreException {
		if ((acceptKinds & delta.getKind()) == 0)
			return false;
		IResource resource = delta.getResource();
		if (resource.getType() == IResource.FILE) {
			T result = converter.convert(resource);
			if (filter == null || filter.select(result)) {
				collector.add(result);
				deltaKinds.put(result, delta.getKind());
			}
			return false;
		}
		// Must be a container type, so return true in order to recurse in.
		return true;
	}

	public int queryDeltaKind(T t) {
		Integer kind = deltaKinds.get(t);
		return kind != null ? kind.intValue() : IResourceDelta.NO_CHANGE;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy