1   /* Copyright 2002-2024 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy 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,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.time;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.orekit.errors.OrekitIllegalArgumentException;
23  import org.orekit.errors.OrekitMessages;
24  
25  /** Enumerate representing a calendar month.
26   * <p>This enum is mainly useful to parse data files that use month names
27   * like Jan or JAN or January or numbers like 1 or 01. It handles month numbers
28   * as well as three letters abbreviation and full names, independently of capitalization.</p>
29   * @see DateComponents
30   * @author Luc Maisonobe
31   */
32  public enum Month {
33  
34      /** January. */
35      JANUARY( 1),
36  
37      /** February. */
38      FEBRUARY( 2),
39  
40      /** March. */
41      MARCH( 3),
42  
43      /** April. */
44      APRIL( 4),
45  
46      /** May. */
47      MAY( 5),
48  
49      /** June. */
50      JUNE( 6),
51  
52      /** July. */
53      JULY( 7),
54  
55      /** August. */
56      AUGUST( 8),
57  
58      /** September. */
59      SEPTEMBER( 9),
60  
61      /** October. */
62      OCTOBER(10),
63  
64      /** November. */
65      NOVEMBER(11),
66  
67      /** December. */
68      DECEMBER(12);
69  
70      /** Parsing map. */
71      private static final Map<String, Month> STRINGS_MAP = new HashMap<String, Month>();
72      static {
73          for (final Month month : values()) {
74              STRINGS_MAP.put(month.getLowerCaseName(),         month);
75              STRINGS_MAP.put(month.getLowerCaseAbbreviation(), month);
76          }
77      }
78  
79      /** Numbers map. */
80      private static final Map<Integer, Month> NUMBERS_MAP = new HashMap<Integer, Month>();
81      static {
82          for (final Month month : values()) {
83              NUMBERS_MAP.put(month.getNumber(), month);
84          }
85      }
86  
87      /** Month number. */
88      private final int number;
89  
90      /** Lower case full name. */
91      private final String lowerCaseName;
92  
93      /** Capitalized full name. */
94      private final String capitalizedName;
95  
96      /** Upper case three letters abbreviation. */
97      private final String upperCaseAbbreviation;
98  
99      /** Lower case three letters abbreviation. */
100     private final String lowerCaseAbbreviation;
101 
102     /** Capitalized three letters abbreviation. */
103     private final String capitalizedAbbreviation;
104 
105     /** Simple constructor.
106      * @param number month number
107      */
108     Month(final int number) {
109         this.number             = number;
110         lowerCaseName           = toString().toLowerCase();
111         capitalizedName         = toString().charAt(0) + lowerCaseName.substring(1);
112         upperCaseAbbreviation   = toString().substring(0, 3);
113         lowerCaseAbbreviation   = lowerCaseName.substring(0, 3);
114         capitalizedAbbreviation = capitalizedName.substring(0, 3);
115     }
116 
117     /** Get the month number.
118      * @return month number between 1 and 12
119      */
120     public int getNumber() {
121         return number;
122     }
123 
124     /** Get the upper case full name.
125      * @return upper case full name
126      */
127     public String getUpperCaseName() {
128         return toString();
129     }
130 
131     /** Get the lower case full name.
132      * @return lower case full name
133      */
134     public String getLowerCaseName() {
135         return lowerCaseName;
136     }
137 
138     /** Get the capitalized full name.
139      * @return capitalized full name
140      */
141     public String getCapitalizedName() {
142         return capitalizedName;
143     }
144 
145     /** Get the upper case three letters abbreviation.
146      * @return upper case three letters abbreviation
147      */
148     public String getUpperCaseAbbreviation() {
149         return upperCaseAbbreviation;
150     }
151 
152     /** Get the lower case three letters abbreviation.
153      * @return lower case three letters abbreviation
154      */
155     public String getLowerCaseAbbreviation() {
156         return lowerCaseAbbreviation;
157     }
158 
159     /** Get the capitalized three letters abbreviation.
160      * @return capitalized three letters abbreviation
161      */
162     public String getCapitalizedAbbreviation() {
163         return capitalizedAbbreviation;
164     }
165 
166     /** Parse the string to get the month.
167      * <p>
168      * The string can be either the month number, the full name or the
169      * three letter abbreviation. The parsing ignore the case of the specified
170      * string and trims surrounding blanks.
171      * </p>
172      * @param s string to parse
173      * @return the month corresponding to the string
174      * @exception IllegalArgumentException if the string does not correspond to a month
175      */
176     public static Month parseMonth(final String s) {
177         final String normalizedString = s.trim().toLowerCase();
178         final Month month = STRINGS_MAP.get(normalizedString);
179         if (month == null) {
180             try {
181                 return getMonth(Integer.parseInt(normalizedString));
182             } catch (NumberFormatException nfe) {
183                 throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_MONTH, s);
184             }
185         }
186         return month;
187     }
188 
189     /** Get the month corresponding to a number.
190      * @param number month number
191      * @return the month corresponding to the string
192      * @exception IllegalArgumentException if the string does not correspond to a month
193      */
194     public static Month getMonth(final int number) {
195         final Month month = NUMBERS_MAP.get(number);
196         if (month == null) {
197             throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_MONTH, number);
198         }
199         return month;
200     }
201 
202 }