]> git.mxchange.org Git - jcore.git/blobdiff - src/org/mxchange/jcore/BaseFrameworkSystem.java
Introduced fieldIterator() + all Contact instances are now iterable over their attributes
[jcore.git] / src / org / mxchange / jcore / BaseFrameworkSystem.java
index 606361149e3378da8c176474d5a18898e7a905f5..5f74161b26bd28c790a5ff9cada55dead595c118 100644 (file)
@@ -22,10 +22,14 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.text.MessageFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
@@ -33,6 +37,7 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.mxchange.jcore.application.Application;
 import org.mxchange.jcore.client.Client;
+import org.mxchange.jcore.contact.Contact;
 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
 import org.mxchange.jcore.manager.Manageable;
 
@@ -42,12 +47,21 @@ import org.mxchange.jcore.manager.Manageable;
  * @author Roland Haeder
  */
 public class BaseFrameworkSystem implements FrameworkInterface {
+       /**
+        * Bundle instance
+        */
+       private static ResourceBundle bundle;
 
        /**
         * Instance for own properties
         */
        private static final Properties properties = new Properties(System.getProperties());
 
+       /**
+        * Self instance
+        */
+       private static FrameworkInterface selfInstance;
+
        /**
         * Class' logger
         */
@@ -58,16 +72,17 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         */
        private Application application;
 
-       /**
-        * Bundle instance
-        */
-       private final ResourceBundle bundle;
 
        /**
         * Client instance
         */
        private Client client;
 
+       /**
+        * Contact instance
+        */
+       private Contact contact;
+
        /**
         * Manager instance
         */
@@ -83,12 +98,22 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         */
        private DatabaseFrontend wrapper;
 
+
        /**
         * Initialize object
         */
        {
                LOG = LogManager.getLogger(this);
-               bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
+       }
+
+       /**
+        * Getter for this application
+        *
+        * @return Instance from this application
+        */
+       public static final FrameworkInterface getInstance () {
+               // Return it
+               return selfInstance;
        }
 
        /**
@@ -97,6 +122,9 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        protected BaseFrameworkSystem () {
                // Init properties file
                this.initProperties();
+
+               // Set own instance
+               this.setSelfInstance();
        }
 
        /**
@@ -109,6 +137,151 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                return this.application;
        }
 
+       /**
+        * Getter for logger
+        *
+        * @return Logger
+        */
+       @Override
+       public final Logger getLogger () {
+               return this.LOG;
+       }
+
+       /**
+        * Manager instance
+        *
+        * @return the contactManager
+        */
+       @Override
+       public final Manageable getManager () {
+               return this.manager;
+       }
+
+       /**
+        * Getter for human-readable string from given key
+        *
+        * @param key Key to return
+        * @return Human-readable message
+        */
+       @Override
+       public final String getMessageStringFromKey (final String key) {
+               // Return message
+               return this.getBundle().getString(key);
+       }
+
+       /**
+        * Some "getter for a value from given column name. This name will be
+        * translated into a method name and then this method is called.
+        *
+        * @param columnName Column name
+        * @return Value from field
+        */
+       @Override
+       public Object getValueFromColumn (final String columnName) {
+               throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0}", columnName)); //NOI18N
+       }
+
+       /**
+        * Some "getter" for target class instance from given name.
+        *
+        * @param instance Instance to iterate on
+        * @param targetClass Class name to look for
+        * @return Class instance
+        */
+       @SuppressWarnings ("unchecked")
+       private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
+               // Trace message
+               this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
+
+               // Instance reflaction of this class
+               Class<? extends FrameworkInterface> c = instance.getClass();
+
+               // Analyze class
+               while (!targetClass.equals(c.getSimpleName())) {
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
+
+                       // Get super class (causes unchecked warning)
+                       c = (Class<? extends FrameworkInterface>) c.getSuperclass();
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
+
+               // Return it
+               return c;
+       }
+
+       /**
+        * Some "getter" for a Method instance from given method name
+        *
+        * @param instance Actual instance to call
+        * @param targetClass Target class name
+        * @param methodName Method name
+        * @return A Method instance
+        */
+       private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) {
+               // Trace messahe
+               this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
+
+               // Get target class instance
+               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
+
+               // Init field instance
+               Method method = null;
+
+               // Use reflection to get all attributes
+               try {
+                       method = c.getDeclaredMethod(methodName, new Class<?>[0]);
+               } catch (final SecurityException ex) {
+                       // Security problem
+                       this.abortProgramWithException(ex);
+               } catch (final NoSuchMethodException ex) {
+                       // Method not found
+                       this.abortProgramWithException(ex);
+               }
+
+               // Assert on field
+               assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
+
+               // Return it
+               return method;
+       }
+
+       /**
+        * Setter for self instance
+        */
+       private void setSelfInstance () {
+               // Need to set it here
+               selfInstance = this;
+       }
+
+       /**
+        * Aborts program with given exception
+        *
+        * @param       throwable Any type of Throwable
+        */
+       protected final void abortProgramWithException (final Throwable throwable) {
+               // Log exception ...
+               this.getLogger().catching(throwable);
+
+               // .. and exit
+               System.exit(1);
+               
+       }
+
+       /**
+        * Application instance
+        *
+        * @param application the application to set
+        */
+       protected final void setApplication (final Application application) {
+               this.application = application;
+       }
+
        /**
         * Client instance
         *
@@ -119,6 +292,24 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                return this.client;
        }
 
+       /**
+        * Getter for bundle instance
+        *
+        * @return Resource bundle
+        */
+       protected final ResourceBundle getBundle () {
+               return BaseFrameworkSystem.bundle;
+       }
+
+       /**
+        * Client instance
+        *
+        * @param client the client to set
+        */
+       protected final void setClient (final Client client) {
+               this.client = client;
+       }
+
        /**
         * Checks if given boolean field is available and set to same value
         *
@@ -330,6 +521,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @param delimiter Delimiter
         * @param size Size of array
         * @return Array from tokenized string
+        * @todo Get rid of size parameter
         */
        protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
                // Trace message
