Month.java

  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. import java.util.HashMap;
  19. import java.util.Map;

  20. import org.orekit.errors.OrekitIllegalArgumentException;
  21. import org.orekit.errors.OrekitMessages;

  22. /** Enumerate representing a calendar month.
  23.  * <p>This enum is mainly useful to parse data files that use month names
  24.  * like Jan or JAN or January or numbers like 1 or 01. It handles month numbers
  25.  * as well as three letters abbreviation and full names, independently of capitalization.</p>
  26.  * @see DateComponents
  27.  * @author Luc Maisonobe
  28.  */
  29. public enum Month {

  30.     /** January. */
  31.     JANUARY( 1),

  32.     /** February. */
  33.     FEBRUARY( 2),

  34.     /** March. */
  35.     MARCH( 3),

  36.     /** April. */
  37.     APRIL( 4),

  38.     /** May. */
  39.     MAY( 5),

  40.     /** June. */
  41.     JUNE( 6),

  42.     /** July. */
  43.     JULY( 7),

  44.     /** August. */
  45.     AUGUST( 8),

  46.     /** September. */
  47.     SEPTEMBER( 9),

  48.     /** October. */
  49.     OCTOBER(10),

  50.     /** November. */
  51.     NOVEMBER(11),

  52.     /** December. */
  53.     DECEMBER(12);

  54.     /** Parsing map. */
  55.     private static final Map<String, Month> STRINGS_MAP = new HashMap<String, Month>();
  56.     static {
  57.         for (final Month month : values()) {
  58.             STRINGS_MAP.put(month.getLowerCaseName(),         month);
  59.             STRINGS_MAP.put(month.getLowerCaseAbbreviation(), month);
  60.         }
  61.     }

  62.     /** Numbers map. */
  63.     private static final Map<Integer, Month> NUMBERS_MAP = new HashMap<Integer, Month>();
  64.     static {
  65.         for (final Month month : values()) {
  66.             NUMBERS_MAP.put(month.getNumber(), month);
  67.         }
  68.     }

  69.     /** Month number. */
  70.     private final int number;

  71.     /** Lower case full name. */
  72.     private final String lowerCaseName;

  73.     /** Capitalized full name. */
  74.     private final String capitalizedName;

  75.     /** Upper case three letters abbreviation. */
  76.     private final String upperCaseAbbreviation;

  77.     /** Lower case three letters abbreviation. */
  78.     private final String lowerCaseAbbreviation;

  79.     /** Capitalized three letters abbreviation. */
  80.     private final String capitalizedAbbreviation;

  81.     /** Simple constructor.
  82.      * @param number month number
  83.      */
  84.     Month(final int number) {
  85.         this.number             = number;
  86.         lowerCaseName           = toString().toLowerCase();
  87.         capitalizedName         = toString().charAt(0) + lowerCaseName.substring(1);
  88.         upperCaseAbbreviation   = toString().substring(0, 3);
  89.         lowerCaseAbbreviation   = lowerCaseName.substring(0, 3);
  90.         capitalizedAbbreviation = capitalizedName.substring(0, 3);
  91.     }

  92.     /** Get the month number.
  93.      * @return month number between 1 and 12
  94.      */
  95.     public int getNumber() {
  96.         return number;
  97.     }

  98.     /** Get the upper case full name.
  99.      * @return upper case full name
  100.      */
  101.     public String getUpperCaseName() {
  102.         return toString();
  103.     }

  104.     /** Get the lower case full name.
  105.      * @return lower case full name
  106.      */
  107.     public String getLowerCaseName() {
  108.         return lowerCaseName;
  109.     }

  110.     /** Get the capitalized full name.
  111.      * @return capitalized full name
  112.      */
  113.     public String getCapitalizedName() {
  114.         return capitalizedName;
  115.     }

  116.     /** Get the upper case three letters abbreviation.
  117.      * @return upper case three letters abbreviation
  118.      */
  119.     public String getUpperCaseAbbreviation() {
  120.         return upperCaseAbbreviation;
  121.     }

  122.     /** Get the lower case three letters abbreviation.
  123.      * @return lower case three letters abbreviation
  124.      */
  125.     public String getLowerCaseAbbreviation() {
  126.         return lowerCaseAbbreviation;
  127.     }

  128.     /** Get the capitalized three letters abbreviation.
  129.      * @return capitalized three letters abbreviation
  130.      */
  131.     public String getCapitalizedAbbreviation() {
  132.         return capitalizedAbbreviation;
  133.     }

  134.     /** Parse the string to get the month.
  135.      * <p>
  136.      * The string can be either the month number, the full name or the
  137.      * three letter abbreviation. The parsing ignore the case of the specified
  138.      * string and trims surrounding blanks.
  139.      * </p>
  140.      * @param s string to parse
  141.      * @return the month corresponding to the string
  142.      * @exception IllegalArgumentException if the string does not correspond to a month
  143.      */
  144.     public static Month parseMonth(final String s) {
  145.         final String normalizedString = s.trim().toLowerCase();
  146.         final Month month = STRINGS_MAP.get(normalizedString);
  147.         if (month == null) {
  148.             try {
  149.                 return getMonth(Integer.parseInt(normalizedString));
  150.             } catch (NumberFormatException nfe) {
  151.                 throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_MONTH, s);
  152.             }
  153.         }
  154.         return month;
  155.     }

  156.     /** Get the month corresponding to a number.
  157.      * @param number month number
  158.      * @return the month corresponding to the string
  159.      * @exception IllegalArgumentException if the string does not correspond to a month
  160.      */
  161.     public static Month getMonth(final int number) {
  162.         final Month month = NUMBERS_MAP.get(number);
  163.         if (month == null) {
  164.             throw new OrekitIllegalArgumentException(OrekitMessages.UNKNOWN_MONTH, number);
  165.         }
  166.         return month;
  167.     }

  168. }