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

org.nasdanika.persistence.DispatchingLoader Maven / Gradle / Ivy

package org.nasdanika.persistence;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;

import org.eclipse.emf.common.util.URI;
import org.nasdanika.common.ProgressMonitor;

/**
 * Registers loaders by prefixes and then delegates to the registered loaders removing prefix first.  
 * @author Pavel
 *
 */
public class DispatchingLoader implements ObjectLoader {
	
	private org.nasdanika.persistence.ObjectLoader chain;

	public DispatchingLoader(ObjectLoader chain) {
		this.chain = chain;
	}
	
	public DispatchingLoader() {}
	
	protected Map registry = new HashMap<>();
	
	/**
	 * Registers a loader with given prefix
	 * @param prefix
	 * @param loader
	 */
	public void register(String prefix, ObjectLoader loader) {
		registry.put(prefix, loader);
	}

	@Override
	public Object create(
			ObjectLoader loader, 
			String type, 
			Object config, 
			URI base, 
			BiConsumer> resolver,
			Collection markers,
			ProgressMonitor progressMonitor) {
		for (Entry re: registry.entrySet()) {
			if (type.startsWith(re.getKey())) {
				return re.getValue().create(
						loader, 
						type.substring(re.getKey().length()), 
						config, 
						base,
						resolver,
						markers,
						progressMonitor);
			}			
		}
		
		if (chain == null) {
			throw new ConfigurationException("Unsupported type: " + type, markers);
		}
		
		return chain.create(
				loader, 
				type, 
				config, 
				base, 
				resolver,
				markers,
				progressMonitor);
	}

	@Override
	public Object create(ObjectLoader loader, String type) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void load(
			ObjectLoader loader, 
			Object config, 
			Object target, 
			URI base,
			BiConsumer> resolver, 
			Collection markers,
			ProgressMonitor progressMonitor) {
		
		throw new UnsupportedOperationException();		
	}
		
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy