public TimeAmount cloneInstance() { TimeAmount clone = new TimeAmount(); clone.quantity = quantity; clone.type = type; return clone; }
public TimeAmount cloneInstance() { TimeAmount clone = new TimeAmount(); clone.quantity = quantity; clone.type = type; return clone; }
/** * Parses a time amount expression. For example: "10second", "-1year", ... * @return A TimeAmount instance. * @throws IllegalArgumentException If the expression is not valid */ public static TimeAmount parse(String timeAmount) { if (timeAmount == null || timeAmount.length() == 0) { throw new IllegalArgumentException("Empty time amount expression"); } String number = ""; String expr = timeAmount.trim(); boolean isNegative = expr.startsWith("-"); boolean isPositive = expr.startsWith("+"); int i = isNegative || isPositive ? 1 : 0; for (; i<expr.length(); i++) { char ch = expr.charAt(i); if (Character.isDigit(ch)) number += ch; else break; } String type = expr.substring(i).trim(); DateIntervalType intervalType = DateIntervalType.getByName(type); if (number.length() == 0) { throw new IllegalArgumentException("Missing quantity (ex '-10year'): "+ timeAmount); } if (intervalType == null) { throw new IllegalArgumentException("Invalid interval type (ex '-10year'): "+ timeAmount); } return new TimeAmount(Long.parseLong(number) * (isNegative ? -1 : 1), intervalType); } }
/** * Parses a time amount expression. For example: "10second", "-1year", ... * @return A TimeAmount instance. * @throws IllegalArgumentException If the expression is not valid */ public static TimeAmount parse(String timeAmount) { if (timeAmount == null || timeAmount.length() == 0) { throw new IllegalArgumentException("Empty time amount expression"); } String number = ""; String expr = timeAmount.trim(); boolean isNegative = expr.startsWith("-"); boolean isPositive = expr.startsWith("+"); int i = isNegative || isPositive ? 1 : 0; for (; i<expr.length(); i++) { char ch = expr.charAt(i); if (Character.isDigit(ch)) number += ch; else break; } String type = expr.substring(i).trim(); DateIntervalType intervalType = DateIntervalType.getByName(type); if (number.length() == 0) { throw new IllegalArgumentException("Missing quantity (ex '-10year'): "+ timeAmount); } if (intervalType == null) { throw new IllegalArgumentException("Invalid interval type (ex '-10year'): "+ timeAmount); } return new TimeAmount(Long.parseLong(number) * (isNegative ? -1 : 1), intervalType); } }
protected void changeQuantity(long q) { if (timeAmount == null) { timeAmount = new TimeAmount(); DateIntervalType type = INTERVAL_TYPES.get(0); timeAmount.setType(type); } timeAmount.setQuantity(q); onChangeCommand.execute(); } }
@Override public String getValue() { final Double quantity = view.getQuantity(); final DateIntervalType type = getSelectedIntervalType(); return new TimeAmount(quantity.longValue(), type).toString(); }
protected void changeQuantity(long q) { if (timeAmount == null) { timeAmount = new TimeAmount(); DateIntervalType type = INTERVAL_TYPES.get(0); timeAmount.setType(type); } timeAmount.setQuantity(q); onChangeCommand.execute(); } }
@Override public String getValue() { final Double quantity = view.getQuantity(); final DateIntervalType type = getSelectedIntervalType(); return new TimeAmount(quantity.longValue(), type).toString(); }
public void init(final TimeAmount ta, Command onChangeCommand) { this.onChangeCommand = onChangeCommand; this.timeAmount = ta != null ? ta : new TimeAmount(); view.setQuantity(timeAmount.getQuantity()); view.clearIntervalTypeSelector(); for (int i=0; i< INTERVAL_TYPES.size(); i++) { DateIntervalType type = INTERVAL_TYPES.get(i); view.addIntervalTypeItem(type); if (timeAmount != null && timeAmount.getType().equals(type)) { view.setSelectedTypeIndex(i); } } }
public void init(final TimeAmount ta, Command onChangeCommand) { this.onChangeCommand = onChangeCommand; this.timeAmount = ta != null ? ta : new TimeAmount(); view.setQuantity(timeAmount.getQuantity()); view.clearIntervalTypeSelector(); for (int i=0; i< INTERVAL_TYPES.size(); i++) { DateIntervalType type = INTERVAL_TYPES.get(i); view.addIntervalTypeItem(type); if (timeAmount != null && timeAmount.getType().equals(type)) { view.setSelectedTypeIndex(i); } } }
@Test public void testViewInitialization() { TimeAmount timeAmount = new TimeAmount(10, DateIntervalType.DAY); TimeAmountEditor editor = new TimeAmountEditor(timeAmountView); editor.init(timeAmount, changeCommand); verify(timeAmountView).clearIntervalTypeSelector(); verify(timeAmountView, times(TimeAmountEditor.INTERVAL_TYPES.size())).addIntervalTypeItem(any(DateIntervalType.class)); verify(timeAmountView).setSelectedTypeIndex(3); verify(timeAmountView).setQuantity(10); }
@Test public void testViewInitialization() { TimeAmount timeAmount = new TimeAmount(10, DateIntervalType.DAY); TimeAmountEditor editor = new TimeAmountEditor(timeAmountView); editor.init(timeAmount, changeCommand); verify(timeAmountView).clearIntervalTypeSelector(); verify(timeAmountView, times(TimeAmountEditor.INTERVAL_TYPES.size())).addIntervalTypeItem(any(DateIntervalType.class)); verify(timeAmountView).setSelectedTypeIndex(3); verify(timeAmountView).setQuantity(10); }
@Test public void testViewInitialization() { TimeInstant beginMonth = new TimeInstant(TimeInstant.TimeMode.BEGIN, DateIntervalType.MONTH, Month.JANUARY, new TimeAmount(10, DateIntervalType.DAY)); TimeInstantEditor timeInstantEditor = new TimeInstantEditor(timeInstantView, timeAmountEditor); timeInstantEditor.init(beginMonth, changeCommand); assertEquals(timeInstantView, timeInstantEditor.view); verify(timeInstantView).init(timeInstantEditor); verify(timeInstantView).clearTimeModeSelector(); verify(timeInstantView, times(TimeInstant.TimeMode.values().length)).addTimeModeItem(any(TimeInstant.TimeMode.class)); verify(timeInstantView).setSelectedTimeModeIndex(TimeInstant.TimeMode.BEGIN.getIndex()); verify(timeInstantView).enableIntervalTypeSelector(); verify(timeInstantView).clearIntervalTypeSelector(); verify(timeInstantView, times(TimeInstantEditor.INTERVAL_TYPES.size())).addIntervalTypeItem(any(DateIntervalType.class)); verify(timeInstantView).setSelectedIntervalTypeIndex(3); }
@Test public void testViewInitialization() { TimeInstant beginMonth = new TimeInstant(TimeInstant.TimeMode.BEGIN, DateIntervalType.MONTH, Month.JANUARY, new TimeAmount(10, DateIntervalType.DAY)); TimeInstantEditor timeInstantEditor = new TimeInstantEditor(timeInstantView, timeAmountEditor); timeInstantEditor.init(beginMonth, changeCommand); assertEquals(timeInstantView, timeInstantEditor.view); verify(timeInstantView).init(timeInstantEditor); verify(timeInstantView).clearTimeModeSelector(); verify(timeInstantView, times(TimeInstant.TimeMode.values().length)).addTimeModeItem(any(TimeInstant.TimeMode.class)); verify(timeInstantView).setSelectedTimeModeIndex(TimeInstant.TimeMode.BEGIN.getIndex()); verify(timeInstantView).enableIntervalTypeSelector(); verify(timeInstantView).clearIntervalTypeSelector(); verify(timeInstantView, times(TimeInstantEditor.INTERVAL_TYPES.size())).addIntervalTypeItem(any(DateIntervalType.class)); verify(timeInstantView).setSelectedIntervalTypeIndex(3); }
@Test public void testDecreaseQuantity() { TimeAmount timeAmount = new TimeAmount(10, DateIntervalType.DAY); TimeAmountEditor timeAmountEditor = new TimeAmountEditor(timeAmountView); timeAmountEditor.init(timeAmount, changeCommand); timeAmountEditor.decreaseQuantity(); verify(timeAmountView).setQuantity(9); verify(changeCommand).execute(); assertEquals(timeAmount.getQuantity(), 9); }
@Test public void testDecreaseQuantity() { TimeAmount timeAmount = new TimeAmount(10, DateIntervalType.DAY); TimeAmountEditor timeAmountEditor = new TimeAmountEditor(timeAmountView); timeAmountEditor.init(timeAmount, changeCommand); timeAmountEditor.decreaseQuantity(); verify(timeAmountView).setQuantity(9); verify(changeCommand).execute(); assertEquals(timeAmount.getQuantity(), 9); }
@Test public void testIncreaseQuantity() { TimeAmount timeAmount = new TimeAmount(10, DateIntervalType.DAY); TimeAmountEditor timeAmountEditor = new TimeAmountEditor(timeAmountView); timeAmountEditor.init(timeAmount, changeCommand); timeAmountEditor.increaseQuantity(); verify(timeAmountView).setQuantity(11); verify(changeCommand).execute(); assertEquals(timeAmount.getQuantity(), 11); } }
@Test public void testIncreaseQuantity() { TimeAmount timeAmount = new TimeAmount(10, DateIntervalType.DAY); TimeAmountEditor timeAmountEditor = new TimeAmountEditor(timeAmountView); timeAmountEditor.init(timeAmount, changeCommand); timeAmountEditor.increaseQuantity(); verify(timeAmountView).setQuantity(11); verify(changeCommand).execute(); assertEquals(timeAmount.getQuantity(), 11); } }