Merge pull request #1156 from jotomo/properly-strip-incoming-cpp-profile-from-ns

Fix name of incoming profile from NS with timeshift/percentage.
This commit is contained in:
Milos Kozak 2018-06-25 16:01:41 +02:00 committed by GitHub
commit acbfe371fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View file

@ -103,14 +103,11 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
if(LocalProfilePlugin.LOCAL_PROFILE.equals(name)){ if(LocalProfilePlugin.LOCAL_PROFILE.equals(name)){
name = DecimalFormatter.to2Decimal(getProfileObject().percentageBasalSum()) + "U "; name = DecimalFormatter.to2Decimal(getProfileObject().percentageBasalSum()) + "U ";
} }
//Test if name is already containing percentage or timeshift if (isCPP) {
if (!name.endsWith("h)") || !name.endsWith("%)")) { name += "(" + percentage + "%";
if (isCPP) { if (timeshift != 0)
name += "(" + percentage + "%"; name += "," + timeshift + "h";
if (timeshift != 0) name += ")";
name += "," + timeshift + "h";
name += ")";
}
} }
return name; return name;
} }

View file

@ -8,14 +8,23 @@ import java.util.regex.Pattern;
*/ */
public class PercentageSplitter { public class PercentageSplitter {
// "Profile name (200%,2h)"
private static final Pattern percentagePattern = Pattern.compile("(.+)\\(\\d+%,\\d+h\\)");
// "Profile name (200%)"
private static final Pattern percentageShiftPattern = Pattern.compile("(.+)\\(\\d+%\\)");
/** Removes the suffix for percentage and timeshift from a profile name. */
public static String pureName(String name) { public static String pureName(String name) {
String newName = name; Matcher percentageMatch = percentagePattern.matcher(name);
String s = "(.*)\\((\\d+)\\%\\)"; if (percentageMatch.find()) {
Pattern r = Pattern.compile(s); return percentageMatch.group(1).trim();
Matcher m = r.matcher(name);
if (m.find()) {
newName = m.group(1);
} }
return newName;
Matcher percentageShiftMatch = percentageShiftPattern.matcher(name);
if (percentageShiftMatch.find()) {
return percentageShiftMatch.group(1).trim();
}
return name;
} }
} }

View file

@ -14,7 +14,12 @@ public class PercentageSplitterTest {
} }
@Test @Test
public void pureNameTest() throws Exception { public void pureNameTestPercentageOnly() {
assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp(101%)")); assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp(101%)"));
} }
@Test
public void pureNameTestPercentageAndShift() {
assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp (101%,2h)"));
}
} }