View Javadoc
1   /*
2    * #%L
3    * Nuiton Utils
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.util;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import static org.nuiton.i18n.I18n.t;
29  
30  /**
31   * Une énumération pour représenter les mois d'une année.
32   *
33   * @author Tony Chemit - chemit@codelutin.com
34   */
35  
36  public enum MonthEnum {
37  
38      JANUARY(t("nuitonutil.month.january")),
39      FEBRUARY(t("nuitonutil.month.february")),
40      MARCH(t("nuitonutil.month.march")),
41      APRIL(t("nuitonutil.month.april")),
42      MAY(t("nuitonutil.month.may")),
43      JUNE(t("nuitonutil.month.june")),
44      JULY(t("nuitonutil.month.july")),
45      AUGUST(t("nuitonutil.month.august")),
46      SEPTEMBER(t("nuitonutil.month.september")),
47      OCTOBER(t("nuitonutil.month.october")),
48      NOVEMBER(t("nuitonutil.month.november")),
49      DECEMBER(t("nuitonutil.month.december"));
50  
51      /** Logger */
52      private static final Log log = LogFactory.getLog(MonthEnum.class);
53  
54      private final String libelle;
55  
56      MonthEnum(String libelle) {
57          this.libelle = libelle;
58      }
59  
60      public String getLibelle() {
61          return libelle;
62      }
63  
64      public static MonthEnum valueOf(String month, MonthEnum defaultValue) {
65          MonthEnum monthEnum = null;
66          try {
67              monthEnum = valueOf(month.toUpperCase());
68          } catch (Exception e) {
69              log.error(
70                      t("nuitonutil.error.unfound.month", month, defaultValue),
71                      e);
72          }
73          return monthEnum == null ? defaultValue : monthEnum;
74      }
75  
76      @Override
77      public String toString() {
78          // on force la traduction (au cas où i18n n'était pas ini au moment
79          // du chargement de l'enum...)
80          return t(libelle);
81      }
82  }