private static Date end(Object dateOrDateRange) { if (dateOrDateRange instanceof DateRange) { return ((DateRange) dateOrDateRange).getMaxValue(); } else { return (Date) dateOrDateRange; } }
/** * Formats the specified object either as a single time, if it's a Date, or as a continuous * interval, if it's a DateRange (and will throw an {@link IllegalArgumentException} otherwise) * * @param date */ public String format(Object date) { if (date instanceof Date) { return format((Date) date); } else if (date instanceof DateRange) { DateRange range = (DateRange) date; StringBuilder sb = new StringBuilder(); format(range.getMinValue(), sb); sb.append("/"); format(range.getMaxValue(), sb); sb.append("/PT1S"); return sb.toString(); } else { throw new IllegalArgumentException( "Date argument should be either a Date or a " + "DateRange, however this one is neither: " + date); } }
if (pos.getIndex() == computedValue.length()) { DateRange range = f.expand(time); if (range.getMinValue().equals(range.getMaxValue())) { return range.getMinValue(); } else {
public static void assertRangeEnds(DateRange range, Date expectedEnd) { if (range.getMaxValue() == null) fail("Expected valid end date in range " + range); assertEquals("Range " + range + " should have end", expectedEnd, range.getMaxValue()); } }
@Test public void testFutureTimeRange() throws Exception { AcceptableRange range = AcceptableRange.getAcceptableRange("P0D/P1D", Date.class); assertEquals(0l, range.getBefore()); assertEquals(DAY_IN_MS, range.getAfter()); Date value = new Date(); DateRange searchRange = (DateRange) range.getSearchRange(value); assertEquals(0l, value.getTime() - searchRange.getMinValue().getTime()); assertEquals(DAY_IN_MS, searchRange.getMaxValue().getTime() - value.getTime()); } }
@Test public void testPastTimeRange() throws Exception { AcceptableRange range = AcceptableRange.getAcceptableRange("P1D/P0D", Date.class); assertEquals(DAY_IN_MS, range.getBefore()); assertEquals(0l, range.getAfter()); Date value = new Date(); DateRange searchRange = (DateRange) range.getSearchRange(value); assertEquals(DAY_IN_MS, value.getTime() - searchRange.getMinValue().getTime()); assertEquals(0l, searchRange.getMaxValue().getTime() - value.getTime()); }
@Test public void testSymmetricTimeRange() throws Exception { AcceptableRange range = AcceptableRange.getAcceptableRange("P1D", Date.class); assertEquals(DAY_IN_MS, range.getBefore()); assertEquals(DAY_IN_MS, range.getAfter()); Date value = new Date(); DateRange searchRange = (DateRange) range.getSearchRange(value); assertEquals(DAY_IN_MS, value.getTime() - searchRange.getMinValue().getTime()); assertEquals(DAY_IN_MS, searchRange.getMaxValue().getTime() - value.getTime()); }
public static void assertRangeLength(DateRange range, long expectedLength) { if (range.getMinValue() == null) fail("Expected finite range, saw: " + range); if (range.getMaxValue() == null) fail("Expected finite range, saw: " + range); long min = range.getMinValue().getTime(); long max = range.getMaxValue().getTime(); assertEquals("Range " + range + " should have length", expectedLength, max - min); }
public void testContinuousInterval() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList((Collection) timeKvpParser.parse(CONTINUOUS_PERIOD)); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertEquals(format.parse("2007-01-01T12Z"), range.getMinValue()); Date end = format.parse("2007-01-31T13Z"); end.setTime(end.getTime() - 1); assertEquals(end, range.getMaxValue()); }
public void testContinuousIntervalDuration() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList((Collection) timeKvpParser.parse(CONTINUOUS_PERIOD_TIME_DURATION)); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertEquals(format.parse("2007-01-01T12Z"), range.getMinValue()); Date end = format.parse("2007-01-02T13Z"); assertEquals(end, range.getMaxValue()); }
public void testMixedValues() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList( (Collection) timeKvpParser.parse(CONTINUOUS_PERIOD + ",2007-02-01T12Z")); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertEquals(format.parse("2007-01-01T12Z"), range.getMinValue()); Date end = format.parse("2007-01-31T13Z"); end.setTime(end.getTime() - 1); assertEquals(end, range.getMaxValue()); assertRange( (DateRange) l.get(1), format.parse("2007-02-01T12Z"), format.parse("2007-02-01T13Z")); }
range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue());
@Test public void testMixedTimeExtraction() throws IOException, ParseException { MockDimensionReader reader = new MockDimensionReader(); reader.metadata.put(GridCoverage2DReader.HAS_TIME_DOMAIN, "true"); reader.metadata.put( GridCoverage2DReader.TIME_DOMAIN, "2016-02-23T03:00:00.000Z/2016-02-23T03:00:00.000Z/PT1S,2016-02-23T06:00:00.000Z,2016-02-23T09:00:00.000Z/2016-02-23T12:00:00.000Z/PT1S"); ReaderDimensionsAccessor accessor = new ReaderDimensionsAccessor(reader); TreeSet<Object> domain = accessor.getTimeDomain(); assertEquals(3, domain.size()); Iterator<Object> it = domain.iterator(); Date firstEntry = (Date) it.next(); assertEquals(accessor.getTimeFormat().parse("2016-02-23T03:00:00.000Z"), firstEntry); Date secondEntry = (Date) it.next(); assertEquals(accessor.getTimeFormat().parse("2016-02-23T06:00:00.000Z"), secondEntry); DateRange thirdEntry = (DateRange) it.next(); assertEquals( accessor.getTimeFormat().parse("2016-02-23T09:00:00.000Z"), thirdEntry.getMinValue()); assertEquals( accessor.getTimeFormat().parse("2016-02-23T12:00:00.000Z"), thirdEntry.getMaxValue()); }
private static void assertInstant(Date expected, Object object) { if (object instanceof DateRange) { assertEquals(object + " Should start at", expected, ((DateRange) object).getMinValue()); assertEquals(object + " Should end at", expected, ((DateRange) object).getMaxValue()); } else if (object instanceof Date) { assertEquals(expected, object); } else { fail("Should have a DateRange: " + object); } }
@Override public int compare(DateRange firstDateRange, DateRange secondDateRange) { Utilities.ensureNonNull("firstDateRange", firstDateRange); Utilities.ensureNonNull("secondDateRange", secondDateRange); final long beginFirst = firstDateRange.getMinValue().getTime(); final long endFirst = firstDateRange.getMaxValue().getTime(); final long beginSecond = secondDateRange.getMinValue().getTime(); final long endSecond = secondDateRange.getMaxValue().getTime(); return NumberRangeComparator.doubleCompare(beginFirst, endFirst, beginSecond, endSecond); } }
private String buildTemporalList(SortedSet<? extends DateRange> temporalElements) { Iterator<DateRange> iterator = (Iterator<DateRange>) temporalElements.iterator(); // LinkedHashSet<String> result = new LinkedHashSet<String>(); final StringBuilder buff = new StringBuilder(""); while (iterator.hasNext()) { DateRange range = iterator.next(); buff.append( ConvertersHack.convert(range.getMinValue(), String.class) + "/" + ConvertersHack.convert(range.getMaxValue(), String.class)); if (iterator.hasNext()) { buff.append(","); } } return buff.toString(); }
@Override public int compare(DateRange firstDateRange, DateRange secondDateRange) { Utilities.ensureNonNull("firstDateRange", firstDateRange); Utilities.ensureNonNull("secondDateRange", secondDateRange); final long beginFirst = firstDateRange.getMinValue().getTime(); final long endFirst = firstDateRange.getMaxValue().getTime(); final long beginSecond = secondDateRange.getMinValue().getTime(); final long endSecond = secondDateRange.getMaxValue().getTime(); return NumberRangeComparator.doubleCompare( beginFirst, endFirst, beginSecond, endSecond); } }
/** * Format a DateRange into ISO8601 interval strings * * @param range * @return */ private String formatRange(DateRange range) { final StringBuilder builder = new StringBuilder(); final String begin = ConvertersHack.convert(range.getMinValue(), String.class); final String end = ConvertersHack.convert(range.getMaxValue(), String.class); builder.append(begin + "/" + end + "/PT1S"); return builder.toString(); }
/** * Merge 2 ranges together. In order to speed up the computations, this method does the * assumption that the 2 date ranges are already sorted (first < second) and they intersect * together. * * @param firstDateRange * @param secondDateRange * @return */ private static DateRange merge(DateRange firstDateRange, DateRange secondDateRange) { Utilities.ensureNonNull("firstDateRange", firstDateRange); Utilities.ensureNonNull("secondDateRange", secondDateRange); final long beginFirst = firstDateRange.getMinValue().getTime(); final long endFirst = firstDateRange.getMaxValue().getTime(); final long beginSecond = secondDateRange.getMinValue().getTime(); final long endSecond = secondDateRange.getMaxValue().getTime(); final long max = Math.max(endFirst, endSecond); final long min = Math.min(beginFirst, beginSecond); final Date beginDate = new Date(min); final Date endDate = new Date(max); return new DateRange(beginDate, endDate); } }
return ConvertersHack.convert(overall.getMaxValue(), String.class); } else if (name.endsWith("minimum")) { return ConvertersHack.convert(overall.getMinValue(), String.class);