Author: btellier Date: Mon Jun 29 08:27:00 2015 New Revision: 1688119 URL: http://svn.apache.org/r1688119 Log: MAILBOX-235 Round date based on James specified time units
Added: james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/ james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/ james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java Added: james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java?rev=1688119&view=auto ============================================================================== --- james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java (added) +++ james/mailbox/trunk/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java Mon Jun 29 08:27:00 2015 @@ -0,0 +1,63 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.elasticsearch.query; + +import org.apache.james.mailbox.model.SearchQuery; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; +import java.util.Date; + +public class DateResolutionFormater { + + public static ZonedDateTime computeUpperDate(ZonedDateTime date, SearchQuery.DateResolution resolution) { + return date.truncatedTo(convertDateResolutionField(resolution)).plus(1,convertDateResolutionField(resolution)); + } + + public static ZonedDateTime computeLowerDate(ZonedDateTime date, SearchQuery.DateResolution resolution) { + return date.truncatedTo(convertDateResolutionField(resolution)); + } + + private static TemporalUnit convertDateResolutionField(SearchQuery.DateResolution resolution) { + switch(resolution) { + case Year: + return ChronoUnit.YEARS; + case Month: + return ChronoUnit.MONTHS; + case Day: + return ChronoUnit.DAYS; + case Hour: + return ChronoUnit.HOURS; + case Minute: + return ChronoUnit.MINUTES; + case Second: + return ChronoUnit.SECONDS; + default: + throw new RuntimeException("Unknown Date resolution used"); + } + } + + public static ZonedDateTime convertDateToZonedDateTime(Date date) { + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); + } +} \ No newline at end of file Added: james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java?rev=1688119&view=auto ============================================================================== --- james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java (added) +++ james/mailbox/trunk/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java Mon Jun 29 08:27:00 2015 @@ -0,0 +1,72 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.elasticsearch.query; + +import static org.assertj.core.api.Assertions.assertThat; +import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME; + +import org.apache.james.mailbox.model.SearchQuery; +import org.junit.Test; + +import java.text.ParseException; +import java.time.ZonedDateTime; + + +public class DateResolutionFormaterTest { + + private final String dateString = "2014-01-02T15:15:15Z"; + + @Test + public void calculateUpperDateShouldReturnDateUpToTheNextMinuteUsingMinuteUnit() throws ParseException { + assertThat( + ISO_OFFSET_DATE_TIME.format( + DateResolutionFormater.computeUpperDate(ZonedDateTime.parse(dateString, ISO_OFFSET_DATE_TIME), SearchQuery.DateResolution.Minute) + ) + ).isEqualTo("2014-01-02T15:16:00Z"); + } + + @Test + public void calculateUpperDateShouldReturnDateUpToTheNextDayUsingDayUnit() throws ParseException { + assertThat( + ISO_OFFSET_DATE_TIME.format( + DateResolutionFormater.computeUpperDate(ZonedDateTime.parse(dateString, ISO_OFFSET_DATE_TIME), SearchQuery.DateResolution.Day) + ) + ).isEqualTo("2014-01-03T00:00:00Z"); + } + + @Test + public void calculateLowerDateShouldReturnDateUpToThePreviousMinuteUsingMinuteUnit() throws ParseException { + assertThat( + ISO_OFFSET_DATE_TIME.format( + DateResolutionFormater.computeLowerDate(ZonedDateTime.parse(dateString, ISO_OFFSET_DATE_TIME), SearchQuery.DateResolution.Minute) + ) + ).isEqualTo("2014-01-02T15:15:00Z"); + } + + @Test + public void calculateLowerDateShouldReturnDateUpToThePreviousDayUsingDayUnit() throws ParseException { + assertThat( + ISO_OFFSET_DATE_TIME.format( + DateResolutionFormater.computeLowerDate(ZonedDateTime.parse(dateString, ISO_OFFSET_DATE_TIME), SearchQuery.DateResolution.Day) + ) + ).isEqualTo("2014-01-02T00:00:00Z"); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org