Search in sources :

Example 1 with NumberType

use of org.mule.metadata.api.model.NumberType in project mule by mulesoft.

the class SchemaTypeConversion method convertType.

public static QName convertType(final MetadataType type, ExpressionSupport expressionSupport) {
    final boolean dynamic = acceptsExpressions(expressionSupport);
    final Reference<QName> qName = new Reference<>(null);
    type.accept(new MetadataTypeVisitor() {

        @Override
        public void visitBoolean(BooleanType booleanType) {
            qName.set(dynamic ? EXPRESSION_BOOLEAN : SUBSTITUTABLE_BOOLEAN);
        }

        @Override
        public void visitNumber(NumberType numberType) {
            if (getId(numberType).isPresent()) {
                Class<Number> type = JavaTypeUtils.getType(numberType);
                if (anyOf(type, Integer.class, int.class)) {
                    qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
                } else if (anyOf(type, Double.class, double.class)) {
                    qName.set(dynamic ? EXPRESSION_DOUBLE : SUBSTITUTABLE_DECIMAL);
                } else if (anyOf(type, Long.class, long.class)) {
                    qName.set(dynamic ? EXPRESSION_LONG : SUBSTITUTABLE_LONG);
                } else {
                    qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
                }
            } else {
                if (numberType.getAnnotation(IntAnnotation.class).isPresent()) {
                    qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
                } else {
                    qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
                }
            }
        }

        @Override
        public void visitString(StringType stringType) {
            qName.set(dynamic ? EXPRESSION_STRING : STRING);
        }

        @Override
        public void visitDateTime(DateTimeType dateTimeType) {
            onDate();
        }

        @Override
        public void visitDate(DateType dateType) {
            onDate();
        }

        @Override
        public void visitArrayType(ArrayType arrayType) {
            qName.set(dynamic ? EXPRESSION_LIST : SUBSTITUTABLE_NAME);
        }

        @Override
        public void visitObject(ObjectType objectType) {
            if (isMap(objectType)) {
                qName.set(dynamic ? EXPRESSION_MAP : SUBSTITUTABLE_NAME);
            } else {
                defaultVisit(objectType);
            }
        }

        @Override
        protected void defaultVisit(MetadataType metadataType) {
            qName.set(STRING);
        }

        private void onDate() {
            qName.set(dynamic ? EXPRESSION_DATE_TIME : SUBSTITUTABLE_DATE_TIME);
        }

        private boolean anyOf(Class<Number> type, Class<?>... targets) {
            for (Class<?> target : targets) {
                if (type.equals(target)) {
                    return true;
                }
            }
            return false;
        }
    });
    return qName.get();
}
Also used : StringType(org.mule.metadata.api.model.StringType) QName(javax.xml.namespace.QName) Reference(org.mule.runtime.api.util.Reference) BooleanType(org.mule.metadata.api.model.BooleanType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) NumberType(org.mule.metadata.api.model.NumberType) DateTimeType(org.mule.metadata.api.model.DateTimeType) DateType(org.mule.metadata.api.model.DateType)

Aggregations

QName (javax.xml.namespace.QName)1 ArrayType (org.mule.metadata.api.model.ArrayType)1 BooleanType (org.mule.metadata.api.model.BooleanType)1 DateTimeType (org.mule.metadata.api.model.DateTimeType)1 DateType (org.mule.metadata.api.model.DateType)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 NumberType (org.mule.metadata.api.model.NumberType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1 StringType (org.mule.metadata.api.model.StringType)1 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)1 Reference (org.mule.runtime.api.util.Reference)1