@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object top = stack.pop(); if (!(top instanceof Long)) { throw new WarpScriptException(getName() + " expects a number of time units (LONG) on top of the stack."); } long duration = ((Number) top).longValue(); StringBuffer buf = new StringBuffer(); ReadablePeriod period = new MutablePeriod(duration / Constants.TIME_UNITS_PER_MS); ISOPeriodFormat.standard().getPrinter().printTo(buf, period, Locale.US); stack.push(buf.toString()); return stack; }
public void addDuration(Duration value, int columnNumber) { HSSFRow currentRow = getRow(); HSSFCell cell = currentRow.createCell(columnNumber); PeriodFormatter fmt = new PeriodFormatterBuilder().printZeroAlways().appendHours().appendSeparator(":").minimumPrintedDigits(2) .appendMinutes().toFormatter(); MutablePeriod valueFormatted = new MutablePeriod(value.getMillis(), PeriodType.time()); if (value.toPeriod().getMinutes() < 0) { valueFormatted.setMinutes(-value.toPeriod().getMinutes()); if (value.toPeriod().getHours() == 0) { fmt = new PeriodFormatterBuilder().printZeroAlways().appendLiteral("-").appendHours().appendSeparator(":") .minimumPrintedDigits(2).appendMinutes().toFormatter(); } } cell.setCellValue(fmt.print(valueFormatted)); cell.setCellStyle(getExcelStyle(excelStyle.getValueStyle(), wrapText)); }
@Test public void retrievePeriodValueFromConfiguration() { Configuration configuration = ConfigurationProvider.getConfiguration(); MutablePeriod referenceValue = new MutablePeriod(); ISOPeriodFormat.standard().getParser().parseInto(referenceValue, "P1Y1M1W1DT1H1M1S", 0, Locale.ENGLISH); String periodAsString = configuration.get("periodValueA"); Period period = configuration.get("periodValueA", Period.class); assertThat(periodAsString, equalTo("P1Y1M1W1DT1H1M1S")); assertThat(period, equalTo(referenceValue.toPeriod())); }
/** * Creates a new period based on another using the {@link ConverterManager}. * * @param period the period to convert * @param type which set of fields this period supports, null means use type from object * @param chrono the chronology to use, null means ISO default * @throws IllegalArgumentException if period is invalid * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected BasePeriod(Object period, PeriodType type, Chronology chrono) { super(); PeriodConverter converter = ConverterManager.getInstance().getPeriodConverter(period); type = (type == null ? converter.getPeriodType(period) : type); type = checkPeriodType(type); iType = type; if (this instanceof ReadWritablePeriod) { iValues = new int[size()]; chrono = DateTimeUtils.getChronology(chrono); converter.setInto((ReadWritablePeriod) this, period, chrono); } else { iValues = new MutablePeriod(period, type, chrono).getValues(); } }
/** * Parses a period from the given text, returning a new MutablePeriod. * * @param text text to parse * @return parsed value in a MutablePeriod object * @throws IllegalArgumentException if any field is out of range */ public MutablePeriod parseMutablePeriod(String text) { checkParser(); MutablePeriod period = new MutablePeriod(0, iParseType); int newPos = getParser().parseInto(period, text, 0, iLocale); if (newPos >= 0) { if (newPos >= text.length()) { return period; } } else { newPos = ~newPos; } throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos)); }
@Override public int parseInto(ReadWritablePeriod period, String text, int position, Locale locale) { int bestValidPos = position; ReadWritablePeriod bestValidPeriod = null; int bestInvalidPos = position; for (PeriodParser parser : parsers) { ReadWritablePeriod parsedPeriod = new MutablePeriod(); int parsePos = parser.parseInto(parsedPeriod, text, position, locale); if (parsePos >= position) { if (parsePos > bestValidPos) { bestValidPos = parsePos; bestValidPeriod = parsedPeriod; if (parsePos >= text.length()) { break; } } } else if (parsePos < 0) { parsePos = ~parsePos; if (parsePos > bestInvalidPos) { bestInvalidPos = parsePos; } } } if (bestValidPos > position || (bestValidPos == position)) { // Restore the state to the best valid parse. if (bestValidPeriod != null) { period.setPeriod(bestValidPeriod); } return bestValidPos; } return ~bestInvalidPos; }
@Override public List<Object> visitValueDuration(com.rocana.configuration.antlr.ConfigurationParser.ValueDurationContext ctx) { MutablePeriod period = new MutablePeriod(); if (ctx.DURATION_SIMPLE() != null && DURATION_PARSER_SIMPLE.parseInto(period, ctx.DURATION_SIMPLE().getText(), 0, Locale.US) > 0) { return Lists.<Object>newArrayList(period.toPeriod()); } else if (ctx.DURATION_ISO8601() != null && DURATION_PARSER_ISO8601.parseInto(period, ctx.DURATION_ISO8601().getText(), 0, Locale.US) > 0) { return Lists.<Object>newArrayList(period.toPeriod()); } else { throw new ConfigurationException("Unable to parse duration value:'" + ctx.getText() + "' - Not in a known format"); } }
@Override public Object create(Object request, SpecimenContext context) { if (!(request.equals(ReadablePeriod.class) || request.equals(ReadWritablePeriod.class))) return new NoSpecimen(); DateTime dateA = (DateTime) context.resolve(DateTime.class); DateTime dateB = (DateTime) context.resolve(DateTime.class); if (dateA.isBefore(dateB)) return new MutablePeriod(dateA, dateB); else return new MutablePeriod(dateB, dateA); }
/** * Returns the period required to start the milestone window with the given name. * * @param windowName the name of the milestone window * @return the period required to start the milestone window */ public Period getWindowStart(WindowName windowName) { MutablePeriod period = new MutablePeriod(); for (MilestoneWindow window : milestoneWindows) { if (window.getName().equals(windowName)) { break; } period.add(window.getPeriod()); } return period.toPeriod(); }
/** * Returns the period required to end the milestone window with the given name. * * @param windowName the name of the milestone window * @return the period required to end the milestone window */ public Period getWindowEnd(WindowName windowName) { MutablePeriod period = new MutablePeriod(); for (MilestoneWindow window : milestoneWindows) { period.add(window.getPeriod()); if (window.getName().equals(windowName)) { break; } } return period.toPeriod(); }
/** * Returns the duration of all milestones in this schedule. * * @return the duration of all milestones */ @Ignore public Period getDuration() { MutablePeriod duration = new MutablePeriod(); for (Milestone milestone : milestones) { duration.add(milestone.getMaximumDuration()); } return duration.toPeriod(); }
private Period getPeriodFromValue(List<String> readableValues, Locale locale) { ReadWritablePeriod period = new MutablePeriod(); final JodaFormatter jodaFormatter = new JodaFormatter(); for (String s : readableValues) { period.add(jodaFormatter.parse(s, locale)); } return period.toPeriod(); }
public void testSetInto_Object() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); ReadableDurationConverter.INSTANCE.setInto(m, new Duration( 3L * DateTimeConstants.MILLIS_PER_DAY + 4L * DateTimeConstants.MILLIS_PER_MINUTE + 5L ), null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(3 * 24, m.getHours()); assertEquals(4, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(5, m.getMillis()); }
public void testSetIntoPeriod_Object1() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y6M9DT12H24M48S", null); assertEquals(2, m.getYears()); assertEquals(6, m.getMonths()); assertEquals(9, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(48, m.getSeconds()); assertEquals(0, m.getMillis()); }
public void testSetIntoPeriod_Object2() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M48S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(48, m.getSeconds()); assertEquals(0, m.getMillis()); }
public void testSetIntoPeriod_Object3() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M48.034S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(48, m.getSeconds()); assertEquals(34, m.getMillis()); }
public void testSetIntoPeriod_Object4() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M.056S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(56, m.getMillis()); }
public void testSetIntoPeriod_Object5() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M56.S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(56, m.getSeconds()); assertEquals(0, m.getMillis()); }
public void testSetIntoPeriod_Object6() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M56.1234567S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(56, m.getSeconds()); assertEquals(123, m.getMillis()); }
public void testSetIntoPeriod_Object7() throws Exception { MutablePeriod m = new MutablePeriod(1, 0, 1, 1, 1, 1, 1, 1, PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3D", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(0, m.getMillis()); }
public void testSetInto_Object() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); ReadablePeriodConverter.INSTANCE.setInto(m, new Period(0, 0, 0, 3, 0, 4, 0, 5), null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(0, m.getHours()); assertEquals(4, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(5, m.getMillis()); }
public void testSetIntoPeriod_Object1() throws Exception { Interval i = new Interval(100L, 223L); MutablePeriod m = new MutablePeriod(PeriodType.millis()); ReadableIntervalConverter.INSTANCE.setInto(m, i, null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(123, m.getMillis()); }
public void testSetIntoPeriod_Object2() throws Exception { Interval i = new Interval(100L, 223L); MutablePeriod m = new MutablePeriod(PeriodType.millis()); ReadableIntervalConverter.INSTANCE.setInto(m, i, CopticChronology.getInstance()); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(123, m.getMillis()); }
public void testParseMutablePeriod_simple() { MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); assertEquals(expect, f.parseMutablePeriod("P1Y2M3W4DT5H6M7.008S")); try { f.parseMutablePeriod("ABC"); fail(); } catch (IllegalArgumentException ex) {} }
public void testParseInto_simple() { MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); MutablePeriod result = new MutablePeriod(); assertEquals(20, f.parseInto(result, "P1Y2M3W4DT5H6M7.008S", 0)); assertEquals(expect, result); try { f.parseInto(null, "P1Y2M3W4DT5H6M7.008S", 0); fail(); } catch (IllegalArgumentException ex) {} assertEquals(~0, f.parseInto(result, "ABC", 0)); }