[LIB-9] Output of application results to console
authorgotty <gotty@hedgecode.org>
Mon, 29 Apr 2019 14:44:17 +0000 (17:44 +0300)
committergotty <gotty@hedgecode.org>
Mon, 29 Apr 2019 14:44:17 +0000 (17:44 +0300)
chesshog-core/src/main/java/org/hedgecode/chess/ChessHogApp.java
chesshog-core/src/main/java/org/hedgecode/chess/ChessHogConsole.java [new file with mode: 0644]
chesshog-core/src/main/java/org/hedgecode/chess/ChessHogProperties.java [new file with mode: 0644]
chesshog-core/src/main/resources/chesshog.properties [new file with mode: 0644]

index 94dfaa0..0a4f3dc 100644 (file)
@@ -25,6 +25,7 @@ public final class ChessHogApp {
 
     public static void main(String[] args) {
 
+        ChessHogConsole.init();
 
     }
 
diff --git a/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogConsole.java b/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogConsole.java
new file mode 100644 (file)
index 0000000..32da115
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Calendar;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class ChessHogConsole {
+
+    private static final String EMPTY = "";
+    private static final String SPACE = " ";
+
+    private static final String[] ASCII_LOGO = {
+            "  _______     _                   _                  ",
+            " |   |  _|_ _| |___ ___ ___ ___ _| |___              ",
+            " |     | -_| . | . | -_|  _| . | . | -_|             ",
+            " |     |___|___|_  |___|___|___|___|___|             ",
+            " |___|___|     |___|    _               _            ",
+            "                    ___| |_ ___ ___ ___| |_ ___ ___  ",
+            "                   |  _|   | -_|_ -|_ -|   | . | . | ",
+            "                   |___|_|_|___|___|___|_|_|___|_  | ",
+            "                                               |___| "
+    };
+
+    private static final String COPYRIGHT = "Copyright (c) Hedgecode";
+
+    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
+    private static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
+
+    private static boolean logMode = false;
+
+    private ChessHogConsole() {
+    }
+
+    public static void setLogMode(boolean isLogMode) {
+        logMode = isLogMode;
+    }
+
+    public static void console(String message) {
+        if (logMode) {
+            log(message);
+        } else {
+            output(message);
+        }
+    }
+
+    public static void output(String message) {
+        System.out.println(
+                message
+        );
+    }
+
+    public static void log(String message) {
+        System.out.println(
+                logTime() + message
+        );
+    }
+
+    public static void init() {
+        asciiLogo();
+        copyright();
+        version();
+        console(EMPTY);
+        os();
+        jave();
+    }
+
+
+    private static String logTime() {
+        return "[" + TIME_FORMAT.format(LocalDateTime.now()) + "]" + SPACE;
+    }
+
+    private static void asciiLogo() {
+        for (String line : ASCII_LOGO) {
+            console(line);
+        }
+    }
+
+    private static void copyright() {
+        int inceptionYear = Integer.parseInt(
+                ChessHogProperties.get("chesshog.inception.year")
+        );
+        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
+        String years = currentYear > inceptionYear
+                ? inceptionYear + "-" + currentYear + SPACE
+                : inceptionYear + SPACE;
+        console(
+                alignCenter(years + COPYRIGHT)
+        );
+    }
+
+    private static void version() {
+        console(
+                alignCenter("v. " + ChessHogProperties.get("chesshog.version"))
+        );
+    }
+
+    private static void os() {
+        console(
+                "OS:" +
+                        SPACE + System.getProperty("os.name") +
+                        SPACE + System.getProperty("os.version") +
+                        SPACE + System.getProperty("os.arch")
+        );
+    }
+
+    private static void jave() {
+        console(
+                "JRE:" +
+                        SPACE + System.getProperty("java.runtime.name") +
+                        SPACE + System.getProperty("java.runtime.version")
+        );
+        console(
+                "JVM:" +
+                        SPACE + System.getProperty("java.vm.vendor") +
+                        SPACE + System.getProperty("java.vm.name") +
+                        SPACE + System.getProperty("java.vm.version")
+        );
+    }
+
+    private static String alignCenter(String message) {
+        int indent = (ASCII_LOGO[0].length() + message.length()) / 2;
+        return String.format("%" + indent + "s", message);
+    }
+
+}
diff --git a/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogProperties.java b/chesshog-core/src/main/java/org/hedgecode/chess/ChessHogProperties.java
new file mode 100644 (file)
index 0000000..0642890
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * ChessHog properties holder.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class ChessHogProperties {
+
+    /** Properties file. */
+    private static final String PROPERTIES_FILE = "chesshog.properties";
+
+    /** Properties. */
+    private static final Properties PROPERTIES;
+
+    /**
+     * Static properties initialization.
+     */
+    static {
+        PROPERTIES = new Properties();
+        readProperties(PROPERTIES_FILE, PROPERTIES, true);
+    }
+
+    /**
+     * @param propsFile Properties file name.
+     * @param props Properties.
+     * @param throwExc Flag indicating whether to throw an exception or not.
+     */
+    public static void readProperties(String propsFile, Properties props, boolean throwExc) {
+        try (InputStream is = ChessHogProperties.class.getClassLoader().getResourceAsStream(propsFile)) {
+            if (is == null) {
+                if (throwExc)
+                    throw new RuntimeException("Failed to find properties file: " + propsFile);
+                else
+                    return;
+            }
+
+            props.load(is);
+        }
+        catch (IOException e) {
+            throw new RuntimeException("Failed to read properties file: " + propsFile, e);
+        }
+    }
+
+    /**
+     * Empty string for not found properties.
+     */
+    private static final String EMPTY = "";
+
+    /**
+     * Gets property value.
+     *
+     * @param key Property key.
+     * @return Property value (possibly empty string, but never {@code null}).
+     */
+    public static String get(String key) {
+        return PROPERTIES.getProperty(key, EMPTY);
+    }
+
+    /**
+     * Private constructor.
+     */
+    private ChessHogProperties() {
+    }
+
+}
diff --git a/chesshog-core/src/main/resources/chesshog.properties b/chesshog-core/src/main/resources/chesshog.properties
new file mode 100644 (file)
index 0000000..3947321
--- /dev/null
@@ -0,0 +1,22 @@
+#
+# Copyright (c) 2018-2019. Developed by Hedgecode.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+chesshog.version=0.1-SNAPSHOT
+chesshog.build=0
+chesshog.revision=DEV
+chesshog.inception.year=2018
+
+