Search in sources :

Example 1 with RecordType

use of org.opengis.util.RecordType in project sis by apache.

the class DefaultRecordSchema method createRecordType.

/**
 * Creates a new record type of the given name, which will contains the given members.
 * Members are declared in iteration order.
 *
 * @param  typeName  the record type name.
 * @param  members   the name of each record member, together with the expected value types.
 * @return a record type of the given name and members.
 * @throws IllegalArgumentException if a record already exists for the given name but with different members.
 */
public RecordType createRecordType(final CharSequence typeName, final Map<CharSequence, Class<?>> members) throws IllegalArgumentException {
    ArgumentChecks.ensureNonNull("typeName", typeName);
    ArgumentChecks.ensureNonNull("members", members);
    final TypeName name = nameFactory.createTypeName(namespace, typeName);
    final Map<CharSequence, Type> memberTypes = ObjectConverters.derivedValues(members, CharSequence.class, toTypes);
    RecordType record;
    synchronized (description) {
        record = description.get(typeName);
        if (record == null) {
            record = new DefaultRecordType(name, this, memberTypes, nameFactory);
            description.put(name, record);
            return record;
        }
    }
    /*
         * If a record type already exists for the given name, verify that it contains the same members.
         */
    final Iterator<Map.Entry<CharSequence, Class<?>>> it1 = members.entrySet().iterator();
    final Iterator<Map.Entry<MemberName, Type>> it2 = record.getMemberTypes().entrySet().iterator();
    boolean hasNext;
    while ((hasNext = it1.hasNext()) == it2.hasNext()) {
        if (!hasNext) {
            // Finished comparison successfully.
            return record;
        }
        final Map.Entry<CharSequence, Class<?>> e1 = it1.next();
        final Map.Entry<MemberName, Type> e2 = it2.next();
        if (!e2.getKey().tip().toString().equals(e1.toString())) {
            // Member names differ.
            break;
        }
        if (!((SimpleAttributeType) e2.getValue()).getValueClass().equals(e1.getValue())) {
            // Value classes differ.
            break;
        }
    }
    throw new IllegalArgumentException(Errors.format(Errors.Keys.RecordAlreadyDefined_2, getSchemaName(), typeName));
}
Also used : TypeName(org.opengis.util.TypeName) SimpleAttributeType(org.apache.sis.internal.simple.SimpleAttributeType) SimpleAttributeType(org.apache.sis.internal.simple.SimpleAttributeType) Type(org.opengis.util.Type) RecordType(org.opengis.util.RecordType) RecordType(org.opengis.util.RecordType) MemberName(org.opengis.util.MemberName) WeakValueHashMap(org.apache.sis.util.collection.WeakValueHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 2 with RecordType

use of org.opengis.util.RecordType in project sis by apache.

the class DefaultRecordSchemaTest method testCreateRecordType.

/**
 * Tests {@link DefaultRecordSchema#createRecordType(CharSequence, Map)}.
 */
@Test
public void testCreateRecordType() {
    final DefaultRecordSchema schema = new DefaultRecordSchema(null, null, "MySchema");
    final Map<CharSequence, Class<?>> members = new LinkedHashMap<>(8);
    assertNull(members.put("city", String.class));
    assertNull(members.put("latitude", Double.class));
    assertNull(members.put("longitude", Double.class));
    assertNull(members.put("population", Integer.class));
    final RecordType recordType = schema.createRecordType("MyRecordType", members);
    /*
         * Inspect properties.
         */
    assertSame("container", schema, recordType.getContainer());
    assertEquals("typeName", Names.createTypeName("MySchema", ":", "MyRecordType"), recordType.getTypeName());
    int count = 0;
    for (final Map.Entry<MemberName, Type> entry : recordType.getMemberTypes().entrySet()) {
        final String expectedName;
        final String expectedType;
        final Class<?> expectedClass;
        switch(count) {
            case 0:
                {
                    expectedName = "city";
                    expectedType = "OGC:CharacterString";
                    expectedClass = String.class;
                    break;
                }
            case 1:
                {
                    expectedName = "latitude";
                    expectedType = "OGC:Real";
                    expectedClass = Double.class;
                    break;
                }
            case 2:
                {
                    expectedName = "longitude";
                    expectedType = "OGC:Real";
                    expectedClass = Double.class;
                    break;
                }
            case 3:
                {
                    expectedName = "population";
                    expectedType = "OGC:Integer";
                    expectedClass = Integer.class;
                    break;
                }
            default:
                {
                    throw new AssertionError(count);
                }
        }
        final Type type = entry.getValue();
        assertEquals(expectedName, entry.getKey().toString());
        assertEquals(expectedType, type.getTypeName().toFullyQualifiedName().toString());
        assertEquals(expectedClass, ((SimpleAttributeType) type).getValueClass());
        count++;
    }
    /*
         * The DefaultRecordType(TypeName, RecordSchema, Map) constructor performs many argument checks, so
         * we use that constructor as a way to perform a final validation, especially regarding namespaces.
         */
    final DefaultRecordType copy = new DefaultRecordType(recordType.getTypeName(), recordType.getContainer(), recordType.getMemberTypes());
    assertEquals(recordType, copy);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) SimpleAttributeType(org.apache.sis.internal.simple.SimpleAttributeType) RecordType(org.opengis.util.RecordType) Type(org.opengis.util.Type) RecordType(org.opengis.util.RecordType) MemberName(org.opengis.util.MemberName) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Map (java.util.Map)2 SimpleAttributeType (org.apache.sis.internal.simple.SimpleAttributeType)2 MemberName (org.opengis.util.MemberName)2 RecordType (org.opengis.util.RecordType)2 Type (org.opengis.util.Type)2 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 WeakValueHashMap (org.apache.sis.util.collection.WeakValueHashMap)1 Test (org.junit.Test)1 TypeName (org.opengis.util.TypeName)1