use of org.opengis.util.TypeName in project sis by apache.
the class NamesTest method testClassFromClassname.
/**
* Tests {@link Names#toClass(TypeName)} with a name in the {@code "class"} scope.
* If the name is not recognized, then {@code toClass(TypeName)} is expected to throw an exception.
*/
@Test
public void testClassFromClassname() {
final DefaultNameFactory factory = DefaultFactories.forBuildin(NameFactory.class, DefaultNameFactory.class);
final TypeName type = factory.toTypeName(Random.class);
assertEquals("class:java.util.Random", type.toFullyQualifiedName().toString());
assertValueClassEquals(Random.class, type);
assertValueClassEquals(DefaultNameFactoryTest.class, new DefaultTypeName(type.scope(), DefaultNameFactoryTest.class.getName()));
assertValueClassEquals(UnknownNameException.class, new DefaultTypeName(type.scope(), "org.apache.sis.Dummy"));
}
use of org.opengis.util.TypeName in project sis by apache.
the class TypeNamesTest method verifyLookup.
/**
* Verifies that the call to {@link TypeNames#toTypeName(NameFactory, Class)} returns a {@code TypeName} having the
* given name and namespace, then tests the reverse operation with {@link TypeNames#toClass(String, String)}.
*/
private static void verifyLookup(final String namespace, final String name, final Class<?> valueClass) throws ClassNotFoundException {
final DefaultNameFactory factory = DefaultFactories.forBuildin(NameFactory.class, DefaultNameFactory.class);
final TypeName type = factory.toTypeName(valueClass);
assertNotNull(name, type);
assertSame(name, valueClass, ((DefaultTypeName) type).toClass());
assertEquals(name, namespace, type.scope().name().toString());
assertEquals(name, name, type.toString());
assertEquals(name, valueClass, TypeNames.toClass(namespace, name));
}
use of org.opengis.util.TypeName in project sis by apache.
the class DefaultRecordSchema method toAttributeType.
/**
* Suggests an attribute type for the given value class. The {@code TypeName} will use the UML identifier
* of OGC/ISO specification when possible, e.g. {@code "GCO:CharacterString"} for {@code java.lang.String}.
* See <cite>Mapping Java classes to type names</cite> in {@link DefaultTypeName} javadoc for more information.
*
* @param valueClass the value class to represent as an attribute type.
* @return attribute type for the given value class.
*/
final Type toAttributeType(final Class<?> valueClass) {
if (!TypeNames.isValid(valueClass)) {
return null;
}
Type type = attributeTypes.get(valueClass);
if (type == null) {
if (valueClass == Void.TYPE) {
throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentValue_2, "valueClass", "void"));
}
final TypeName name = nameFactory.toTypeName(valueClass);
type = new SimpleAttributeType<>(name, valueClass);
final Type old = attributeTypes.putIfAbsent(valueClass, type);
if (old != null) {
// May happen if the type has been computed concurrently.
return old;
}
}
return type;
}
use of org.opengis.util.TypeName in project sis by apache.
the class TypeNames method toTypeName.
/**
* Infers the type name from the given class.
*
* @param factory the same factory than the one given to the constructor.
* @param valueClass the value class for which to get a type name.
* @return a type name for the given class (never {@code null}).
*/
final TypeName toTypeName(final NameFactory factory, final Class<?> valueClass) {
String name;
NameSpace ns = ogcNS;
if (CharSequence.class.isAssignableFrom(valueClass)) {
name = InternationalString.class.isAssignableFrom(valueClass) ? "FreeText" : "CharacterString";
} else if (Number.class.isAssignableFrom(valueClass)) {
name = Numbers.isInteger(valueClass) ? "Integer" : "Real";
} else {
/*
* Iterate over the special cases, excluding the numbers and character sequences
* since they were verified in the above statements.
*/
final Iterator<Map.Entry<String, Class<?>>> it = MAPPING.entrySet().iterator();
Class<?> base;
do {
final Map.Entry<String, Class<?>> entry = it.next();
base = entry.getValue();
if (base.isAssignableFrom(valueClass)) {
name = entry.getKey();
return factory.createTypeName(ns, name);
}
} while (// See MAPPING javadoc for the role of Boolean as a sentinel value.
base != Boolean.class);
/*
* Found no special case. Checks for the UML annotation, to be also formatted in the "OGC:" namespace.
* If no UML identifier is found, then we will format the Java class in the "class:" namespace. We use
* Class.getName() - not Class.getCanonicalName() - because we want a name readable by Class.forName(…).
*/
name = Types.getStandardName(valueClass);
if (name == null) {
ns = classNS;
// See above comment.
name = valueClass.getName();
}
}
/*
* Now create the name and remember the 'valueClass' for that name if the implementation allows that.
*/
final TypeName t = factory.createTypeName(ns, name);
if (t instanceof DefaultTypeName) {
((DefaultTypeName) t).setValueClass(ns, name, valueClass);
}
return t;
}
Aggregations