/** * 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(); } }
/** * Sets the values of the mutable duration from the specified interval. * * @param writablePeriod the period to modify * @param object the interval to set from * @param chrono the chronology to use */ public void setInto(ReadWritablePeriod writablePeriod, Object object, Chronology chrono) { ReadableInterval interval = (ReadableInterval) object; chrono = (chrono != null ? chrono : DateTimeUtils.getIntervalChronology(interval)); long start = interval.getStartMillis(); long end = interval.getEndMillis(); int[] values = chrono.get(writablePeriod, start, end); for (int i = 0; i < values.length; i++) { writablePeriod.setValue(i, values[i]); } }
/** * Extracts duration values from an object of this converter's type, and * sets them into the given ReadWritableDuration. * * @param period period to get modified * @param object the String to convert, must not be null * @param chrono the chronology to use * @return the millisecond duration * @throws ClassCastException if the object is invalid */ public void setInto(ReadWritablePeriod period, Object object, Chronology chrono) { String str = (String) object; PeriodFormatter parser = ISOPeriodFormat.standard(); period.clear(); int pos = parser.parseInto(period, str, 0); if (pos < str.length()) { if (pos < 0) { // Parse again to get a better exception thrown. parser.withParseType(period.getPeriodType()).parseMutablePeriod(str); } throw new IllegalArgumentException("Invalid format: \"" + str + '"'); } }
void setFieldValue(ReadWritablePeriod period, int field, int value) { switch (field) { default: break; case YEARS: period.setYears(value); break; case MONTHS: period.setMonths(value); break; case WEEKS: period.setWeeks(value); break; case DAYS: period.setDays(value); break; case HOURS: period.setHours(value); break; case MINUTES: period.setMinutes(value); break; case SECONDS: period.setSeconds(value); break; case MILLIS: period.setMillis(value); break; } }
public int parseInto( ReadWritablePeriod period, String periodStr, int position, Locale locale) { if (periodStr.regionMatches(true, position, iText, 0, iText.length())) { return position + iText.length(); } return ~position; }
public int parseInto( ReadWritablePeriod period, String periodStr, int position, Locale locale) { PeriodParser[] parsers = iParsers; if (parsers == null) { throw new UnsupportedOperationException(); } int len = parsers.length; for (int i=0; i<len && position >= 0; i++) { position = parsers[i].parseInto(period, periodStr, position, locale); } return position; }
@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 int parseInto(ReadWritablePeriod period, String periodStr, int position, Locale locale) { Matcher match = PERIOD_PATTERN.matcher(periodStr.substring(position)); if (match.matches()) { int num = 0; String numstr = match.group(1); if (numstr != null && !numstr.equals("")) { num = Integer.parseInt(numstr); } String unit = match.group(2); if (unit == null || unit.equals("") || unit.startsWith("d") || unit.startsWith("D")) { period.setDays(num); } else if (unit.startsWith("w") || unit.startsWith("W")) { period.setWeeks(num); } else if (unit.startsWith("mo") || unit.startsWith("MO") || unit.equals("M") || unit.equals("m")) { period.setMonths(num); } else if (unit.startsWith("y") || unit.startsWith("Y")) { period.setYears(num); } else if (unit.startsWith("h") || unit.startsWith("H")) { period.setHours(num); } else if (unit.startsWith("mi") || unit.startsWith("MI") || unit.equals("'")) { period.setMinutes(num); } else if (unit.startsWith("s") || unit.startsWith("S")) { period.setSeconds(num); } return periodStr.length(); } // unrecognized period/units return 0; }
@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); }
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(); }
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop(); if (!(o instanceof String)) { throw new WarpScriptException(getName() + " expects an ISO8601 duration (a string) on top of the stack. See http://en.wikipedia.org/wiki/ISO_8601#Durations"); } ReadWritablePeriod period = new MutablePeriod(); ISOPeriodFormat.standard().getParser().parseInto(period, o.toString(), 0, Locale.US); Period p = period.toPeriod(); if (p.getMonths() != 0 || p.getYears() != 0) { throw new WarpScriptException(getName() + " doesn't support ambiguous durations containing years or months, please convert those to days."); } Duration duration = p.toDurationFrom(new Instant()); stack.push(duration.getMillis() * Constants.TIME_UNITS_PER_MS); return stack; }
public int parseInto( ReadWritablePeriod period, String periodStr, int position, Locale locale) { int oldPos = position; position = iBeforeParser.parseInto(period, periodStr, position, locale); if (position < 0) { return position; } boolean found = false; if (position > oldPos) { // Consume this separator. String[] parsedForms = iParsedForms; int length = parsedForms.length; for (int i=0; i < length; i++) { String parsedForm = parsedForms[i]; if ((parsedForm == null || parsedForm.length() == 0) || periodStr.regionMatches (true, position, parsedForm, 0, parsedForm.length())) { position += (parsedForm == null ? 0 : parsedForm.length()); found = true; break; } } } oldPos = position; position = iAfterParser.parseInto(period, periodStr, position, locale); if (position < 0) { return position; } if (found && position == oldPos) { // Separator should not have been supplied. return ~oldPos; } if (position > oldPos && !found && !iUseBefore) { // Separator was required. return ~oldPos; } return position; }
@Test public void creates_instance_of_ReadWritablePeriod() throws ParseException { ReadWritablePeriod period = fixture.create(ReadWritablePeriod.class); assertThat(period, notNullValue()); assertThat(period, Matchers.<ReadablePeriod>is(new MutablePeriod(1,0,0,0,0,0,0,0))); // 1Yr }
public int parseInto( ReadWritablePeriod period, String periodStr, int position, Locale locale) { int oldPos = position; position = iBeforeParser.parseInto(period, periodStr, position, locale); if (position < 0) { return position; } boolean found = false; int parsedFormLength = -1; if (position > oldPos) { // Consume this separator. String[] parsedForms = iParsedForms; int length = parsedForms.length; for (int i=0; i < length; i++) { String parsedForm = parsedForms[i]; if ((parsedForm == null || parsedForm.length() == 0) || periodStr.regionMatches (true, position, parsedForm, 0, parsedForm.length())) { parsedFormLength = (parsedForm == null ? 0 : parsedForm.length()); position += parsedFormLength; found = true; break; } } } oldPos = position; position = iAfterParser.parseInto(period, periodStr, position, locale); if (position < 0) { return position; } if (found && position == oldPos && parsedFormLength > 0) { // Separator should not have been supplied. return ~oldPos; } if (position > oldPos && !found && !iUseBefore) { // Separator was required. return ~oldPos; } return position; }