@@ -396,161 +588,6 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                return value;
        }
 
-       /**
-        * Client instance
-        *
-        * @param client the client to set
-        */
-       protected final void setClient (final Client client) {
-               this.client = client;
-       }
-
-       /**
-        * Application instance
-        *
-        * @param application the application to set
-        */
-       protected final void setApplication (final Application application) {
-               this.application = application;
-       }
-
-       /**
-        * Getter for logger
-        *
-        * @return Logger
-        */
-       @Override
-       public final Logger getLogger () {
-               return this.LOG;
-       }
-
-       /**
-        * Manager instance
-        *
-        * @return the contactManager
-        */
-       @Override
-       public final Manageable getManager () {
-               return this.manager;
-       }
-
-       /**
-        * Getter for human-readable string from given key
-        *
-        * @param key Key to return
-        * @return Human-readable message
-        */
-       @Override
-       public final String getMessageStringFromKey (final String key) {
-               // Return message
-               return this.getBundle().getString(key);
-       }
-
-       /**
-        * Some "getter for a value from given column name. This name will be
-        * translated into a method name and then this method is called.
-        *
-        * @param columnName Column name
-        * @return Value from field
-        */
-       @Override
-       public Object getValueFromColumn (final String columnName) {
-               throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0}", columnName)); //NOI18N
-       }
-
-       /**
-        * Some "getter" for target class instance from given name.
-        *
-        * @param instance Instance to iterate on
-        * @param targetClass Class name to look for
-        * @return Class instance
-        */
-       @SuppressWarnings ("unchecked")
-       private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
-               // Trace message
-               this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
-               
-               // Instance reflaction of this class
-               Class<? extends FrameworkInterface> c = instance.getClass();
-               
-               // Analyze class
-               while (!targetClass.equals(c.getSimpleName())) {
-                       // Debug message
-                       this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
-                       
-                       // Get super class (causes unchecked warning)
-                       c = (Class<? extends FrameworkInterface>) c.getSuperclass();
-               }
-               
-               // Trace message
-               this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
-               
-               // Return it
-               return c;
-       }
-
-       /**
-        * Some "getter" for a Method instance from given method name
-        *
-        * @param instance Actual instance to call
-        * @param targetClass Target class name
-        * @param methodName Method name
-        * @return A Method instance
-        */
-       private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) {
-               // Trace messahe
-               this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
-               
-               // Get target class instance
-               Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
-               
-               // Init field instance
-               Method method = null;
-               
-               // Use reflection to get all attributes
-               try {
-                       method = c.getDeclaredMethod(methodName, new Class<?>[0]);
-               } catch (final SecurityException ex) {
-                       // Security problem
-                       this.abortProgramWithException(ex);
-               } catch (final NoSuchMethodException ex) {
-                       // Method not found
-                       this.abortProgramWithException(ex);
-               }
-               
-               // Assert on field
-               assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
-               
-               // Trace message
-               this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
-               
-               // Return it
-               return method;
-       }
-
-       /**
-        * Aborts program with given exception
-        *
-        * @param       throwable Any type of Throwable
-        */
-       protected final void abortProgramWithException (final Throwable throwable) {
-               // Log exception ...
-               this.getLogger().catching(throwable);
-               
-               // .. and exit
-               System.exit(1);
-               
-       }
-
-       /**
-        * Getter for bundle instance
-        *
-        * @return Resource bundle
-        */
-       protected final ResourceBundle getBundle () {
-               return this.bundle;
-       }
-
        /**
         * Manager instance
         *
@@ -602,7 +639,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * @return Propety value
         */
        protected final String getProperty (final String key) {
-               return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.addressbook.%s", key)); //NOI18N
+               return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
        }
 
        /**
@@ -628,7 +665,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         *
         * @return DatabaseFrontend instance
         */
