You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/11/10 11:10:34 UTC

[isis] branch master updated: ISIS-2891: split CalendarService into interface and impl.

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 254de7f  ISIS-2891: split CalendarService into interface and impl.
254de7f is described below

commit 254de7f2a178a4d5d2e04ca1f3119c75fceed6aa
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Nov 10 12:10:29 2021 +0100

    ISIS-2891: split CalendarService into interface and impl.
    
    - also properly register with Spring
---
 .../base/applib/services/BaseServicesModule.java   |  8 ++-
 .../applib/services/calendar/CalendarService.java  | 57 +++-----------------
 ...darService.java => CalendarServiceDefault.java} | 63 +++++++---------------
 .../CalendarServiceTest_beginningOfMonth.java      |  2 +-
 .../CalendarServiceTest_beginningOfQuarter.java    |  2 +-
 5 files changed, 33 insertions(+), 99 deletions(-)

diff --git a/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/BaseServicesModule.java b/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/BaseServicesModule.java
index e0804ac..86ff7ea 100644
--- a/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/BaseServicesModule.java
+++ b/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/BaseServicesModule.java
@@ -18,13 +18,17 @@
  */
 package org.apache.isis.subdomains.base.applib.services;
 
-import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+import org.apache.isis.subdomains.base.applib.services.calendar.CalendarServiceDefault;
 
 /**
  * @since 2.0 {@index}
  */
 @Configuration
-@ComponentScan
+@Import({
+    CalendarServiceDefault.class
+})
 public class BaseServicesModule {
 }
diff --git a/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java b/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java
index ed0a5ab..fc0fe75 100644
--- a/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java
+++ b/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java
@@ -1,55 +1,16 @@
-/*
- *  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.isis.subdomains.base.applib.services.calendar;
 
-import lombok.RequiredArgsConstructor;
-import org.apache.isis.applib.annotation.PriorityPrecedence;
-import org.apache.isis.applib.services.clock.ClockService;
-import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.stereotype.Service;
-
-import javax.annotation.Priority;
-import javax.inject.Inject;
-import javax.inject.Named;
 import java.time.LocalDate;
-import java.time.ZoneId;
 
 /**
  * @since 2.0 {@index}
  */
