Example usage for org.springframework.util NumberUtils parseNumber

List of usage examples for org.springframework.util NumberUtils parseNumber

Introduction

In this page you can find the example usage for org.springframework.util NumberUtils parseNumber.

Prototype

@SuppressWarnings("unchecked")
public static <T extends Number> T parseNumber(String text, Class<T> targetClass) 

Source Link

Document

Parse the given text into a Number instance of the given target class, using the corresponding decode / valueOf method.

Usage

From source file:net.cpollet.jixture.fixtures.transformers.TestFileFixtureTransformer.java

@Before
public void setUp() throws NoSuchFieldException {
    GenericConversionService genericConversionService = new GenericConversionService();

    genericConversionService.addConverter(new Converter<String, Integer>() {
        @Override/*from  www.ja v  a 2 s .co  m*/
        public Integer convert(String source) {
            if (0 == source.length()) {
                return null;
            }
            return NumberUtils.parseNumber(source, Integer.class);
        }
    });

    Mockito.when(conversionServiceFactoryBean.getObject()).thenReturn(genericConversionService);

    mockCartEntry();
    mockProduct();
}

From source file:ch.ralscha.extdirectspring.controller.ApiController.java

private String buildApiString(String apiNs, String actionNs, String remotingApiVar, String pollingUrlsVar,
        String routerUrl, String basePollUrl, String group, boolean debug, boolean doc) {

    RemotingApi remotingApi = new RemotingApi(this.configurationService.getConfiguration().getProviderType(),
            routerUrl, actionNs);/*  w  w  w .  ja v  a 2s  . c o m*/

    remotingApi.setTimeout(this.configurationService.getConfiguration().getTimeout());
    remotingApi.setMaxRetries(this.configurationService.getConfiguration().getMaxRetries());

    Object enableBuffer = this.configurationService.getConfiguration().getEnableBuffer();
    if (enableBuffer instanceof String && StringUtils.hasText((String) enableBuffer)) {
        String enableBufferString = (String) enableBuffer;
        if (enableBufferString.equalsIgnoreCase("true")) {
            remotingApi.setEnableBuffer(Boolean.TRUE);
        } else if (enableBufferString.equalsIgnoreCase("false")) {
            remotingApi.setEnableBuffer(Boolean.FALSE);
        } else {
            Integer enableBufferMs = NumberUtils.parseNumber(enableBufferString, Integer.class);
            remotingApi.setEnableBuffer(enableBufferMs);
        }
    } else if (enableBuffer instanceof Number || enableBuffer instanceof Boolean) {
        remotingApi.setEnableBuffer(enableBuffer);
    }

    if (this.configurationService.getConfiguration().getBufferLimit() != null) {
        remotingApi.setBufferLimit(this.configurationService.getConfiguration().getBufferLimit());
    }

    buildRemotingApi(remotingApi, group);

    StringBuilder sb = new StringBuilder();

    if (StringUtils.hasText(apiNs)) {
        sb.append("Ext.ns('");
        sb.append(apiNs);
        sb.append("');");
    }

    if (debug) {
        sb.append("\n\n");
    }

    if (StringUtils.hasText(actionNs)) {
        sb.append("Ext.ns('");
        sb.append(actionNs);
        sb.append("');");

        if (debug) {
            sb.append("\n\n");
        }
    }

    String jsonConfig;
    if (!doc) {
        jsonConfig = writeValueAsString(remotingApi, debug);
    } else {
        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixIn(RemotingApi.class, RemotingApiMixin.class);
        try {
            jsonConfig = mapper.writer().withDefaultPrettyPrinter().writeValueAsString(remotingApi);
        } catch (JsonProcessingException e) {
            jsonConfig = null;
            LogFactory.getLog(ApiController.class).info("serialize object to json", e);
        }
    }

    if (StringUtils.hasText(apiNs)) {
        sb.append(apiNs).append(".");
    }
    sb.append(remotingApiVar).append(" = ");
    sb.append(jsonConfig);
    sb.append(";");

    List<PollingProvider> pollingProviders = remotingApi.getPollingProviders();
    if (!pollingProviders.isEmpty()) {

        if (debug) {
            sb.append("\n\n");
        }

        if (StringUtils.hasText(apiNs)) {
            sb.append(apiNs).append(".");
        }
        sb.append(pollingUrlsVar).append(" = {");
        if (debug) {
            sb.append("\n");
        }

        for (int i = 0; i < pollingProviders.size(); i++) {
            if (debug) {
                sb.append("  ");
            }

            sb.append("\"");
            sb.append(pollingProviders.get(i).getEvent());
            sb.append("\"");
            sb.append(" : \"").append(basePollUrl).append("/");
            sb.append(pollingProviders.get(i).getBeanName());
            sb.append("/");
            sb.append(pollingProviders.get(i).getMethod());
            sb.append("/");
            sb.append(pollingProviders.get(i).getEvent());
            sb.append("\"");
            if (i < pollingProviders.size() - 1) {
                sb.append(",");
                if (debug) {
                    sb.append("\n");
                }
            }
        }
        if (debug) {
            sb.append("\n");
        }
        sb.append("};");
    }

    return sb.toString();
}

From source file:org.springframework.cloud.aws.messaging.support.converter.NotificationRequestConverter.java

private static Object getNumberValue(String attributeType, String attributeValue) {
    String numberType = attributeType.substring(MessageAttributeDataTypes.NUMBER.length() + 1);
    try {//  ww  w  .  ja  v  a 2s  .  c o m
        Class<? extends Number> numberTypeClass = Class.forName(numberType).asSubclass(Number.class);
        return NumberUtils.parseNumber(attributeValue, numberTypeClass);
    } catch (ClassNotFoundException e) {
        throw new MessagingException(
                String.format(
                        "Message attribute with value '%s' and data type '%s' could not be converted "
                                + "into a Number because target class was not found.",
                        attributeValue, attributeType),
                e);
    }
}