[LIB-8] Console output, properties, date utils
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerScoreConsole.java
1 /*
2  * Copyright (c) 2017-2019. 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.util.Calendar;
20
21 /**
22  * Snooker Score utility class for working with console.
23  *
24  * @author Dmitry Samoshin aka gotty
25  */
26 public final class SnookerScoreConsole {
27
28     private static final String[] ASCII_LOGO = {
29             "                     _______     _                   _                          ",
30             "                    |   |  _|_ _| |___ ___ ___ ___ _| |___                      ",
31             "                    |     | -_| . | . | -_|  _| . | . | -_|                     ",
32             "                    |     |___|___|_  |___|___|___|___|___|                     ",
33             "                    |___|___|  _  |___|                                         ",
34             "               ___ ___ ___ ___| |_ ___ ___   ___ ___ ___ ___ ___                ",
35             "              |_ -|   | . | . | '_| -_|  _| |_ -|  _| . |  _| -_|               ",
36             "              |___|_|_|___|___|_,_|___|_|   |___|___|___|_| |___|               ",
37             "                                 _____ _____ _                                  ",
38             "                                |  _  |  _  |_|                                 ",
39             "                                |     |   __| |                                 ",
40             "                                |__|__|__|  |_|                                 "
41     };
42
43     private static final String COPYRIGHT = "Copyright (c) Hedgecode";
44
45     private static final String DELIMITER = "*";
46
47     private static final String[] WELCOME = {
48             "                    Welcome to Hedgecode Snooker Score API!                     ",
49             "                -----------------------------------------------                 ",
50             "         It is an API library for portal snooker.org, which contains            ",
51             "      the results of snooker competitions and other snooker information.        ",
52             "    This library provides a set of entity objects that can be used in client    ",
53             "    applications (to inform about the results of snooker), developed in Java.   "
54     };
55
56     public static final String ARG_QUIET = "quiet";
57     public static final String ARG_SERVER = "server";
58     public static final String ARG_PORT = "port";
59     public static final String ARG_HELP = "help";
60
61     private SnookerScoreConsole() {
62     }
63
64     public static void console(String message) {
65         System.out.println(
66                 message == null ? "" : message
67         );
68     }
69
70     public static void empty() {
71         console(null);
72     }
73
74     public static void delimiter() {
75         console(
76                 String.format(
77                         "%" + length() + "s",
78                         DELIMITER
79                 ).replace(" ", DELIMITER)
80         );
81     }
82
83     public static void init() {
84         asciiLogo();
85         empty();
86         copyright();
87         version();
88         empty();
89     }
90
91     public static void welcome() {
92         for (String line : WELCOME) {
93             console(line);
94         }
95     }
96
97     public static void help() {
98         console(
99                 String.format(
100                         "Usage: java -jar %s-%s.jar -[%s|%s|%s XXXX|%s]",
101                         SnookerScoreProperties.get("snooker.name"),
102                         SnookerScoreProperties.get("snooker.version"),
103                         ARG_QUIET,
104                         ARG_SERVER,
105                         ARG_PORT,
106                         ARG_HELP
107                 )
108         );
109     }
110
111     private static void asciiLogo() {
112         for (String line : ASCII_LOGO) {
113             console(line);
114         }
115     }
116
117     private static void copyright() {
118         int inceptionYear = Integer.parseInt(
119                 SnookerScoreProperties.get("snooker.inception.year")
120         );
121         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
122         String copyright = currentYear > inceptionYear
123                 ? String.format("%s-%s %s", inceptionYear, currentYear, COPYRIGHT)
124                 : String.format("%s %s", currentYear, COPYRIGHT);
125         console(
126                 alignCenter(copyright)
127         );
128     }
129
130     private static void version() {
131         console(
132                 alignCenter(
133                         String.format(
134                                 "v. %s", SnookerScoreProperties.get("snooker.version")
135                         )
136                 )
137         );
138     }
139
140     private static String alignCenter(String message) {
141         int indent = (length() + message.length()) / 2;
142         return String.format(
143                 "%" + indent + "s",
144                 message
145         );
146     }
147
148     private static int length() {
149         return ASCII_LOGO[0].length();
150     }
151
152 }