Search in sources :

Example 6 with TypeName

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"));
}
Also used : TypeName(org.opengis.util.TypeName) Test(org.junit.Test)

Example 7 with TypeName

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));
}
Also used : TypeName(org.opengis.util.TypeName)

Example 8 with TypeName

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;
}
Also used : SimpleAttributeType(org.apache.sis.internal.simple.SimpleAttributeType) Type(org.opengis.util.Type) RecordType(org.opengis.util.RecordType) TypeName(org.opengis.util.TypeName)

Example 9 with TypeName

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;
}
Also used : TypeName(org.opengis.util.TypeName) Iterator(java.util.Iterator) NameSpace(org.opengis.util.NameSpace) InternationalString(org.opengis.util.InternationalString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

TypeName (org.opengis.util.TypeName)9 Test (org.junit.Test)3 MemberName (org.opengis.util.MemberName)3 Map (java.util.Map)2 SimpleAttributeType (org.apache.sis.internal.simple.SimpleAttributeType)2 InternationalString (org.opengis.util.InternationalString)2 NameSpace (org.opengis.util.NameSpace)2 RecordType (org.opengis.util.RecordType)2 Type (org.opengis.util.Type)2 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)1 WeakValueHashMap (org.apache.sis.util.collection.WeakValueHashMap)1 DefaultLocalName (org.apache.sis.util.iso.DefaultLocalName)1 DefaultMemberName (org.apache.sis.util.iso.DefaultMemberName)1 DefaultTypeName (org.apache.sis.util.iso.DefaultTypeName)1 GenericName (org.opengis.util.GenericName)1 LocalName (org.opengis.util.LocalName)1