Java如何从来自多个来源的数据推断类型 [英] Java how to infer type from data coming from multiple sources

查看:31
本文介绍了Java如何从来自多个来源的数据推断类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个从多个传感器收集数据的 Java 服务器.这些传感器通常将查询的值作为字符串值返回.在标头中,它指示了服务器必须使用的数据类型来转换接收到的值.这些值可以是整数、布尔值、双精度值、浮点数和长整数.

I'm developing a java server that collects data from multiple sensors. These sensors usually return the queried values as string values. In the header it's indicated the data type that the server has to use to cast the received value. These value can be integer, boolean, double, float, and long.

传感器可能不提供值的数据类型描述",因此:我想找到一种方法来理解它分析接收到的字符串的数据类型.

It might happen that the sensors don't provide the "data type description" of the values so: I want to find a way to understand the data type it analyzing the received string.

我正在考虑使用 REGEX,但也许还有其他一些方法可以做得更好.有什么建议吗?

I was thinking about using REGEX but maybe there are some other ways to do it better. Any suggestion?

推荐答案

有几种方法可以做到这一点.一种是尝试按正确的顺序按不同的标准 java 类型解析值,即

There is several approaches to do this. One is try to parse value by different standart java types in proper order i.e.

Boolean.parseBoolean(s)
Integer.parseInteger(s)
Long.parseLong(s)
... 
(and so on)

每一步都捕获异常第二种方法 - 使用 apache commons 库,检测类型,即

And catch exception every step Second approach - use apache commons library, there is detection of types i.e.

BooleanUtils.isBoolean(s)    
StringUtils.IsNumeric(s)
StringUtils.IsAlpha(s)

这篇关于Java如何从来自多个来源的数据推断类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