Java Code Examples for org.apache.commons.beanutils.converters.IntegerConverter

The following examples show how to use org.apache.commons.beanutils.converters.IntegerConverter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ServerCore   Source File: Matrix3IntConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int[][][] convert(String str) {
    if (str == null || str.isEmpty()) {
        return new int[0][][];
    }
    // 格式 1,2;3,4|5,6  =>  [[[1, 2], [3, 4]], [[5,6]]]
    IntegerConverter integerConverter = new IntegerConverter(); // 基本类型
    ArrayConverter arrayConverter1 = new ArrayConverter(int[].class, integerConverter); // 一维数组 默认使用逗号分割

    ArrayConverter arrayConverter2 = new ArrayConverter(int[][].class, arrayConverter1); // 二维数组
    arrayConverter2.setDelimiter(';'); // 使用分号分割
    arrayConverter2.setAllowedChars(new char[]{','});


    ArrayConverter arrayConverter3 = new ArrayConverter(int[][].class, arrayConverter2); // 二维数组
    arrayConverter3.setDelimiter('|'); // 使用竖线分割
    arrayConverter3.setAllowedChars(new char[]{';', ','});

    return arrayConverter3.convert(int[][][].class, str);
}
 
Example 2
Source Project: GameServer   Source File: Matrix3IntConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int[][][] convert(String str) {
    if (str == null || str.isEmpty()) {
        return new int[0][][];
    }
    // 格式 1,2;3,4|5,6  =>  [[[1, 2], [3, 4]], [[5,6]]]
    // 基本类型
    IntegerConverter integerConverter = new IntegerConverter();
    // 一维数组 默认使用逗号分割
    ArrayConverter arrayConverter1 = new ArrayConverter(int[].class, integerConverter);

    // 二维数组
    ArrayConverter arrayConverter2 = new ArrayConverter(int[][].class, arrayConverter1);
    // 使用分号分割
    arrayConverter2.setDelimiter(';');
    arrayConverter2.setAllowedChars(new char[]{','});

    // 二维数组
    ArrayConverter arrayConverter3 = new ArrayConverter(int[][].class, arrayConverter2);
    // 使用竖线分割
    arrayConverter3.setDelimiter('|');
    arrayConverter3.setAllowedChars(new char[]{';', ','});

    return arrayConverter3.convert(int[][][].class, str);
}
 
Example 3
Source Project: velocity-tools   Source File: ConfigTests.java    License: Apache License 2.0 6 votes vote down vote up
public @Test void testBadData()
{
    Data datum = new Data();
    // a fresh datum should be invalid
    assertInvalid(datum);
    // setting a key is not enough to be valid
    datum.setKey("test");
    assertInvalid(datum);

    // set type to number value to a non-number
    datum.setValue("true");
    datum.setType("number");
    assertInvalid(datum);

    // should fail to convert a decimal string to an integer
    datum.setValue("0.1");
    datum.convertWith(new IntegerConverter());
    assertInvalid(datum);
}
 
Example 4
Source Project: bluetooth-gatt-parser   Source File: FieldHolder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns an Integer representation of the field or a default value in case if the field cannot
 * be converted to an Integer.
 * @param def the default value to be returned if an error occurs converting the field
 * @return an Integer representation of the field
 */
public Integer getInteger(Integer def) {
    Integer result = new IntegerConverter(null).convert(Integer.class, prepareValue());
    if (result != null) {
        double multiplier = getMultiplier();
        double offset = getOffset();
        if (multiplier != 1.0 || offset != 0.0) {
            return (int) Math.round(result * multiplier + offset);
        } else {
            return result;
        }
    } else {
        return def;
    }
}
 
Example 5
Source Project: bluetooth-gatt-parser   Source File: FieldHolder.java    License: Apache License 2.0 5 votes vote down vote up
private AbstractConverter getConverter() {
    FieldFormat fieldFormat = field.getFormat();
    int size = fieldFormat.getSize();
    switch (fieldFormat.getType()) {
        case BOOLEAN: return new BooleanConverter();
        case UINT:
            if (size < 32) {
                return new IntegerConverter();
            } else if (size < 64) {
                return new LongConverter();
            } else {
                return new BigIntegerConverter();
            }
        case SINT:
            if (size <= 32) {
                return new IntegerConverter();
            } else if (size <= 64) {
                return new LongConverter();
            } else {
                return new BigIntegerConverter();
            }
        case FLOAT_IEE754:
        case FLOAT_IEE11073: return size <= 32 ? new FloatConverter() : new DoubleConverter();
        case UTF8S:
        case UTF16S: return new StringConverter();
        default:
            throw new IllegalStateException("Unsupported field format: " + fieldFormat.getType());
    }
}
 
Example 6
Source Project: feilong-core   Source File: ConvertUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Register standard default null.
 * 
 * @see ConvertUtilsBean#registerPrimitives(boolean) registerPrimitives(boolean throwException)
 * @see ConvertUtilsBean#registerStandard(boolean,boolean) registerStandard(boolean throwException, boolean defaultNull)
 * @see ConvertUtilsBean#registerOther(boolean) registerOther(boolean throwException)
 * @see ConvertUtilsBean#registerArrays(boolean,int) registerArrays(boolean throwException, int defaultArraySize)
 * @see ConvertUtilsBean#deregister(Class) ConvertUtilsBean.deregister(Class)
 * @since 1.11.2
 */