-       protected DatabaseFrontend getWrapper () {
+       protected final DatabaseFrontend getWrapper () {
                return this.wrapper;
        }
 
@@ -637,10 +674,42 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         *
         * @param wrapper A DatabaseFrontend instance
         */
-       protected void setWrapper (final DatabaseFrontend wrapper) {
+       protected final void setWrapper (final DatabaseFrontend wrapper) {
                this.wrapper = wrapper;
        }
 
+       /**
+        * Getter for Contact instance
+        *
+        * @return Contact instance
+        */
+       protected final Contact getContact () {
+               return this.contact;
+       }
+
+       /**
+        * Setter for Contact instance
+        *
+        * @param contact A Contact instance
+        */
+       protected final void setContact (final Contact contact) {
+               this.contact = contact;
+       }
+
+       /**
+        * Initializes i18n bundles
+        */
+       protected void initBundle () {
+               // Is the bundle set?
+               if (bundle instanceof ResourceBundle) {
+                       // Is already set
+                       throw new IllegalStateException("called twice");
+               }
+
+               // Set instance
+               bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
+       }
+
        /**
         * Checks whether the given field is a boolean field by probing it.
         * 
@@ -682,4 +751,38 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Return result
                return isBool;
        }
+
+       /**
+        * Creates an iterator from given instance and class name. The class name
+        * is required in getValueFromColumn() to make a proper call.
+        * 
+        * @param instance Instance to run getter calls on
+        * @param className Class name to iterate over
+        * @return An iterator over all object's fields
+        */
+       protected Iterator<Object> fieldIterator (final FrameworkInterface instance, final String className) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className));
+
+               // Get all attributes from given instance
+               Field[] fields = instance.getClass().getDeclaredFields();
+
+               // A list is fine
+               List<Object> list = new ArrayList<>(fields.length);
+
+               // Walk through all
+               for (final Field field : fields) {
+                       // Get value from it
+                       Object value = this.getValueFromColumn(field.getName());
+
+                       // Add it to list
+                       assert(list.add(value)) : MessageFormat.format("value {0} has not been added", value);
+               }
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", list.size()));
+
+               // Return list iterator
+               return list.iterator();
+       }
 }