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 java.time.temporal.ChronoUnit;
20
21 /**
22 * DatePeriod class representing standard date ranges.
23 *
24 * @author Tony Washer
25 */
26 public enum OceanusDatePeriod {
27 /**
28 * OneWeek.
29 */
30 ONEWEEK(ChronoUnit.WEEKS, 1),
31
32 /**
33 * Two Weeks.
34 */
35 FORTNIGHT(ChronoUnit.WEEKS, 2),
36
37 /**
38 * One Month.
39 */
40 ONEMONTH(ChronoUnit.MONTHS, 1),
41
42 /**
43 * Three Months.
44 */
45 QUARTERYEAR(ChronoUnit.MONTHS, 3),
46
47 /**
48 * Six Months.
49 */
50 HALFYEAR(ChronoUnit.MONTHS, 6),
51
52 /**
53 * One Year.
54 */
55 ONEYEAR(ChronoUnit.YEARS, 1),
56
57 /**
58 * Calendar Month.
59 */
60 CALENDARMONTH(ChronoUnit.MONTHS, 1),
61
62 /**
63 * Calendar Quarter.
64 */
65 CALENDARQUARTER(ChronoUnit.MONTHS, 3),
66
67 /**
68 * Calendar Year.
69 */
70 CALENDARYEAR(ChronoUnit.YEARS, 1),
71
72 /**
73 * Fiscal Year.
74 */
75 FISCALYEAR(ChronoUnit.YEARS, 1),
76
77 /**
78 * Dates Up to.
79 */
80 DATESUPTO(null, -1),
81
82 /**
83 * Custom.
84 */
85 CUSTOM(null, -1),
86
87 /**
88 * All.
89 */
90 ALLDATES(null, -1);
91
92 /**
93 * The String name.
94 */
95 private String theName;
96
97 /**
98 * The calendar field.
99 */
100 private final transient ChronoUnit theField;
101
102 /**
103 * The adjustments amount.
104 */
105 private final int theAmount;
106
107 /**
108 * Constructor.
109 *
110 * @param pField the Calendar field
111 * @param pAmount the adjustment value
112 */
113 OceanusDatePeriod(final ChronoUnit pField,
114 final int pAmount) {
115 /* Store values */
116 theField = pField;
117 theAmount = pAmount;
118 }
119
120 /**
121 * Obtain field.
122 *
123 * @return the field
124 */
125 public ChronoUnit getField() {
126 return theField;
127 }
128
129 /**
130 * Obtain amount.
131 *
132 * @param bForward forward/backward amount
133 * @return the amount
134 */
135 public int getAmount(final boolean bForward) {
136 return bForward
137 ? theAmount
138 : -theAmount;
139 }
140
141 @Override
142 public String toString() {
143 /* If we have not yet loaded the name */
144 if (theName == null) {
145 /* Load the name */
146 theName = OceanusDateResource.getKeyForPeriod(this).getValue();
147 }
148
149 /* return the name */
150 return theName;
151 }
152
153 /**
154 * Is period next/previous available?
155 *
156 * @return true/false
157 */
158 public boolean adjustPeriod() {
159 return theField != null;
160 }
161
162 /**
163 * Is period DatesUpTo?
164 *
165 * @return true/false
166 */
167 public boolean datesUpTo() {
168 return this == DATESUPTO;
169 }
170
171 /**
172 * Is period a containing period?
173 *
174 * @return true/false
175 */
176 public boolean isContaining() {
177 switch (this) {
178 case CALENDARMONTH:
179 case CALENDARQUARTER:
180 case CALENDARYEAR:
181 case FISCALYEAR:
182 return true;
183 default:
184 return false;
185 }
186 }
187 }