View Javadoc
1   /*
2    * Oceanus: Java Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.oceanus.date;
18  
19  import io.github.tonywasher.joceanus.oceanus.resource.OceanusBundleId;
20  
21  import java.time.temporal.ChronoUnit;
22  
23  /**
24   * DatePeriod class representing standard date ranges.
25   *
26   * @author Tony Washer
27   */
28  public enum OceanusDatePeriod {
29      /**
30       * OneWeek.
31       */
32      ONEWEEK(ChronoUnit.WEEKS, 1),
33  
34      /**
35       * Two Weeks.
36       */
37      FORTNIGHT(ChronoUnit.WEEKS, 2),
38  
39      /**
40       * One Month.
41       */
42      ONEMONTH(ChronoUnit.MONTHS, 1),
43  
44      /**
45       * Three Months.
46       */
47      QUARTERYEAR(ChronoUnit.MONTHS, 3),
48  
49      /**
50       * Six Months.
51       */
52      HALFYEAR(ChronoUnit.MONTHS, 6),
53  
54      /**
55       * One Year.
56       */
57      ONEYEAR(ChronoUnit.YEARS, 1),
58  
59      /**
60       * Calendar Month.
61       */
62      CALENDARMONTH(ChronoUnit.MONTHS, 1),
63  
64      /**
65       * Calendar Quarter.
66       */
67      CALENDARQUARTER(ChronoUnit.MONTHS, 3),
68  
69      /**
70       * Calendar Year.
71       */
72      CALENDARYEAR(ChronoUnit.YEARS, 1),
73  
74      /**
75       * Fiscal Year.
76       */
77      FISCALYEAR(ChronoUnit.YEARS, 1),
78  
79      /**
80       * Dates Up to.
81       */
82      DATESUPTO(null, -1),
83  
84      /**
85       * Custom.
86       */
87      CUSTOM(null, -1),
88  
89      /**
90       * All.
91       */
92      ALLDATES(null, -1);
93  
94      /**
95       * The String name.
96       */
97      private final String theName;
98  
99      /**
100      * The calendar field.
101      */
102     private final transient ChronoUnit theField;
103 
104     /**
105      * The adjustments amount.
106      */
107     private final int theAmount;
108 
109     /**
110      * Constructor.
111      *
112      * @param pField  the Calendar field
113      * @param pAmount the adjustment value
114      */
115     OceanusDatePeriod(final ChronoUnit pField,
116                       final int pAmount) {
117         /* Store values */
118         theField = pField;
119         theAmount = pAmount;
120         theName = bundleIdForPeriod(this).getValue();
121     }
122 
123     /**
124      * Obtain field.
125      *
126      * @return the field
127      */
128     public ChronoUnit getField() {
129         return theField;
130     }
131 
132     /**
133      * Obtain amount.
134      *
135      * @param bForward forward/backward amount
136      * @return the amount
137      */
138     public int getAmount(final boolean bForward) {
139         return bForward
140                 ? theAmount
141                 : -theAmount;
142     }
143 
144     @Override
145     public String toString() {
146         return theName;
147     }
148 
149     /**
150      * Is period next/previous available?
151      *
152      * @return true/false
153      */
154     public boolean adjustPeriod() {
155         return theField != null;
156     }
157 
158     /**
159      * Is period DatesUpTo?
160      *
161      * @return true/false
162      */
163     public boolean datesUpTo() {
164         return this == DATESUPTO;
165     }
166 
167     /**
168      * Is period a containing period?
169      *
170      * @return true/false
171      */
172     public boolean isContaining() {
173         return switch (this) {
174             case CALENDARMONTH, CALENDARQUARTER, CALENDARYEAR, FISCALYEAR -> true;
175             default -> false;
176         };
177     }
178 
179     /**
180      * Obtain the resource bundleId for the period.
181      *
182      * @param pPeriod the period
183      * @return the resource bundleId
184      */
185     private static OceanusBundleId bundleIdForPeriod(final OceanusDatePeriod pPeriod) {
186         /* Create the map and return it */
187         return switch (pPeriod) {
188             case ONEWEEK -> OceanusDateResource.PERIOD_ONEWEEK;
189             case FORTNIGHT -> OceanusDateResource.PERIOD_FORTNIGHT;
190             case ONEMONTH -> OceanusDateResource.PERIOD_ONEMONTH;
191             case QUARTERYEAR -> OceanusDateResource.PERIOD_QUARTERYEAR;
192             case HALFYEAR -> OceanusDateResource.PERIOD_HALFYEAR;
193             case ONEYEAR -> OceanusDateResource.PERIOD_ONEYEAR;
194             case CALENDARMONTH -> OceanusDateResource.PERIOD_CALENDARMONTH;
195             case CALENDARQUARTER -> OceanusDateResource.PERIOD_CALENDARQUARTER;
196             case CALENDARYEAR -> OceanusDateResource.PERIOD_CALENDARYEAR;
197             case FISCALYEAR -> OceanusDateResource.PERIOD_FISCALYEAR;
198             case DATESUPTO -> OceanusDateResource.PERIOD_DATESUPTO;
199             case CUSTOM -> OceanusDateResource.PERIOD_CUSTOM;
200             case ALLDATES -> OceanusDateResource.PERIOD_ALLDATES;
201             default -> throw new IllegalArgumentException();
202         };
203     }
204 }