[LIB-5] Collection empty objects,reporting and serializable
[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.io.Serializable;
20 import java.util.Calendar;
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23
24 /**
25  * Season Entity.
26  *
27  * @author Dmitry Samoshin aka gotty
28  */
29 public class Season implements Serializable {
30
31     private static final Calendar CURRENT_CALENDAR = Calendar.getInstance();
32
33     public static final int BEGIN_YEAR = 2005;
34     public static final int CURRENT_YEAR =
35             CURRENT_CALENDAR.get(Calendar.MONTH) >= 6
36                     ? CURRENT_CALENDAR.get(Calendar.YEAR)
37                     : CURRENT_CALENDAR.get(Calendar.YEAR) - 1;
38
39     public static final int ALL_SEASONS = -1;
40
41     public static final Season ALL = new Season(ALL_SEASONS);
42     public static final Season CURRENT_SEASON = new Season(CURRENT_YEAR);
43
44     private static final Map<Integer, Season> SEASONS = new LinkedHashMap<>();
45
46     static {
47         for (int year = BEGIN_YEAR; year < CURRENT_YEAR; ++year) {
48             SEASONS.put(year, new Season(year));
49         }
50         SEASONS.put(CURRENT_YEAR, CURRENT_SEASON);
51         SEASONS.put(ALL_SEASONS, ALL);
52     }
53
54     private int year;
55
56     private Season(int year) {
57         this.year = year;
58     }
59
60     public static Season getSeason(int year) {
61         return SEASONS.get(year);
62     }
63
64     public int getYear() {
65         return year;
66     }
67
68     @Override
69     public String toString() {
70         return year == ALL_SEASONS ? "All Seasons" : year + "/" + (year + 1);
71     }
72
73 }