BusinessDurationParser { public static BusinessDuration parse(String durationString) { Preconditions.checkNotNull(durationString, "duration is null"); durationString = durationString.trim(); int index = indexOfNonWhite(durationString, 0); char lead = durationString.charAt(index); if (lead == '+' || lead == '-') { index++; } NumberFormat format = NumberFormat.getNumberInstance(Locale.US); index = indexOfNonWhite(durationString, index); ParsePosition position = new ParsePosition(index); Number quantity = format.parse(durationString, position); if (quantity == null) { throw new IllegalArgumentException("improper format of duration '" + durationString + "'"); } String unitText = durationString.substring(position.getIndex()).trim(); boolean businessTime = false; if (unitText.startsWith("business ")) { businessTime = true; unitText = unitText.substring("business ".length()); } Integer unit = calendarFields.get(unitText); if (unit == null) { throw new IllegalArgumentException("improper format of duration '" + durationString + "'"); } int calendarField = unit.intValue(); int amount = quantity.intValue(); if (lead == '-') { amount = -amount; } return new BusinessDuration(calendarField, amount, businessTime); }  static BusinessDuration parse(String durationString);  }
@Test(dataProvider = "getDurations") public void parseDurations(String durationString, BusinessDuration expected) { BusinessDuration businessDuration = BusinessDurationParser.parse(durationString); assertEquals(businessDuration, expected); }
@Test(expectedExceptions = IllegalArgumentException.class) public void parseBadDuration() { BusinessDurationParser.parse("1 business week"); }