// This software is released into the Public Domain. See copying.txt for details.
package org.openstreetmap.osmosis.core.sort.v0_6;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.openstreetmap.osmosis.core.container.v0_6.EntityContainer;
import org.openstreetmap.osmosis.core.OsmosisRuntimeException;
import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration;
import org.openstreetmap.osmosis.core.pipeline.common.TaskManager;
import org.openstreetmap.osmosis.core.pipeline.common.TaskManagerFactory;
import org.openstreetmap.osmosis.core.pipeline.v0_6.SinkSourceManager;
/**
* The task manager factory for an entity sorter.
*
* @author Brett Henderson
*/
public class EntitySorterFactory extends TaskManagerFactory {
private static final String ARG_COMPARATOR_TYPE = "type";
private Map<String, Comparator<EntityContainer>> comparatorMap;
private String defaultComparatorType;
/**
* Creates a new instance.
*/
public EntitySorterFactory() {
comparatorMap = new HashMap<String, Comparator<EntityContainer>>();
}
/**
* Registers a new comparator.
*
* @param comparatorType
* The name of the comparator.
* @param comparator
* The comparator.
* @param setAsDefault
* If true, this will be set to be the default comparator if no
* comparator is specified.
*/
public void registerComparator(
String comparatorType, Comparator<EntityContainer> comparator, boolean setAsDefault) {
if (comparatorMap.containsKey(comparatorType)) {
throw new OsmosisRuntimeException("Comparator type \"" + comparatorType + "\" already exists.");
}
if (setAsDefault) {
defaultComparatorType = comparatorType;
}
comparatorMap.put(comparatorType, comparator);
}
/**
* Retrieves the comparator identified by the specified type.
*
* @param comparatorType
* The comparator to be retrieved.
* @return The comparator.
*/
private Comparator<EntityContainer> getComparator(String comparatorType) {
if (!comparatorMap.containsKey(comparatorType)) {
throw new OsmosisRuntimeException("Comparator type " + comparatorType
+ " doesn't exist.");
}
return comparatorMap.get(comparatorType);
}
/**
* {@inheritDoc}
*/
@Override
protected TaskManager createTaskManagerImpl(TaskConfiguration taskConfig) {
Comparator<EntityContainer> comparator;
// Get the comparator.
comparator = getComparator(
getStringArgument(
taskConfig,
ARG_COMPARATOR_TYPE,
getDefaultStringArgument(taskConfig, defaultComparatorType)
)
);
return new SinkSourceManager(
taskConfig.getId(),
new EntitySorter(comparator),
taskConfig.getPipeArgs()
);
}
}