1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   |  
    private static Object parseDpbString(String name, Object value) {
 
        // for the sake of unification we allow passing boolean, byte and integer
        // types too, we loose some cycles here, but that is called relatively
        // rarely, a tradeoff between code maintainability and CPU cycles.
        if (value instanceof Boolean)
            return value;
        else
        if (value instanceof Byte)
            return value;
        else
        if (value instanceof Integer)
            return value;
        Integer type;
        // if passed value is not string, throw an exception
        if (value != null && !(value instanceof String))
            throw new ClassCastException(value.getClass().getName());
        else
            if( value != null)
               type = TYPE_STRING;
            else
               type = (Integer)dpbParameterTypes.get(name);
 
        if( type == null)
            type = new Integer(TYPE_UNKNOWN);
 
        switch(type.intValue()) {
            case TYPE_BOOLEAN :
                return "".equals(value) ? Boolean.TRUE : new Boolean((String)value);
 
            case TYPE_BYTE :
                return new Byte((String)value);
 
            case TYPE_INT :
                return new Integer((String)value);
 
            case TYPE_STRING :
                return value;
 
            case TYPE_UNKNOWN :
            default :
 
                // set the value of the DPB by probing to convert string
                // into int or byte value, this method gives very good result
                // for guessing the method to call from the actual value;
                // null values are assumed to be booleans.
 
                if (value == null)
                    return Boolean.TRUE;
 
                try {
 
                    // try to deal with a value as a byte
                    int intValue = Integer.parseInt((String)value);
 
                    if (intValue < 256)
                        return new Byte((byte) intValue);
                    else
                        return new Integer(intValue);
 
                } catch (NumberFormatException nfex) {
 
                    // ok, that's not a byte, then set it as string
                    if ("".equals(value))
                        return Boolean.TRUE;
                    else
                        return value;
                }
        }
    } | 
Partager