fix(StateManager): 修复配置值为空时的解析异常
- 在 getStateAsInt、getStateAsLong、getStateAsFloat、getStateAsBoolean、 getStateAsDouble、getStateAsChar、getStateAsByte 和 getStateAsShort 方法中 增加了对配置值为空的检查 - 避免了空指针异常和 NumberFormatException - 提高了代码的健壮性和可靠性
This commit is contained in:
@@ -130,34 +130,42 @@ public class StateManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getStateAsInt(String key) {
|
public int getStateAsInt(String key) {
|
||||||
return Integer.parseInt(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Integer.parseInt(value) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getStateAsLong(String key) {
|
public long getStateAsLong(String key) {
|
||||||
return Long.parseLong(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Long.parseLong(value) : 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getStateAsFloat(String key) {
|
public float getStateAsFloat(String key) {
|
||||||
return Float.parseFloat(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Float.parseFloat(value) : 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getStateAsBoolean(String key) {
|
public boolean getStateAsBoolean(String key) {
|
||||||
return Boolean.parseBoolean(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Boolean.parseBoolean(value) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getStateAsDouble(String key) {
|
public double getStateAsDouble(String key) {
|
||||||
return Double.parseDouble(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Double.parseDouble(value) : 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public char getStateAsChar(String key) {
|
public char getStateAsChar(String key) {
|
||||||
return configMap.get(key).charAt(0);
|
String value = configMap.get(key);
|
||||||
|
return value != null && !value.isEmpty() ? value.charAt(0) : '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte getStateAsByte(String key) {
|
public byte getStateAsByte(String key) {
|
||||||
return Byte.parseByte(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Byte.parseByte(value) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public short getStateAsShort(String key) {
|
public short getStateAsShort(String key) {
|
||||||
return Short.parseShort(configMap.get(key));
|
String value = configMap.get(key);
|
||||||
|
return value != null ? Short.parseShort(value) : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user