From 69575ff4e4b0226856a5afe6464fbc1bea30195f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 12 Nov 2022 21:22:58 +0100 Subject: [PATCH] Continued: - moved utilities classes to own class - added constructor with facade to avoid setter invocation in other constructors --- .../mxchange/jcore/BaseFrameworkSystem.java | 121 +++++++----------- .../mxchange/jcore/utils/FrameworkUtils.java | 80 ++++++++++++ 2 files changed, 125 insertions(+), 76 deletions(-) create mode 100644 src/org/mxchange/jcore/utils/FrameworkUtils.java diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 1bf15c1..55547f5 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -17,7 +17,6 @@ package org.mxchange.jcore; import java.util.ResourceBundle; -import java.util.StringTokenizer; import org.mxchange.jcore.application.Application; import org.mxchange.jcore.client.Client; import org.mxchange.jcore.facade.Facade; @@ -88,38 +87,56 @@ public abstract class BaseFrameworkSystem implements FrameworkInterface { protected BaseFrameworkSystem () { } - @Override - public Application getApplication () { - return this.application; + /** + * Constructor with a facade + *

+ * @param facade Facade instance + */ + protected BaseFrameworkSystem (final Facade facade) { + // Invole other constructor + this(); + + // Check parameter + if (null == facade) { + // Throw NPE + throw new NullPointerException("facade is null"); //NOI18N + } + + // Set instance + this.facade = facade; } @Override - public Facade getFacade () { - return this.facade; + public Application getApplication () { + return this.application; } @Override - public String getMessageStringFromKey (final String key) { - // Return message - return this.getBundle().getString(key); + public Client getClient () { + return this.client; } /** - * Getter for bundle instance + * Client instance *

- * @return Resource bundle + * @param client the client to set */ - protected ResourceBundle getBundle () { - return BaseFrameworkSystem.bundle; + protected void setClient (final Client client) { + this.client = client; } /** - * Setter for bundle instance + * Application instance *

- * @param bundle the bundle to set + * @param application the application to set */ - protected static void setBundle (final ResourceBundle bundle) { - BaseFrameworkSystem.bundle = bundle; + protected void setApplication (final Application application) { + this.application = application; + } + + @Override + public Facade getFacade () { + return this.facade; } /** @@ -131,76 +148,28 @@ public abstract class BaseFrameworkSystem implements FrameworkInterface { this.facade = facade; } - /** - * Application instance - *

- * @param application the application to set - */ - protected void setApplication (final Application application) { - this.application = application; - } - @Override - public Client getClient () { - return this.client; + public String getMessageStringFromKey (final String key) { + // Return message + return this.getBundle().getString(key); } /** - * Client instance + * Getter for bundle instance *

- * @param client the client to set + * @return Resource bundle */ - protected void setClient (final Client client) { - this.client = client; - } - - /** - * Converts null to empty string or leaves original object untouched. - *

- * @param object Any string - *

- * @return Empty string if null or original string - * @TODO: Move to own utility class - */ - protected Object convertNullToEmpty (final Object object) { - // Is it null? - if (null == object) { - // Return empty string - return ""; //NOI18N - } - - // Return it - return object; + protected ResourceBundle getBundle () { + return BaseFrameworkSystem.bundle; } /** - * Some "getter" for an array from given string and tokenizer - *

- * @param str String to tokenize and get array from - * @param delimiter Delimiter + * Setter for bundle instance *

- * @return Array from tokenized string - * @TODO: Move to own utility class + * @param bundle the bundle to set */ - protected String[] getArrayFromString (final String str, final String delimiter) { - // Get tokenizer - StringTokenizer tokenizer = new StringTokenizer(str, delimiter); - - // Init array and index - String[] tokens = new String[tokenizer.countTokens()]; - int index = 0; - - // Run through all tokens - while (tokenizer.hasMoreTokens()) { - // Get current token and add it - tokens[index] = tokenizer.nextToken(); - - // Increment index - index++; - } - - // Return it - return tokens; + protected static void setBundle (final ResourceBundle bundle) { + BaseFrameworkSystem.bundle = bundle; } /** diff --git a/src/org/mxchange/jcore/utils/FrameworkUtils.java b/src/org/mxchange/jcore/utils/FrameworkUtils.java new file mode 100644 index 0000000..aef9ac0 --- /dev/null +++ b/src/org/mxchange/jcore/utils/FrameworkUtils.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcore.utils; + +import java.util.StringTokenizer; + +/** + * An utilities class for this framework + *

+ * @author Roland Häder + */ +public class FrameworkUtils { + + /** + * Converts null to empty string or leaves original object untouched. + *

+ * @param object Any string + *

+ * @return Empty string if null or original string + */ + public static Object convertNullToEmpty (final Object object) { + // Is it null? + if (null == object) { + // Return empty string + return ""; //NOI18N + } + + // Return it + return object; + } + + /** + * Some "getter" for an array from given string and tokenizer + *

+ * @param str String to tokenize and get array from + * @param delimiter Delimiter + *

+ * @return Array from tokenized string + */ + public static String[] getArrayFromString (final String str, final String delimiter) { + // Get tokenizer + StringTokenizer tokenizer = new StringTokenizer(str, delimiter); + + // Init array and index + String[] tokens = new String[tokenizer.countTokens()]; + int index = 0; + + // Run through all tokens + while (tokenizer.hasMoreTokens()) { + // Get current token and add it + tokens[index] = tokenizer.nextToken(); + // Increment index + index++; + } + + // Return it + return tokens; + } + + /** + * No instances from utility classes + */ + private FrameworkUtils () { + } + +} -- 2.39.5