DiskSpaceThreshold { public static DiskSpaceThreshold parse(String thresholdString) throws DiskSpaceCheckerException { if (thresholdString.startsWith("-")) { throw new DiskSpaceCheckerException("Negative threshold string not allowed: " + thresholdString); } Matcher m = THRESHOLD_PATTERN.matcher(thresholdString); if (!m.matches()) { throw new DiskSpaceCheckerException("Cannot parse threshold string " + thresholdString); } DiskSpaceThreshold t; long value = Long.valueOf(m.group(1)); if (StringUtils.isBlank(m.group(2))) { t = new DiskSpaceThreshold(value, false); } else { String unit = m.group(2).toUpperCase(); switch (unit) { case "%": if (value > 100) { throw new DiskSpaceCheckerException("Percentage value higher than 100%: " + thresholdString); } else { t = new DiskSpaceThreshold(value, true); } break; case "G": value = value * KILOBYTE; case "M": value = value * KILOBYTE; case "K": value = value * KILOBYTE; t = new DiskSpaceThreshold(value, false); break; default: throw new DiskSpaceCheckerException("Unexpected unit value: " + unit); } } return t; } DiskSpaceThreshold(final long threshold, final boolean isPercentage); static DiskSpaceThreshold parse(String thresholdString); long getThresholdValue(); boolean isPercentage(); boolean withinThreshold(DiskSpace diskSpace); boolean withinThreshold(long freeSpace, long maxSpace);  }
@Test(expected=uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionForBadString() throws Exception { DiskSpaceThreshold.parse("abcd"); }
@Test(expected=uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionForNegativeString() throws Exception { DiskSpaceThreshold.parse("-5"); }
@Test(expected=uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionForBadPercentage() throws Exception { DiskSpaceThreshold.parse("105 %"); }