fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject [fineract-cn-lang] 35/49: Moved DateRange here from accounting.
Date Mon, 22 Jan 2018 15:01:30 GMT
This is an automated email from the ASF dual-hosted git repository.

myrle pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-lang.git

commit 30c9b680519b3c17ba36016b22e34e0b9d52020b
Author: Myrle Krantz <myrle@apache.org>
AuthorDate: Fri Jul 14 08:59:54 2017 +0200

    Moved DateRange here from accounting.
---
 src/main/java/io/mifos/core/lang/DateRange.java    | 84 ++++++++++++++++++++++
 .../java/io/mifos/core/lang/DateRangeTest.java     | 61 ++++++++++++++++
 2 files changed, 145 insertions(+)

diff --git a/src/main/java/io/mifos/core/lang/DateRange.java b/src/main/java/io/mifos/core/lang/DateRange.java
new file mode 100644
index 0000000..5a54687
--- /dev/null
+++ b/src/main/java/io/mifos/core/lang/DateRange.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2017 The Mifos Initiative
+ *
+ * Licensed 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 io.mifos.core.lang;
+
+import org.springframework.util.Assert;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import java.time.Clock;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoUnit;
+import java.util.stream.Stream;
+
+/**
+ * A range of dates specified by a start and an end.  Inclusive of both.
+ *
+ * @author Myrle Krantz
+ */
+@SuppressWarnings("WeakerAccess")
+public class DateRange {
+  private final @Nonnull LocalDate start;
+  private final @Nonnull LocalDate end;
+
+  public DateRange(final @Nonnull LocalDate start, final @Nonnull LocalDate end) {
+    Assert.notNull(start, "Range start must be given.");
+    Assert.notNull(start, "Range end must be given.");
+    this.start = start;
+    this.end = end;
+  }
+
+  public Stream<LocalDate> stream() {
+    return Stream.iterate(start, (current) -> current.plusDays(1))
+        .limit(ChronoUnit.DAYS.between(start, end) + 1);
+  }
+
+  public LocalDateTime getStartDateTime() {
+    return start.atStartOfDay();
+  }
+
+  public LocalDateTime getEndDateTime() {
+    return end.plusDays(1).atStartOfDay();
+  }
+
+  @Override
+  public String toString() {
+    return DateConverter.toIsoString(start) + ".." + DateConverter.toIsoString(end);
+  }
+
+  @Nonnull
+  public static DateRange fromIsoString(@Nullable final String isoDateRangeString) {
+    if (isoDateRangeString == null) {
+      final LocalDate today = LocalDate.now(Clock.systemUTC());
+      return new DateRange(today, today);
+    } else {
+      final String[] dates = isoDateRangeString.split("\\.\\.");
+      if (dates.length != 2)
+        throw ServiceException.badRequest("Date range should consist of exactly two dates.",
+            isoDateRangeString);
+
+      try {
+        return new DateRange(DateConverter.dateFromIsoString(dates[0]), DateConverter.dateFromIsoString(dates[1]));
+      }
+      catch(final DateTimeParseException e){
+        throw ServiceException.badRequest("Date {0} must use ISO format",
+            isoDateRangeString);
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/src/test/java/io/mifos/core/lang/DateRangeTest.java b/src/test/java/io/mifos/core/lang/DateRangeTest.java
new file mode 100644
index 0000000..39bbe4b
--- /dev/null
+++ b/src/test/java/io/mifos/core/lang/DateRangeTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2017 The Mifos Initiative
+ *
+ * Licensed 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 io.mifos.core.lang;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.time.LocalDate;
+
+/**
+ * @author Myrle Krantz
+ */
+public class DateRangeTest {
+
+  @Test
+  public void dateRangeFromIsoString() {
+    final String startString = "2017-07-13Z";
+    final String endString = "2017-07-16Z";
+    final LocalDate start = DateConverter.dateFromIsoString(startString);
+    final LocalDate end = DateConverter.dateFromIsoString(endString);
+    final String dateRangeString = startString + ".." + endString;
+
+    final DateRange dateRange = DateRange.fromIsoString(dateRangeString);
+    Assert.assertEquals(dateRangeString, dateRange.toString());
+    Assert.assertEquals(start.atStartOfDay(), dateRange.getStartDateTime());
+    Assert.assertEquals(end.plusDays(1).atStartOfDay(), dateRange.getEndDateTime());
+    Assert.assertEquals(4, dateRange.stream().count());
+
+    final DateRange dateRangeToday = DateRange.fromIsoString(null);
+    Assert.assertEquals(1, dateRangeToday.stream().count());
+
+    try {
+      final DateRange dateRangeHalf = DateRange.fromIsoString(startString);
+      Assert.fail("Invalid date range format should throw exception.");
+    }
+    catch (final ServiceException ignore) {
+
+    }
+
+    try {
+      final DateRange dateRangeHalf = DateRange.fromIsoString("notanisostringZ..anothernonisostringZ");
+      Assert.fail("Invalid date range format should throw exception.");
+    }
+    catch (final ServiceException ignore) {
+
+    }
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
myrle@apache.org.

Mime
View raw message