/** * Constructs a time interval converting or copying from another object * that describes an interval. * * @param interval the time interval to copy * @param chrono the chronology to use, null means let converter decide * @throws IllegalArgumentException if the interval is invalid */ protected BaseInterval(Object interval, Chronology chrono) { super(); IntervalConverter converter = ConverterManager.getInstance().getIntervalConverter(interval); if (converter.isReadableInterval(interval, chrono)) { ReadableInterval input = (ReadableInterval) interval; iChronology = (chrono != null ? chrono : input.getChronology()); iStartMillis = input.getStartMillis(); iEndMillis = input.getEndMillis(); } else if (this instanceof ReadWritableInterval) { converter.setInto((ReadWritableInterval) this, interval, chrono); } else { MutableInterval mi = new MutableInterval(); converter.setInto(mi, interval, chrono); iChronology = mi.getChronology(); iStartMillis = mi.getStartMillis(); iEndMillis = mi.getEndMillis(); } checkInterval(iStartMillis, iEndMillis); }
/** * Extracts interval endpoint values from an object of this converter's * type, and sets them into the given ReadWritableInterval. * * @param writableInterval interval to get modified, not null * @param object the object to convert, must not be null * @param chrono the chronology to use, may be null * @throws ClassCastException if the object is invalid */ public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) { ReadableInterval input = (ReadableInterval) object; writableInterval.setInterval(input); if (chrono != null) { writableInterval.setChronology(chrono); } else { writableInterval.setChronology(input.getChronology()); } }
@Override public Object create(Object request, SpecimenContext context) { if (!(request.equals(ReadableInterval.class) || request.equals(ReadWritableInterval.class))) return new NoSpecimen(); Interval interval = (Interval) context.resolve(Interval.class); return new MutableInterval(interval); }
@Test public void creates_instance_of_ReadWritableInterval() throws ParseException { ReadWritableInterval interval = fixture.create(ReadWritableInterval.class); assertThat(interval, notNullValue()); assertThat(interval.getStart().toDate(), is(date)); assertThat(interval.getEnd().toDate(), is(secondDate)); }
/** * Sets the value of the mutable interval from the string. * * @param writableInterval the interval to set * @param object the String to convert, must not be null * @param chrono the chronology to use, may be null */ public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) { String str = (String) object; int separator = str.indexOf('/'); if (separator < 0) { throw new IllegalArgumentException("Format requires a '/' separator: " + str); } String leftStr = str.substring(0, separator); if (leftStr.length() <= 0) { throw new IllegalArgumentException("Format invalid: " + str); } String rightStr = str.substring(separator + 1); if (rightStr.length() <= 0) { throw new IllegalArgumentException("Format invalid: " + str); } DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser(); dateTimeParser = dateTimeParser.withChronology(chrono); PeriodFormatter periodParser = ISOPeriodFormat.standard(); long startInstant = 0, endInstant = 0; Period period = null; Chronology parsedChrono = null; // before slash char c = leftStr.charAt(0); if (c == 'P' || c == 'p') { period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr); } else { DateTime start = dateTimeParser.parseDateTime(leftStr); startInstant = start.getMillis(); parsedChrono = start.getChronology(); } // after slash c = rightStr.charAt(0); if (c == 'P' || c == 'p') { if (period != null) { throw new IllegalArgumentException("Interval composed of two durations: " + str); } period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr); chrono = (chrono != null ? chrono : parsedChrono); endInstant = chrono.add(period, startInstant, 1); } else { DateTime end = dateTimeParser.parseDateTime(rightStr); endInstant = end.getMillis(); parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology()); chrono = (chrono != null ? chrono : parsedChrono); if (period != null) { startInstant = chrono.add(period, endInstant, -1); } } writableInterval.setInterval(startInstant, endInstant); writableInterval.setChronology(chrono); }
/** * Extracts interval endpoint values from an object of this converter's * type, and sets them into the given ReadWritableInterval. * * @param writableInterval interval to get modified, not null * @param object the object to convert, must not be null * @param chrono the chronology to use, may be null * @throws ClassCastException if the object is invalid */ void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono);
/** * Extracts interval endpoint values from an object of this converter's * type, and sets them into the given ReadWritableInterval. * * @param writableInterval interval to get modified, not null * @param object the object to convert, which is null * @param chrono the chronology to use, may be null * @throws NullPointerException if the interval is null */ public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) { writableInterval.setChronology(chrono); long now = DateTimeUtils.currentTimeMillis(); writableInterval.setInterval(now, now); }