Java SQL Type marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics)

Here you can find the source of marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics)

Description

marshal Curl Type

License

Open Source License

Declaration

public static String marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    public static String marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics) {
        String v = javaType;//from  w  w  w.j  ava  2  s  .  c  om
        if (equalsOneOf(v, null, "null", "void", "V"))
            return "void";
        else if (equalsOneOf(v, "java.lang.Object", "Object"))
            return "any";
        else if (equalsOneOf(v, "java.lang.Character", "Character", "char", "C"))
            return "char";
        else if (equalsOneOf(v, "java.lang.Boolean", "Boolean", "boolean", "Z"))
            return "bool";
        else if (equalsOneOf(v, "java.lang.Byte", "Byte", "byte", "B"))
            return "int8";
        else if (equalsOneOf(v, "java.lang.Short", "Short", "short", "S"))
            return "int16";
        else if (equalsOneOf(v, "java.lang.Integer", "Integer", "int", "I"))
            return "int";
        else if (equalsOneOf(v, "java.lang.Long", "Long", "long", "J"))
            return "int64";
        else if (equalsOneOf(v, "java.lang.Double", "Double", "double", "D"))
            return "double";
        else if (equalsOneOf(v, "java.lang.Float", "Float", "float", "F"))
            return "float";

        String result;
        if (v.endsWith("[]"))
            result = "{FastArray-of "
                    + marshalCurlType(v.substring(0, v.length() - 2), isAllowNull, generateGenerics) + "}";
        else if (v.startsWith("[L"))
            result = "{FastArray-of "
                    + marshalCurlType(v.substring(2, v.length() - 1), isAllowNull, generateGenerics) + "}";
        else if (v.startsWith("["))
            result = "{FastArray-of " + marshalCurlType(v.substring(1), isAllowNull, generateGenerics) + "}";
        else if (equalsOneOf(v, "java.lang.String", "String"))
            result = v;
        else if (equalsOneOf(v, "java.util.Date", "Date"))
            result = v;
        else if (equalsOneOf(v, "java.sql.Date"))
            result = "COM.CURLAP.ORB.TYPE.CDate";
        else if (equalsOneOf(v, "Time"))
            result = "COM.CURLAP.ORB.TYPE.CTime";
        else if (equalsOneOf(v, "Timestamp"))
            result = "COM.CURLAP.ORB.TYPE.CTimestamp";
        else if (equalsOneOf(v, "CLOB"))
            result = "COM.CURLAP.ORB.TYPE.CLOB";
        else if (equalsOneOf(v, "BLOB"))
            result = "COM.CURLAP.ORB.TYPE.BLOB";
        else if (equalsOneOf(v, "java.math.BigInteger", "BigInteger"))
            result = "COM.CURLAP.ORB.TYPE.BigInteger";
        else if (equalsOneOf(v, "java.math.BigDecimal", "BigDecimal"))
            result = "COM.CURLAP.ORB.TYPE.BigDecimal";
        else if (equalsOneOf(v, "java.util.List", "java.util.ArrayList", "List", "ArrayList"))
            result = "Array";
        else if (startsWithOneOf(v, "java.util.List", "java.util.ArrayList", "List", "ArrayList")) {
            // generics
            if (generateGenerics) {
                String generics = v.substring(v.indexOf('<') + 1, v.lastIndexOf('>'));
                result = "{Array-of " + marshalCurlType(generics, isAllowNull, generateGenerics) + "}";
            } else
                result = "Array";
        } else if (equalsOneOf(v, "java.util.Map", "java.util.HashMap", "java.util.Hashtable", "Map", "HashMap",
                "Hashtable"))
            result = "HashTable";
        else if (startsWithOneOf(v, "java.util.Map", "java.util.HashMap", "java.util.Hashtable", "Map", "HashMap",
                "Hashtable")) {
            // generics
            if (generateGenerics) {
                String generics = v.substring(v.indexOf('<') + 1, v.lastIndexOf('>'));
                int comma = indexOfGenericsComma(generics);
                String key = marshalCurlType(generics.substring(0, comma).trim(), isAllowNull, generateGenerics);
                String val = marshalCurlType(generics.substring(comma + 1).trim(), isAllowNull, generateGenerics);
                result = "{HashTable-of " + key + ", " + val + "}";
            } else
                result = "HashTable";
        } else if (equalsOneOf(v, "java.util.Set", "java.util.HashSet", "Set", "HashSet"))
            result = "Set";
        else if (startsWithOneOf(v, "java.util.Set", "java.util.HashSet", "Set", "HashSet")) {
            // generics
            if (generateGenerics) {
                String generics = v.substring(v.indexOf('<') + 1, v.lastIndexOf('>'));
                result = "{Set-of " + marshalCurlType(generics, isAllowNull, generateGenerics) + "}";
            } else
                result = "Set";
        } else
            result = v;
        return (isAllowNull ? "#" : "") + result;
    }

    public static String marshalCurlType(String javaType) {
        return marshalCurlType(javaType, true, false);
    }

    private static boolean equalsOneOf(String str, String... strs) {
        for (String t : strs)
            if (str.equals(t))
                return true;
        return false;
    }

    private static boolean startsWithOneOf(String str, String... strs) {
        for (String t : strs)
            if (str.startsWith(t))
                return true;
        return false;
    }

    private static int indexOfGenericsComma(String generics) {
        char comma = ',';
        int index = generics.indexOf(comma);
        if (index != -1 && index != generics.lastIndexOf(comma)) {
            boolean skip = false;
            for (int i = 0; i < generics.length(); i++) {
                char c = generics.charAt(i);
                if (c == '<')
                    skip = true;
                else if (c == '>')
                    skip = false;
                else if (!skip && c == comma)
                    return i;
            }
        }
        return index;
    }
}

Related

  1. loadAllTypesData(Connection conn)
  2. loadTestDataForEveryType(Connection conn)
  3. mapECLtype2SQLtype(String ecltype)
  4. mapType(Class javaType)
  5. mapXSDTypeName2SQLtype(String xsdtype)
  6. matchMethods(String name, Method[] methods, Class[] spt, int mrs, boolean exact)
  7. nativeSQL(String sql, boolean noBackslashEscapes)
  8. newArrayOfType(int typeCode, int size)
  9. newInstance(Class clazz, Object... params)