| 1 | package ch.poole.openinghoursparser; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import static ch.poole.openinghoursparser.I18n.tr; |
| 7 | |
| 8 | /** |
| 9 | * Container for objects from the opening_hours specification |
| 10 | * |
| 11 | * @author Simon Poole |
| 12 | * |
| 13 | * Copyright (c) 2015 Simon Poole |
| 14 | * |
| 15 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 16 | * documentation files (the "Software"), to deal in the Software without restriction, including without |
| 17 | * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 18 | * Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 19 | * conditions: |
| 20 | * |
| 21 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 22 | * of the Software. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 25 | * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 26 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 27 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER |
| 28 | * DEALINGS IN THE SOFTWARE. |
| 29 | */ |
| 30 | public enum Month { |
| 31 | JAN("Jan"), FEB("Feb"), MAR("Mar"), APR("Apr"), MAY("May"), JUN("Jun"), JUL("Jul"), AUG("Aug"), SEP("Sep"), OCT("Oct"), NOV("Nov"), DEC("Dec"); |
| 32 | |
| 33 | private static final int[] LASTDAY = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 34 | |
| 35 | private final String name; |
| 36 | |
| 37 | /** |
| 38 | * Construct a new member of the enum |
| 39 | * |
| 40 | * @param name the name as a String |
| 41 | */ |
| 42 | Month(String name) { |
| 43 | this.name = name; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String toString() { |
| 48 | return name; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the Month value for a String |
| 53 | * |
| 54 | * @param month the month as a String |
| 55 | * @return a Month or null |
| 56 | */ |
| 57 | public static Month getValue(String month) { |
| 58 | for (Month m : Month.values()) { |
| 59 | if (m.toString().equals(month)) { |
| 60 | return m; |
| 61 | } |
| 62 | } |
| 63 | throw new IllegalArgumentException(tr("invalid_month", month)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get list of all values as Strings |
| 68 | * |
| 69 | * @return a List with all values |
| 70 | */ |
| 71 | public static List<String> nameValues() { |
| 72 | List<String> result = new ArrayList<>(); |
| 73 | for (Month m : values()) { |
| 74 | result.add(m.toString()); |
| 75 | } |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Return the last day of the month |
| 81 | * |
| 82 | * @param year the year |
| 83 | * @param month the month |
| 84 | * @return the last day |
| 85 | * @throws ParseException if no valid year is supplied and the month is February |
| 86 | */ |
| 87 | public static int lastDay(int year, Month month) throws ParseException { |
| 88 | if (month != FEB) { |
| 89 | return LASTDAY[month.ordinal()]; |
| 90 | } else { |
| 91 | if (year == YearRange.UNDEFINED_YEAR) { |
| 92 | throw new OpeningHoursParseException(tr("missing_day_for_February")); |
| 93 | } |
| 94 | if ((year / 4) * 4 == year && year / 100 * 100 != year) { |
| 95 | return 29; |
| 96 | } else { |
| 97 | return 28; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |