Refactor and clean up.

This commit is contained in:
Johannes Mockenhaupt 2018-10-13 20:56:40 +02:00
parent 51315fc942
commit 4f896fccdb
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 9 additions and 20 deletions

View file

@ -101,11 +101,11 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
return profile; return profile;
} }
/** Note: the name returned here is used as the PS name when uploading to NS. When such a PS is retrieved /**
* Note: the name returned here is used as the PS name when uploading to NS. When such a PS is retrieved
* again from NS, the added parts must be removed again, see * again from NS, the added parts must be removed again, see
* {@link info.nightscout.utils.PercentageSplitter#pureName} */ * {@link info.nightscout.utils.PercentageSplitter#pureName}
// TODO refactor to remove this coupling; either move this to a renamed PercentageSplitter, e.g. */
// ProfileSwitchNameConverter to convert in both directions, are add the pureName method here.
public String getCustomizedName() { public String getCustomizedName() {
String name = profileName; String name = profileName;
if(LocalProfilePlugin.LOCAL_PROFILE.equals(name)){ if(LocalProfilePlugin.LOCAL_PROFILE.equals(name)){

View file

@ -8,26 +8,15 @@ import java.util.regex.Pattern;
*/ */
public class PercentageSplitter { public class PercentageSplitter {
// "Profile name (200%,2h)" // Matches "Profile name (200%,-2h)", "Profile name (50%)
private static final Pattern percentagePattern = Pattern.compile("(.+)\\(\\d+%,-?\\d+h\\)"); private static final Pattern splitPattern = 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. This is the inverse of what /** Removes the suffix for percentage and timeshift from a profile name. This is the inverse of what
* {@link info.nightscout.androidaps.db.ProfileSwitch#getCustomizedName()} does. * {@link info.nightscout.androidaps.db.ProfileSwitch#getCustomizedName()} does.
* Since the customized name is used for the PS upload to NS, this is needed get the original profile name * Since the customized name is used for the PS upload to NS, this is needed get the original profile name
* when retrieving the PS from NS again. */ * when retrieving the PS from NS again. */
public static String pureName(String name) { public static String pureName(String name) {
Matcher percentageMatch = percentagePattern.matcher(name); Matcher percentageMatch = splitPattern.matcher(name);
if (percentageMatch.find()) { return percentageMatch.find() ? percentageMatch.group(1).trim() : name;
return percentageMatch.group(1).trim();
}
Matcher percentageShiftMatch = percentageShiftPattern.matcher(name);
if (percentageShiftMatch.find()) {
return percentageShiftMatch.group(1).trim();
}
return name;
} }
} }

View file

@ -25,6 +25,6 @@ public class PercentageSplitterTest {
@Test @Test
public void pureNameTestPercentageAndNegtiveTimeShift() { public void pureNameTestPercentageAndNegtiveTimeShift() {
assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp (101%,-2h)")); assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp (50%,-2h)"));
} }
} }