private static Date beginning(Object dateOrDateRange) { if (dateOrDateRange instanceof DateRange) { return ((DateRange) dateOrDateRange).getMinValue(); } else { return (Date) dateOrDateRange; } }
public int compare(Object o1, Object o2) { final boolean o1Date = o1 instanceof Date; final boolean o2Date = o2 instanceof Date; if (o1 == o2) { return 0; } // o1 date if (o1Date) { final Date dateLeft = (Date) o1; if (o2Date) { // o2 date return dateLeft.compareTo((Date) o2); } // o2 daterange return dateLeft.compareTo(((DateRange) o2).getMinValue()); } // o1 date range final DateRange left = (DateRange) o1; if (o2Date) { // o2 date return left.getMinValue().compareTo(((Date) o2)); } // o2 daterange return left.getMinValue().compareTo(((DateRange) o2).getMinValue()); } });
@Override public int compare(Object o1, Object o2) { // the domain can be a mix of dates and ranges if (o1 instanceof Date) { if (o1 instanceof DateRange) { return ((Date) o1).compareTo(((DateRange) o2).getMinValue()); } else { return ((Date) o1).compareTo((Date) o2); } } else if (o1 instanceof DateRange) { if (o2 instanceof Date) { return ((DateRange) o1).getMinValue().compareTo((Date) o2); } else { return ((DateRange) o1) .getMinValue() .compareTo(((DateRange) o2).getMinValue()); } } throw new IllegalArgumentException( "Unxpected object type found, was expecting date or date range but found " + o1 + " and " + o2); } };
if (pos.getIndex() == computedValue.length()) { DateRange range = f.expand(time); if (range.getMinValue().equals(range.getMaxValue())) { return range.getMinValue(); } else { return range;
/** * 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); } }
public static void assertRangeStarts(DateRange range, Date expectedStart) { if (range.getMinValue() == null) fail("Expected valid start date in range " + range); assertEquals("Range " + range + " should have start", expectedStart, range.getMinValue()); }
@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")); }
public void testNegativeYearCompliance() throws Exception { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); // base assertion - 0001 is year 1 DateRange date = (DateRange) ((List) timeKvpParser.parse("01-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(1, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.AD, cal.get(Calendar.ERA)); date = (DateRange) ((List) timeKvpParser.parse("00-06-01")).get(0); cal.setTime(date.getMinValue()); // calendar calls it year 1, ISO spec means it's year 0 // but we're just parsing here... assertEquals(1, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); // so, the next year should be 2 date = (DateRange) ((List) timeKvpParser.parse("-01-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(2, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); // now, big negative year compliance (see the spec, appendix D 2.2, pp 57-58) date = (DateRange) ((List) timeKvpParser.parse("-18000-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(18001, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); }
assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); assertTrue(l.get(0) instanceof DateRange); 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); } }
@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(); }