/** * 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); }
public void testGetDurationConverter() { DurationConverter c = ConverterManager.getInstance().getDurationConverter(new Long(0L)); assertEquals(Long.class, c.getSupportedType()); c = ConverterManager.getInstance().getDurationConverter(new Duration(123L)); assertEquals(ReadableDuration.class, c.getSupportedType()); c = ConverterManager.getInstance().getDurationConverter(new Interval(0L, 1000L)); assertEquals(ReadableInterval.class, c.getSupportedType()); c = ConverterManager.getInstance().getDurationConverter(""); assertEquals(String.class, c.getSupportedType()); c = ConverterManager.getInstance().getDurationConverter(null); assertEquals(null, c.getSupportedType()); try { ConverterManager.getInstance().getDurationConverter(Boolean.TRUE); fail(); } catch (IllegalArgumentException ex) {} }
public void testGetPeriodConverter() { PeriodConverter c = ConverterManager.getInstance().getPeriodConverter(new Period(1, 2, 3, 4, 5, 6, 7, 8)); assertEquals(ReadablePeriod.class, c.getSupportedType()); c = ConverterManager.getInstance().getPeriodConverter(new Duration(123L)); assertEquals(ReadableDuration.class, c.getSupportedType()); c = ConverterManager.getInstance().getPeriodConverter(new Interval(0L, 1000L)); assertEquals(ReadableInterval.class, c.getSupportedType()); c = ConverterManager.getInstance().getPeriodConverter(""); assertEquals(String.class, c.getSupportedType()); c = ConverterManager.getInstance().getPeriodConverter(null); assertEquals(null, c.getSupportedType()); try { ConverterManager.getInstance().getPeriodConverter(Boolean.TRUE); fail(); } catch (IllegalArgumentException ex) {} }
/** * Does a time interval contain a specified time interval. * * @param interval the interval to check * @param intervalCompareTo the interval to compare to * @return true if this time ' intervalCompareTo' contains 'intervalCompareTo' */ public static boolean contains(ReadableInterval interval, ReadableInterval intervalCompareTo) { if (intervalCompareTo == null) { return DateUtils.containsNow(intervalCompareTo); } long otherStart = intervalCompareTo.getStartMillis(); long otherEnd = intervalCompareTo.getEndMillis(); long thisStart = interval.getStartMillis(); long thisEnd = interval.getEndMillis(); return (thisStart <= otherStart && otherStart <= thisEnd && otherEnd <= thisEnd); }
/** * Does a time interval intersect another time interval. * <p> * * @param interval1 the interval1 * @param interval2 the interval2 * @return true if the time intervals intersect */ public static boolean intersects(ReadableInterval interval1, ReadableInterval interval2) { if (interval1 == null) { return false; } if (interval2 == null) { return false; } long thisStart = interval1.getStartMillis(); long thisEnd = interval1.getEndMillis(); long otherStart = interval2.getStartMillis(); long otherEnd = interval2.getEndMillis(); return (thisStart <= otherEnd && otherStart <= thisEnd); }
public static Interval intersect(ReadableInterval interval1, ReadableInterval interval2) { if (DateUtils.intersects(interval1, interval2) == false) { return null; } long start = Math.max(interval1.getStartMillis(), interval2.getStartMillis()); long end = Math.min(interval1.getEndMillis(), interval2.getEndMillis()); return new Interval(start, end); }
/** * Is this time interval entirely after the specified interval. * <p> * Intervals are inclusive of the start instant and exclusive of the end. * Only the end time of the specified interval is used in the comparison. * * @param interval the interval to compare to, null means now * @return true if this time interval is after the interval specified */ public boolean isAfter(ReadableInterval interval) { long endMillis; if (interval == null) { endMillis = DateTimeUtils.currentTimeMillis(); } else { endMillis = interval.getEndMillis(); } return (getStartMillis() >= endMillis); }
/** * Compares this object with the specified object for equality based * on start and end millis plus the chronology. * All ReadableInterval instances are accepted. * <p> * To compare the duration of two time intervals, use {@link #toDuration()} * to get the durations and compare those. * * @param readableInterval a readable interval to check against * @return true if the start and end millis are equal */ public boolean equals(Object readableInterval) { if (this == readableInterval) { return true; } if (readableInterval instanceof ReadableInterval == false) { return false; } ReadableInterval other = (ReadableInterval) readableInterval; return getStartMillis() == other.getStartMillis() && getEndMillis() == other.getEndMillis() && FieldUtils.equals(getChronology(), other.getChronology()); }
/** * 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 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_ReadableInterval() throws ParseException { ReadableInterval interval = fixture.create(ReadableInterval.class); assertThat(interval, notNullValue()); assertThat(interval.getStart().toDate(), is(date)); assertThat(interval.getEnd().toDate(), is(secondDate)); }
/** * Compares this object with the specified object for equality based * on start and end millis plus the chronology. * All ReadableInterval instances are accepted. * <p> * To compare the duration of two time intervals, use {@link #toDuration()} * to get the durations and compare those. * * @param readableInterval a readable interval to check against * @return true if the intervals are equal comparing the start millis, * end millis and chronology */ public boolean equals(Object readableInterval) { if (this == readableInterval) { return true; } if (readableInterval instanceof ReadableInterval == false) { return false; } ReadableInterval other = (ReadableInterval) readableInterval; return getStartMillis() == other.getStartMillis() && getEndMillis() == other.getEndMillis() && FieldUtils.equals(getChronology(), other.getChronology()); }
/** * Returns ReadableInterval.class. */ public Class<?> getSupportedType() { return ReadableInterval.class; }
public void testSupportedType() throws Exception { assertEquals(ReadableInterval.class, ReadableIntervalConverter.INSTANCE.getSupportedType()); }