如何避免 if 语句 java7 或使代码更具可读性
How to avoid if statements java7 or make code more readable
我正在研究 java7 中的解析方法,因此我的代码中不允许使用流、lambda。
代码正在解析像 1-9
、-2--1
、2E-5 - 9E2"
这样的表达式,其中 E2
是 10^2
。作为解析的结果,我得到了两个数字的 String[]
:范围的第一个数字和最后一个数字。
我想实现的是使代码更具可读性,也许避免 if 语句并更改为某种模式。或者算法的改变。第一个想法是将每个 if 语句移动到另一个方法。但也许还有另一种方式。
/**
* @param text - analyzes the text below for the occurrence of a range of two numbers
* as a character to the range we use '-'
* the most advanced example for analysis is the one in which there are the most minus signs e. g.
* "-5e-2 - -2e-1" as text
*/
public static String[] prepareRangeNumberToCompare(String text) {
String textToBeAnalyzed = text.trim();
String[] splitText = textToBeAnalyzed.split("-");
//check simple case: (e. g. "0 - 10")
if (splitText.length < 2) {
return new String[] { text };
}
else if (splitText.length == 2) {
return splitText;
}
String firstNumber = splitText[0];
String secondObject = splitText[2];
//check the occurrence of the minus sign at the beginning (e.g. "-1 - 2")
List<String> asList = new LinkedList<String>(Arrays.asList(splitText));
if (text.startsWith("-")) {
firstNumber = "-" + splitText[1];
asList.remove(0);
}
//check the occurrence of the minus sign after the first occurrence of 'e|E' (e.g. "2E-5 - 9E2")
if (asList.size() > 2 && (firstNumber.endsWith("E") || firstNumber.endsWith("e"))) {
firstNumber = firstNumber + "-" + asList.get(1);
asList.remove(1);
secondObject = asList.get(1);
}
//check the occurrence of the minus sign before second text (e.g "-10 - -1")
if (asList.size() > 2 && (asList.get(1).isEmpty() || asList.get(1).matches("\s+"))) {
secondObject = "-" + asList.get(2);
asList.remove(1);
}
//check the occurrence of the minus sign after the second occurrence of 'e|E' (e.g. "2E-5 - 9E-2")
if (asList.size() > 2 && (secondObject.endsWith("E") || secondObject.endsWith("e"))) {
secondObject = secondObject + "-" + asList.get(2);
asList.remove(1);
}
//if we still have more than 2 items in the list, the user has supplied a wrong range
if (asList.size() > 2) {
return new String[] { text };
}
return new String[] { firstNumber, secondObject };
}
有更好的方法来实现您的目标:
public static List<String> prepareRangeNumberToCompare2(String text) {
List<String> result = new ArrayList<>();
String numberPattern = "-?[\d+](E-?\d+)?"; // Pattern for a single number.
String spacesPattern = "\s*"; // Pattern for allowing spaces.
String rangePattern = String.format("(%s)%s-%s(%s)", numberPattern, spacesPattern, spacesPattern,numberPattern); // final pattern is: "(-?[\d+](E-?\d+)?)\s*-\s*(-?[\d+](E-?\d+)?)"
Pattern pattern = Pattern.compile(rangePattern);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
result.add(matcher.group(1));
result.add(matcher.group(3));
}
return result;
}
顺便说一句 - 无需检索字符串数组。首选字符串列表。
我正在研究 java7 中的解析方法,因此我的代码中不允许使用流、lambda。
代码正在解析像 1-9
、-2--1
、2E-5 - 9E2"
这样的表达式,其中 E2
是 10^2
。作为解析的结果,我得到了两个数字的 String[]
:范围的第一个数字和最后一个数字。
我想实现的是使代码更具可读性,也许避免 if 语句并更改为某种模式。或者算法的改变。第一个想法是将每个 if 语句移动到另一个方法。但也许还有另一种方式。
/**
* @param text - analyzes the text below for the occurrence of a range of two numbers
* as a character to the range we use '-'
* the most advanced example for analysis is the one in which there are the most minus signs e. g.
* "-5e-2 - -2e-1" as text
*/
public static String[] prepareRangeNumberToCompare(String text) {
String textToBeAnalyzed = text.trim();
String[] splitText = textToBeAnalyzed.split("-");
//check simple case: (e. g. "0 - 10")
if (splitText.length < 2) {
return new String[] { text };
}
else if (splitText.length == 2) {
return splitText;
}
String firstNumber = splitText[0];
String secondObject = splitText[2];
//check the occurrence of the minus sign at the beginning (e.g. "-1 - 2")
List<String> asList = new LinkedList<String>(Arrays.asList(splitText));
if (text.startsWith("-")) {
firstNumber = "-" + splitText[1];
asList.remove(0);
}
//check the occurrence of the minus sign after the first occurrence of 'e|E' (e.g. "2E-5 - 9E2")
if (asList.size() > 2 && (firstNumber.endsWith("E") || firstNumber.endsWith("e"))) {
firstNumber = firstNumber + "-" + asList.get(1);
asList.remove(1);
secondObject = asList.get(1);
}
//check the occurrence of the minus sign before second text (e.g "-10 - -1")
if (asList.size() > 2 && (asList.get(1).isEmpty() || asList.get(1).matches("\s+"))) {
secondObject = "-" + asList.get(2);
asList.remove(1);
}
//check the occurrence of the minus sign after the second occurrence of 'e|E' (e.g. "2E-5 - 9E-2")
if (asList.size() > 2 && (secondObject.endsWith("E") || secondObject.endsWith("e"))) {
secondObject = secondObject + "-" + asList.get(2);
asList.remove(1);
}
//if we still have more than 2 items in the list, the user has supplied a wrong range
if (asList.size() > 2) {
return new String[] { text };
}
return new String[] { firstNumber, secondObject };
}
有更好的方法来实现您的目标:
public static List<String> prepareRangeNumberToCompare2(String text) {
List<String> result = new ArrayList<>();
String numberPattern = "-?[\d+](E-?\d+)?"; // Pattern for a single number.
String spacesPattern = "\s*"; // Pattern for allowing spaces.
String rangePattern = String.format("(%s)%s-%s(%s)", numberPattern, spacesPattern, spacesPattern,numberPattern); // final pattern is: "(-?[\d+](E-?\d+)?)\s*-\s*(-?[\d+](E-?\d+)?)"
Pattern pattern = Pattern.compile(rangePattern);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
result.add(matcher.group(1));
result.add(matcher.group(3));
}
return result;
}
顺便说一句 - 无需检索字符串数组。首选字符串列表。