1

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

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

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

4

3 回答 3

2

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

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

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

BooleanUtils.isBoolean(s)    
StringUtils.IsNumeric(s)
StringUtils.IsAlpha(s)
于 2012-11-09T19:20:03.447 回答
1

我将创建一个数据验证器责任链,其每个元素都会尝试将输入数据从最严格的类型排序为最不严格的类型:

boolean
integer
long
float
double
String

如果无法解析数据,则链会将其传播到下一个解析器,如果一切都失败,则抛出异常或将其用作字符串。

于 2012-11-09T19:11:35.260 回答
0

我受到这篇文章的启发,写了自己的文章。它真的很容易使用。String#trim()用于删除前导和尾随空格,因此以下工作正常:

jshell> Typifier.typify(" 23  \t\n")
$206 ==> String[2] { "Byte", "23" }

jshell> Typifier.typify("\r\n 3.4")
$207 ==> String[2] { "Float", "3.4" }

但是如果用户输入空格,那也没关系:

jshell> Typifier.typify(" ")
$298 ==> String[2] { "String", " " }

真/假的各种表示用于确定布尔值:

jshell> Typifier.typify(" F ")
$208 ==> String[2] { "Boolean", "false" }

jshell> Typifier.typify(" 1 ")
$209 ==> String[2] { "Boolean", "true" }

jshell> Typifier.typify(" TRUE ")
$210 ==> String[2] { "Boolean", "true" }

Byte、Short 和 Float 的范围用于将值装箱为可用的最窄类型:

jshell> Typifier.typify(" 2 ")
$212 ==> String[2] { "Byte", "2" }

jshell> Typifier.typify(" 200 ")
$213 ==> String[2] { "Short", "200" }

jshell> Typifier.typify(" 2e9 ")
$214 ==> String[2] { "Float", "2.0E9" }

jshell> Typifier.typify(" 2e99 ")
$215 ==> String[2] { "Double", "2.0E99" }

默认类型是字符串,但如果方程可以被 JavaScript ScriptEngine解析,if 将被解析并返回结果

jshell> Typifier.typify("var a = 3; var b = 6; a*b")
$230 ==> String[2] { "Float", "18.0" }

jshell> Typifier.typify("2*(2.4e2 + 34.8)")
$231 ==> String[2] { "Float", "549.6" }

如果输入字符串的长度为 1 并且不是 Boolean 或 Byte,则将为其分配 Character 类型:

jshell> Typifier.typify("4")
$232 ==> String[2] { "Byte", "4" }

jshell> Typifier.typify("-")
$233 ==> String[2] { "Character", "-" }

jshell> Typifier.typify("a")
$234 ==> String[2] { "Character", "a" }

可能的扩展可能包括为公式评估放置一个标志,或者一个将返回类型限制为“常见”类型(布尔、整数、双精度、字符串)的标志。此代码也可以在gist.github找到。无论如何,这里是:

代码

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Typifier {

  public Typifier() {
    // nothing special to do here
  }

  public static String[] typify (String data) {

    String s = data.trim();

    // -1. if the input data is only whitespace, return String
    if (s.length() == 0) return new String[]{"String", data};

    // 0. check if the data is Boolean (true or false)
    if (Arrays.asList("0", "f", "F", "false", "False", "FALSE").contains(s))
      return new String[]{"Boolean", "false"};
    else if (Arrays.asList("1", "t", "T", "true",  "True",  "TRUE" ).contains(s))
      return new String[]{"Boolean", "true"};

    // 1. check if data is a Byte (1-byte integer with range [-(2e7) = -128, ((2e7)-1) = 127])
    try {
      Byte b = Byte.parseByte(s);
      return new String[]{"Byte", b.toString()}; // if we make it to this line, the data parsed fine as a Byte
    } catch (java.lang.NumberFormatException ex) {
      // okay, guess it's not a Byte
    }

    // 2. check if data is a Short (2-byte integer with range [-(2e15) = -32768, ((2e15)-1) = 32767])
    try {
      Short h = Short.parseShort(s);
      return new String[]{"Short", h.toString()}; // if we make it to this line, the data parsed fine as a Short
    } catch (java.lang.NumberFormatException ex) {
      // okay, guess it's not a Short
    }

    // 3. check if data is an Integer (4-byte integer with range [-(2e31), (2e31)-1])
    try {
      Integer i = Integer.parseInt(s);
      return new String[]{"Integer", i.toString()}; // if we make it to this line, the data parsed fine as an Integer
    } catch (java.lang.NumberFormatException ex) {
      // okay, guess it's not an Integer
    }

    String s_L_trimmed = s;

    // 4. check if data is a Long (8-byte integer with range [-(2e63), (2e63)-1])

    //    ...first, see if the last character of the string is "L" or "l"
    if (Arrays.asList("L", "l").contains(s.substring(s.length() - 1)) && s.length() > 1)
      s_L_trimmed = s.substring(0, s.length() - 1);

    try {
      Long l = Long.parseLong(s_L_trimmed);
      return new String[]{"Long", l.toString()}; // if we make it to this line, the data parsed fine as a Long
    } catch (java.lang.NumberFormatException ex) {
      // okay, guess it's not a Long
    }

    // 5. check if data is a Float (32-bit IEEE 754 floating point with approximate extents +/- 3.4028235e38)
    try {
      Float f = Float.parseFloat(s);

      if (!f.isInfinite()) // if it's beyond the range of Float, maybe it's not beyond the range of Double
        return new String[]{"Float", f.toString()}; // if we make it to this line, the data parsed fine as a Float and is finite

    } catch (java.lang.NumberFormatException ex) {
      // okay, guess it's not a Float
    }

    // 6. check if data is a Double (64-bit IEEE 754 floating point with approximate extents +/- 1.797693134862315e308 )
    try {
      Double d = Double.parseDouble(s);

      if (!d.isInfinite())
        return new String[]{"Double", d.toString()}; // if we make it to this line, the data parsed fine as a Double
      else // if it's beyond the range of Double, just return a String and let the user decide what to do
        return new String[]{"String", s};

    } catch (java.lang.NumberFormatException ex) {
      // okay, guess it's not a Double
    }

    // 7. revert to String by default, with caveats...

    //   a. if string has length 1, it is a single character
    if (s.length() == 1) return new String[]{"Character", s};

    //   b. if string contains any of {+, -, /, *, =}, attempt to parse equation
    Pattern pattern = Pattern.compile("[+-/*=]");
    Matcher matcher = pattern.matcher(s);

    //   ...evaluate the equation and send the result back to typify() to get the type
    if (matcher.find()) {
      ScriptEngineManager manager = new ScriptEngineManager();
      ScriptEngine engine = manager.getEngineByName("JavaScript");

      try {
        String evaluated = engine.eval(s).toString();
        return typify(evaluated);
      } catch (javax.script.ScriptException ex) {
        // okay, guess it's not an equation
      }
    }

    // ...if we've made it all the way to here without returning, give up and return "String"

    return new String[]{"String", s};

  }

}
于 2018-03-06T12:07:16.677 回答