-@Service
-@Named("isis.sub.base.CalendarService")
-@Priority(PriorityPrecedence.MIDPOINT)
-@Qualifier("Default")
-@RequiredArgsConstructor(onConstructor_ = {@Inject})
-public class CalendarService {
-
-    private static final int MONTHS_IN_QUARTER = 3;
-
-    private final ClockService clockService;
+public interface CalendarService {
 
     /**
      * Returns the date corresponding to the beginning of the current month.
      */
-    public LocalDate beginningOfMonth() {
-        return beginningOfMonth(nowAsLocalDate());
-    }
+    LocalDate beginningOfMonth();
 
     static LocalDate beginningOfMonth(final LocalDate date) {
         final long dayOfMonth = date.getDayOfMonth();
@@ -62,20 +23,17 @@ public class CalendarService {
      * @see #beginningOfQuarter(LocalDate)
      * @see #beginningOfNextQuarter()
      */
-    public LocalDate beginningOfQuarter() {
-        return beginningOfQuarter(nowAsLocalDate());
-    }
+    LocalDate beginningOfQuarter();
 
     /**
      * Returns the date corresponding to the beginning of the quarter following this one.
      *
      * @see #beginningOfQuarter()
      */
-    public LocalDate beginningOfNextQuarter() {
-        return beginningOfQuarter(nowAsLocalDate().plusMonths(3));
-    }
+    LocalDate beginningOfNextQuarter();
 
     static LocalDate beginningOfQuarter(final LocalDate date) {
+        final int MONTHS_IN_QUARTER = 3;
         final LocalDate beginningOfMonth = beginningOfMonth(date);
         final int monthOfYear = beginningOfMonth.getMonthValue();
         final int quarter = (monthOfYear-1)/MONTHS_IN_QUARTER; // 0, 1, 2, 3
@@ -84,8 +42,5 @@ public class CalendarService {
         return beginningOfMonth.minusMonths(deltaMonth);
     }
 
-    private LocalDate nowAsLocalDate() {
-        return clockService.getClock().nowAsLocalDate(ZoneId.systemDefault());
-    }
 
-}
+}
\ No newline at end of file
diff --git a/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java b/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceDefault.java
similarity index 55%
copy from subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java
copy to subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceDefault.java
index ed0a5ab..28b9380 100644
--- a/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarService.java
+++ b/subdomains/base/applib/src/main/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceDefault.java
@@ -18,71 +18,46 @@
  */
 package org.apache.isis.subdomains.base.applib.services.calendar;
 
-import lombok.RequiredArgsConstructor;
-import org.apache.isis.applib.annotation.PriorityPrecedence;
-import org.apache.isis.applib.services.clock.ClockService;
-import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.stereotype.Service;
+import java.time.LocalDate;
+import java.time.ZoneId;
 
 import javax.annotation.Priority;
 import javax.inject.Inject;
 import javax.inject.Named;
-import java.time.LocalDate;
-import java.time.ZoneId;
 
-/**
- * @since 2.0 {@index}
- */
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Service;
+
+import org.apache.isis.applib.annotation.PriorityPrecedence;
+import org.apache.isis.applib.services.clock.ClockService;
+
+import lombok.RequiredArgsConstructor;
+
 @Service
-@Named("isis.sub.base.CalendarService")
+@Named("isis.sub.base.CalendarServiceDefault")
 @Priority(PriorityPrecedence.MIDPOINT)
 @Qualifier("Default")
 @RequiredArgsConstructor(onConstructor_ = {@Inject})
-public class CalendarService {
-
-    private static final int MONTHS_IN_QUARTER = 3;
+public class CalendarServiceDefault implements CalendarService {
 
     private final ClockService clockService;
 
-    /**
-     * Returns the date corresponding to the beginning of the current month.
-     */
+    @Override
     public LocalDate beginningOfMonth() {
-        return beginningOfMonth(nowAsLocalDate());
+        return CalendarService.beginningOfMonth(nowAsLocalDate());
     }
 
-    static LocalDate beginningOfMonth(final LocalDate date) {
-        final long dayOfMonth = date.getDayOfMonth();
-        return date.minusDays(dayOfMonth-1L);
-    }
-
-    /**
-     * Returns the date corresponding to the beginning of the current quarter (typically: January, April, July or October).
-     *
-     * @see #beginningOfQuarter(LocalDate)
-     * @see #beginningOfNextQuarter()
-     */
+    @Override
     public LocalDate beginningOfQuarter() {
-        return beginningOfQuarter(nowAsLocalDate());
+        return CalendarService.beginningOfQuarter(nowAsLocalDate());
     }
 
-    /**
-     * Returns the date corresponding to the beginning of the quarter following this one.
-     *
-     * @see #beginningOfQuarter()
-     */
+    @Override
     public LocalDate beginningOfNextQuarter() {
-        return beginningOfQuarter(nowAsLocalDate().plusMonths(3));
+        return CalendarService.beginningOfQuarter(nowAsLocalDate().plusMonths(3));
     }
 
-    static LocalDate beginningOfQuarter(final LocalDate date) {
-        final LocalDate beginningOfMonth = beginningOfMonth(date);
-        final int monthOfYear = beginningOfMonth.getMonthValue();
-        final int quarter = (monthOfYear-1)/MONTHS_IN_QUARTER; // 0, 1, 2, 3
-        final int monthStartOfQuarter = quarter*MONTHS_IN_QUARTER+1;
-        final long deltaMonth = (long)monthOfYear - monthStartOfQuarter;
-        return beginningOfMonth.minusMonths(deltaMonth);
-    }
+    // -- HELPER
 
     private LocalDate nowAsLocalDate() {
         return clockService.getClock().nowAsLocalDate(ZoneId.systemDefault());
diff --git a/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfMonth.java b/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfMonth.java
index 95ed197..701261e 100644
--- a/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfMonth.java
+++ b/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfMonth.java
@@ -66,7 +66,7 @@ public class CalendarServiceTest_beginningOfMonth {
                 return VirtualClock.frozenAt(Instant.from(now));
             }
         };
-        calendarService = new CalendarService(stubClockService);
+        calendarService = new CalendarServiceDefault(stubClockService);
     }
 
     @Test
diff --git a/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfQuarter.java b/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfQuarter.java
index bba8046..553dd54 100644
--- a/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfQuarter.java
+++ b/subdomains/base/applib/src/test/java/org/apache/isis/subdomains/base/applib/services/calendar/CalendarServiceTest_beginningOfQuarter.java
@@ -81,7 +81,7 @@ public class CalendarServiceTest_beginningOfQuarter {
                 return VirtualClock.frozenAt(Instant.from(now));
             }
         };
-        calendarService = new CalendarService(stubClockService);
+        calendarService = new CalendarServiceDefault(stubClockService);
     }
 
     @Test