You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
backend/src/main/java/com/xydl/cac/comparator/FloatCompare.java

93 lines
3.1 KiB
Java

package com.xydl.cac.comparator;
import com.xydl.cac.exception.BusinessException;
import java.util.Date;
import java.util.LinkedHashMap;
public class FloatCompare extends Comparator {
private float IgnoreValueN = -0.00001f;
private float IgnoreValue = 0.00001f;
@Override
public boolean compare(Object source, String threshold, Date lastDTime) {
if (source != null) {
float s1 = (float) source;
if ("BTW".equalsIgnoreCase(operator)) {
String[] strs = threshold.split(",");
float t1 = Float.parseFloat(strs[0]);
float t3 = Float.parseFloat(strs[1]);
if (t1 <= s1 && s1 <= t3) {
return true;
}
} else if ("NTW".equalsIgnoreCase(operator)) {
String[] strs = threshold.split(",");
float t1 = Float.parseFloat(strs[0]);
float t3 = Float.parseFloat(strs[1]);
if (t1 > s1 || s1 > t3) {
return true;
}
} else {
float t2 = Float.parseFloat(threshold);
float dif = s1 - t2;
if ("EQU".equalsIgnoreCase(operator)) {
if (IgnoreValueN < dif && dif < IgnoreValue) {
return true;
}
} else if ("NEQ".equalsIgnoreCase(operator)) {
if (IgnoreValueN > dif || dif > IgnoreValue) {
return true;
}
} else if ("LSS".equalsIgnoreCase(operator)) {
if (IgnoreValueN > dif) {
return true;
}
} else if ("LEQ".equalsIgnoreCase(operator)) {
if (IgnoreValue > dif) {
return true;
}
} else if ("GTR".equalsIgnoreCase(operator)) {
if (dif > IgnoreValue) {
return true;
}
} else if ("GEQ".equalsIgnoreCase(operator)) {
if (dif > IgnoreValueN) {
return true;
}
}
}
}
return false;
}
@Override
public LinkedHashMap<String, String> supportedOperator() {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("GTR", "大于");
map.put("GEQ", "大等于");
map.put("LSS", "小于");
map.put("LEQ", "小等于");
map.put("BTW", "介于A-B之间");
map.put("NTW", "在A-B之外");
return map;
}
@Override
public void valid(String operator, String threshold) throws Exception {
if ("BTW".equalsIgnoreCase(operator)) {
if (!threshold.contains(",")) {
throw new BusinessException("阈值不正确");
}
} else if ("NTW".equalsIgnoreCase(operator)) {
if (!threshold.contains(",")) {
throw new BusinessException("阈值不正确");
}
}
}
}