[LIB-5] Collection empty objects,reporting and serializable
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerScoreApp.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;
18
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21
22 import org.hedgecode.snooker.api.APIException;
23 import org.hedgecode.snooker.api.Event;
24 import org.hedgecode.snooker.api.Events;
25 import org.hedgecode.snooker.api.Season;
26
27 /**
28  * Snooker Score Main Application.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public final class SnookerScoreApp {
33
34     private static final String EMPTY = "";
35     private static final String DELIMITER =
36             "********************************************************************************";
37     private static final String[] WELCOME = {
38             "                    Welcome to Hedgecode Snooker Score API!                     ",
39             "                -----------------------------------------------                 ",
40             "         It is an API library for portal snooker.org, which contains            ",
41             "      the results of snooker competitions and other snooker information.        ",
42             "    This library provides a set of entity objects that can be used in client    ",
43             "    applications (to inform about the results of snooker), developed in Java.   "
44     };
45     private static final String CURRENT_EVENTS = "  Current Snooker Events:  ";
46     private static final String UPCOMING_EVENTS = "  Upcoming Snooker Events:  ";
47     private static final String INDENT = "     ";
48
49     private static final String DATE_FORMAT = "dd.MM.yyyy";
50
51     private static final int UPCOMING_COUNT = 5;
52
53     public static void main(String[] args) throws APIException
54     {
55         System.out.println(EMPTY);
56         System.out.println(DELIMITER);
57         for (String line : WELCOME) {
58             System.out.println(line);
59         }
60
61         Events events = Snooker.uncachedAPI().getSeasonEvents(
62                 Season.CURRENT_SEASON
63         );
64         events.sortByDate();
65
66         Events currentEvents = events.current();
67         if (!currentEvents.isEmpty()) {
68             System.out.println(DELIMITER);
69             System.out.println(CURRENT_EVENTS);
70         }
71         for (Event currentEvent : currentEvents) {
72             printEvent(currentEvent);
73         }
74
75         System.out.println(DELIMITER);
76         System.out.println(UPCOMING_EVENTS);
77
78         int count = 0;
79         Events upcomingEvents = events.upcoming();
80         for (Event upcomingEvent : upcomingEvents) {
81             printEvent(upcomingEvent);
82             ++count;
83             if (count >= UPCOMING_COUNT) break;
84         }
85         System.out.println(DELIMITER);
86         System.out.println(EMPTY);
87     }
88
89     private static void printEvent(Event event) {
90         final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
91         System.out.println(
92                 INDENT  + event.name()
93                         + " [" + dateFormat.format(event.startDate())
94                         + " - " + dateFormat.format(event.endDate()) + "]"
95                         + " (" + event.country() + ", " + event.city() + ")"
96         );
97     }
98
99 }