package org.simantics.modeling.services;
import java.nio.CharBuffer;
import java.util.Comparator;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.exception.DatabaseException;
/**
* An abstract base class that contains some helpers for implementing your own
* {@link ComponentNamingStrategy}. A default implementation is also provided
* for {@link #findFreshInstanceName(ReadGraph, Resource, Resource, Resource)}
* based on the Layer0 ontology HasGeneratedNamePrefix property.
*
* @author Tuukka Lehtonen
*/
public abstract class ComponentNamingStrategyBase implements ComponentNamingStrategy {
protected static final boolean DEBUG_ALL = false;
protected static final boolean DEBUG_NAME_MATCHING = false | DEBUG_ALL;
protected final String generatedNameFormat;
protected final boolean caseInsensitive;
/**
* Base constructor for a naming strategy with the specified format as the
* generated name format. The format will receive two arguments:
*
*
proposed name as {@link String}
*
an {@link Integer} that attempts to make the name unique within a
* context
*
* The simplest format specification utilizing both values is
* "%s %d", producing "FOO 1". Another example of a useful name
* format is "%s%04d", producing "FOO0001". Reordering the
* arguments is also possible, e.g. "%2$03d %1$s", producing
* "001 FOO".
*
*
* See {@link Formatter} for the format specification and how to customize
* it.
*
*
* This constructor will create a case-insensitive naming strategy.
*
* @param generatedNameFormat the format to use for generated names
*/
public ComponentNamingStrategyBase(String generatedNameFormat) {
this(generatedNameFormat, true);
}
/**
* Base constructor for a naming strategy with the specified format as the
* generated name format. The format will receive two arguments:
*
*
proposed name as {@link String}
*
an {@link Integer} that attempts to make the name unique within a
* context
*
* The simplest format specification utilizing both values is
* "%s %d", producing "FOO 1". Another example of a useful name
* format is "%s%04d", producing "FOO0001". Reordering the
* arguments is also possible, e.g. "%2$03d %1$s", producing
* "001 FOO".
*
*
* See {@link Formatter} for the format specification and how to customize
* it.
*
* @param generatedNameFormat the format to use for generated names
* @param caseInsensitive controls whether the strategy shall be case-insensitive or not
*/
public ComponentNamingStrategyBase(String generatedNameFormat, boolean caseInsensitive) {
this.generatedNameFormat = generatedNameFormat;
this.caseInsensitive = caseInsensitive;
}
@Override
public String findFreshInstanceName(ReadGraph graph, Resource configurationRoot, Resource container,
Resource componentType) throws NamingException, DatabaseException {
String proposition = ComponentNamingUtil.generateProposition(graph, container, componentType);
return validateInstanceName(graph, configurationRoot, container, componentType, proposition);
}
@Override
public String validateInstanceName(ReadGraph graph, Resource configurationRoot, Resource container,
Resource componentType, String proposition) throws NamingException, DatabaseException {
return validateInstanceName(graph, configurationRoot, container, componentType, proposition, false);
}
@Override
public List validateInstanceNames(
ReadGraph graph,
Resource configurationRoot,
List propositions,
boolean acceptProposition,
Set externallyReserved)
throws NamingException, DatabaseException
{
throw new UnsupportedOperationException();
}
protected Comparator