Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] refactor nest block to improve readability by guard and extract #2057

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand All @@ -31,6 +32,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;
Expand Down Expand Up @@ -68,6 +70,7 @@ private CollectUtil() {

/**
* count match keyword number
*
* @param content content
* @param keyword keyword
* @return match num
Expand All @@ -94,36 +97,36 @@ public static DoubleAndUnit extractDoubleAndUnitFromStr(String str) {
return null;
}
DoubleAndUnit doubleAndUnit = new DoubleAndUnit();
// only contains double value situation
try {
Double doubleValue = Double.parseDouble(str);
doubleAndUnit.setValue(doubleValue);
return doubleAndUnit;
} catch (Exception e) {
log.debug(e.getMessage());
}

if (!str.matches(DOUBLE_AND_UNIT_CHECK_REGEX)){
// do not match DOUBLE_AND_UNIT format situation
if (!str.matches(DOUBLE_AND_UNIT_CHECK_REGEX)) {
return doubleAndUnit;
}
// extract unit from str value, eg: 23.43GB, 33KB, 44.22G
try {
// B KB MB GB % ....
for (String unitSymbol : UNIT_SYMBOLS) {
int index = str.indexOf(unitSymbol);
Double doubleValue = null;
String unit = null;
if (index == 0) {
Double doubleValue = 0d;
String unit = str.trim();
doubleAndUnit.setValue(doubleValue);
doubleAndUnit.setUnit(unit);
return doubleAndUnit;
doubleValue = 0d;
unit = str.trim();
}
if (index > 0) {
Double doubleValue = Double.parseDouble(str.substring(0, index));
String unit = str.substring(index).trim();
doubleAndUnit.setValue(doubleValue);
doubleAndUnit.setUnit(unit);
return doubleAndUnit;
doubleValue = Double.parseDouble(str.substring(0, index));
unit = str.substring(index).trim();
}
doubleAndUnit.setValue(doubleValue);
doubleAndUnit.setUnit(unit);
return doubleAndUnit;
}
} catch (Exception e) {
log.debug(e.getMessage());
Expand Down Expand Up @@ -209,6 +212,7 @@ public static boolean notContainCryPlaceholder(JsonElement jsonElement) {

/**
* match existed cry placeholder fields ^o^field^o^
*
* @param jsonElement json element
* @return match field str
*/
Expand All @@ -233,71 +237,73 @@ public static JsonElement replaceCryPlaceholder(JsonElement jsonElement, Map<Str
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
JsonElement element = entry.getValue();
if (!element.isJsonPrimitive()) {
jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
continue;
}
// Replace normal VALUE value
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.find()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!cryingMatcher.find()) {
continue;
}
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param == null) {
continue;
}
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
jsonObject.addProperty(entry.getKey(), value);
} else {
value = value.replace(group, (String) param.getValue());
}
} else {
jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
}
jsonObject.addProperty(entry.getKey(), value);
}
} else if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
Iterator<JsonElement> iterator = jsonArray.iterator();
int index = 0;
while (iterator.hasNext()) {
JsonElement element = iterator.next();
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.find()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
if (!element.isJsonPrimitive()) {
jsonArray.set(index, replaceCryPlaceholder(element, configmap));
}
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!cryingMatcher.find()) {
index++;
continue;
}
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
}
} else {
jsonArray.set(index, replaceCryPlaceholder(element, configmap));
}
index++;
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
}
}
return jsonElement;
Expand Down Expand Up @@ -440,6 +446,7 @@ public static void replaceFieldsForPushStyleMonitor(Metrics metrics, Map<String,
* convert 16 hexString to byte[]
* eg: 302c0201010409636f6d6d756e697479a11c020419e502e7020100020100300e300c06082b060102010102000500
* 16进制字符串不区分大小写,返回的数组相同
*
* @param hexString 16 hexString
* @return byte[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Configmap {
private String key;

/**
* parameter value 参数value
* parameter value
*/
private Object value;

Expand Down
Loading