[LIB-8] Console output, properties, date utils
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerScoreProperties.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.io.IOException;
20 import java.io.InputStream;
21 import java.util.Properties;
22
23 /**
24  * Snooker Score properties holder.
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public final class SnookerScoreProperties {
29
30     /** Properties file. */
31     private static final String PROPERTIES_FILE = "snooker.properties";
32
33     /** Properties. */
34     private static final Properties PROPERTIES;
35
36     /**
37      * Static properties initialization.
38      */
39     static {
40         PROPERTIES = new Properties();
41         readProperties(PROPERTIES_FILE, PROPERTIES, true);
42     }
43
44     /**
45      * @param propsFile Properties file name.
46      * @param props Properties.
47      * @param throwExc Flag indicating whether to throw an exception or not.
48      */
49     public static void readProperties(String propsFile, Properties props, boolean throwExc) {
50         try (InputStream is = SnookerScoreProperties.class.getClassLoader().getResourceAsStream(propsFile)) {
51             if (is == null) {
52                 if (throwExc)
53                     throw new RuntimeException("Failed to find properties file: " + propsFile);
54                 else
55                     return;
56             }
57
58             props.load(is);
59         }
60         catch (IOException e) {
61             throw new RuntimeException("Failed to read properties file: " + propsFile, e);
62         }
63     }
64
65     /**
66      * Empty string for not found properties.
67      */
68     private static final String EMPTY = "";
69
70     /**
71      * Gets property value.
72      *
73      * @param key Property key.
74      * @return Property value (possibly empty string, but never {@code null}).
75      */
76     public static String get(String key) {
77         return PROPERTIES.getProperty(key, EMPTY);
78     }
79
80     /**
81      * Private constructor.
82      */
83     private SnookerScoreProperties() {
84     }
85
86 }