public static void registerStandardDefaultNull(){
    ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
    ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
    ConvertUtils.register(new BooleanConverter(null), Boolean.class);
    ConvertUtils.register(new ByteConverter(null), Byte.class);
    ConvertUtils.register(new CharacterConverter(null), Character.class);
    ConvertUtils.register(new DoubleConverter(null), Double.class);
    ConvertUtils.register(new FloatConverter(null), Float.class);
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    ConvertUtils.register(new LongConverter(null), Long.class);
    ConvertUtils.register(new ShortConverter(null), Short.class);
    ConvertUtils.register(new StringConverter(null), String.class);
}
 
Example 7
Source Project: rice   Source File: KualiActionServlet.java    License: Educational Community License v2.0 5 votes vote down vote up
/**
    * <p>Initialize other global characteristics of the controller servlet.</p>
    * Overridden to remove the ConvertUtils.deregister() command that caused problems
    * with the concurrent data dictionary load.  (KULRNE-4405)
    *
    * @exception ServletException if we cannot initialize these resources
    */
   @Override
protected void initOther() throws ServletException {

       String value = null;
       value = getServletConfig().getInitParameter("config");
       if (value != null) {
           config = value;
       }

       // Backwards compatibility for form beans of Java wrapper classes
       // Set to true for strict Struts 1.0 compatibility
       value = getServletConfig().getInitParameter("convertNull");
       if ("true".equalsIgnoreCase(value)
           || "yes".equalsIgnoreCase(value)
           || "on".equalsIgnoreCase(value)
           || "y".equalsIgnoreCase(value)
           || "1".equalsIgnoreCase(value)) {

           convertNull = true;
       }

       if (convertNull) {
           ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
           ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
           ConvertUtils.register(new BooleanConverter(null), Boolean.class);
           ConvertUtils.register(new ByteConverter(null), Byte.class);
           ConvertUtils.register(new CharacterConverter(null), Character.class);
           ConvertUtils.register(new DoubleConverter(null), Double.class);
           ConvertUtils.register(new FloatConverter(null), Float.class);
           ConvertUtils.register(new IntegerConverter(null), Integer.class);
           ConvertUtils.register(new LongConverter(null), Long.class);
           ConvertUtils.register(new ShortConverter(null), Short.class);
       }

       // KULRICE-8176: KFS Notes/Attachments Tab Functionality for Note Text Error - Visible/Special characters, spaces, or tabs
       parameterEncoding = getServletConfig().getInitParameter("PARAMETER_ENCODING");
   }
 
Example 8
Source Project: feilong-core   Source File: ConvertUtil.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 将 <code>toBeConvertedValue</code> 转换成 {@link Integer}类型,如果转换不了返回默认值 <code>defaultValue</code>.
 * 
 * <h3>示例:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * ConvertUtil.toInteger(null,null)               = null
 * ConvertUtil.toInteger(null,1)                  = 1
 * ConvertUtil.toInteger("aaaa",1)                = 1
 * ConvertUtil.toInteger(8L,1)                    = 8
 * ConvertUtil.toInteger("8",1)                   = 8
 * ConvertUtil.toInteger(new BigDecimal("8"),1)   = 8
 * </pre>
 * 
 * <p>
 * 如果传入的参数 <code>toBeConvertedValue</code> 是 <b>数组</b>,那么<b>取第一个元素</b>进行转换,参见 {@link AbstractConverter#convertArray(Object)} L227:
 * </p>
 * 
 * <pre class="code">
 * ConvertUtil.toInteger(new String[] { "1", "2", "3" }, 8) = 1
 * </pre>
 * 
 * <p>
 * 如果传入的参数 <code>toBeConvertedValue</code> 是 <b>集合</b>,那么<b>取第一个元素</b>进行转换,参见 {@link AbstractConverter#convertArray(Object)} Line234:
 * </p>
 * 
 * <pre class="code">
 * ConvertUtil.toInteger(toList("1", "2"), 8) = 1
 * </pre>
 * 
 * </blockquote>
 * 
 * <p>
 * 该方法非常适用 获取request请求的分页参数
 * </p>
 * 
 * <h3>示例:</h3>
 * 
 * <blockquote>
 * 
 * 原来的写法:
 * 
 * <pre class="code">
 * 
 * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){
 *     String pageNoString = RequestUtil.getParameter(request, pageParamName);
 *     try{
 *         int pageNo = Integer.parseInt(pageNoString);
 *         return pageNo;
 *     }catch (Exception e){
 *         LOGGER.error(e.getClass().getName(), e);
 *     }
 *     return 1; <span style="color:green">// 不带这个参数或者转换异常返回1</span>
 * }
 * 
 * </pre>
 * 
 * 现在可以更改成:
 * 
 * <pre class="code">
 * 
 * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){
 *     String pageNoString = RequestUtil.getParameter(request, pageParamName);
 *     return ConvertUtil.toInteger(pageNoString, 1);
 * }
 * </pre>
 * 
 * </blockquote>
 *
 * @param toBeConvertedValue
 *            值
 * @param defaultValue
 *            默认值
 * @return 如果 <code>toBeConvertedValue</code> 是null,返回 <code>defaultValue</code> <br>
 *         如果传入的参数 <code>toBeConvertedValue</code> 是 <b>数组</b>,那么<b>取第一个元素</b>进行转换<br>
 *         如果传入的参数 <code>toBeConvertedValue</code> 是 <b>集合</b>,那么<b>取第一个元素</b>进行转换<br>
 *         如果找不到转换器或者转换的时候出现了异常,返回 <code>defaultValue</code>
 * @see org.apache.commons.beanutils.converters.IntegerConverter
 * @see org.apache.commons.lang3.ObjectUtils#defaultIfNull(Object, Object)
 * @since 1.6.1
 */
public static Integer toInteger(Object toBeConvertedValue,Integer defaultValue){
    return new IntegerConverter(defaultValue).convert(Integer.class, toBeConvertedValue);
}