[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / api / Season.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.api;
18
19 import java.util.Calendar;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22
23 /**
24  * Season Entity.
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public class Season {
29
30     private static final Calendar CURRENT_CALENDAR = Calendar.getInstance();
31
32     public static final int BEGIN_YEAR = 2005;
33     public static final int CURRENT_YEAR =
34             CURRENT_CALENDAR.get(Calendar.MONTH) >= 6
35                     ? CURRENT_CALENDAR.get(Calendar.YEAR)
36                     : CURRENT_CALENDAR.get(Calendar.YEAR) - 1;
37
38     private static final int ALL_SEASONS = -1;
39
40     public static final Season ALL = new Season(ALL_SEASONS);
41     public static final Season CURRENT_SEASON = new Season(CURRENT_YEAR);
42
43     public static Map<Integer, Season> SEASONS = new LinkedHashMap<>();
44
45     static {
46         for (int year = BEGIN_YEAR; year < CURRENT_YEAR; ++year) {
47             SEASONS.put(year, new Season(year));
48         }
49         SEASONS.put(CURRENT_YEAR, CURRENT_SEASON);
50         SEASONS.put(ALL_SEASONS, ALL);
51     }
52
53     private int year;
54
55     private Season(int year) {
56         this.year = year;
57     }
58
59     public static Season getSeason(int year) {
60         return SEASONS.get(year);
61     }
62
63     public int getYear() {
64         return year;
65     }
66
67     @Override
68     public String toString() {
69         return year == ALL_SEASONS ? "All Seasons" : year + "/" + (year + 1);
70     }
71
72 }