From 4f896fccdb7b48ccab2e92bdc1ab43489f035e7d Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 13 Oct 2018 20:56:40 +0200 Subject: [PATCH] Refactor and clean up. --- .../androidaps/db/ProfileSwitch.java | 8 ++++---- .../nightscout/utils/PercentageSplitter.java | 19 ++++--------------- .../utils/PercentageSplitterTest.java | 2 +- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java index 7c74e365a8..f7252e97df 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java +++ b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java @@ -101,11 +101,11 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface { 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 - * {@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. + * {@link info.nightscout.utils.PercentageSplitter#pureName} + */ public String getCustomizedName() { String name = profileName; if(LocalProfilePlugin.LOCAL_PROFILE.equals(name)){ diff --git a/app/src/main/java/info/nightscout/utils/PercentageSplitter.java b/app/src/main/java/info/nightscout/utils/PercentageSplitter.java index 5f019a07f0..d590426b28 100644 --- a/app/src/main/java/info/nightscout/utils/PercentageSplitter.java +++ b/app/src/main/java/info/nightscout/utils/PercentageSplitter.java @@ -8,26 +8,15 @@ import java.util.regex.Pattern; */ 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+%\\)"); + // Matches "Profile name (200%,-2h)", "Profile name (50%) + private static final Pattern splitPattern = Pattern.compile("(.+)\\(\\d+%(,-?\\d+h)?\\)"); /** 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. * 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. */ public static String pureName(String name) { - Matcher percentageMatch = percentagePattern.matcher(name); - if (percentageMatch.find()) { - return percentageMatch.group(1).trim(); - } - - Matcher percentageShiftMatch = percentageShiftPattern.matcher(name); - if (percentageShiftMatch.find()) { - return percentageShiftMatch.group(1).trim(); - } - - return name; + Matcher percentageMatch = splitPattern.matcher(name); + return percentageMatch.find() ? percentageMatch.group(1).trim() : name; } } diff --git a/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java b/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java index 087e21ae11..d57b0ea230 100644 --- a/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java +++ b/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java @@ -25,6 +25,6 @@ public class PercentageSplitterTest { @Test public void pureNameTestPercentageAndNegtiveTimeShift() { - assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp (101%,-2h)")); + assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp (50%,-2h)")); } }