From: Roland Haeder Date: Fri, 7 Aug 2015 09:51:31 +0000 (+0200) Subject: Introduced fieldIterator() + all Contact instances are now iterable over their attributes X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d4f6e8da5bd065f28515f68539782d2bab679827;p=jcore.git Introduced fieldIterator() + all Contact instances are now iterable over their attributes Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 2275e32..5f74161 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -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; @@ -188,22 +192,22 @@ public class BaseFrameworkSystem implements FrameworkInterface { private Class 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 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) c.getSuperclass(); } - + // Trace message this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N - + // Return it return c; } @@ -219,13 +223,13 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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 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]); @@ -236,13 +240,13 @@ public class BaseFrameworkSystem implements FrameworkInterface { // 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; } @@ -263,7 +267,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { protected final void abortProgramWithException (final Throwable throwable) { // Log exception ... this.getLogger().catching(throwable); - + // .. and exit System.exit(1); @@ -517,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 @@ -700,7 +705,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Is already set throw new IllegalStateException("called twice"); } - + // Set instance bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N } @@ -746,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 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 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(); + } } diff --git a/src/org/mxchange/jcore/contact/BaseContact.java b/src/org/mxchange/jcore/contact/BaseContact.java index f0bf18b..9085faa 100644 --- a/src/org/mxchange/jcore/contact/BaseContact.java +++ b/src/org/mxchange/jcore/contact/BaseContact.java @@ -17,6 +17,7 @@ package org.mxchange.jcore.contact; import java.text.MessageFormat; +import java.util.Iterator; import java.util.Objects; import org.mxchange.jcore.BaseFrameworkSystem; import org.mxchange.jcore.client.Client; @@ -482,25 +483,25 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { public Object getValueFromColumn (final String columnName) { // Trace message this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); - + // Determine if the given column is boolean if (this.isBooleanField(this, "BaseContact", columnName)) { // Yes, then call other method return this.getBooleanField(this, "BaseContact", columnName); } - + // Convert column name to field name String methodName = this.convertColumnNameToGetterMethod(columnName, false); - + // Debug message this.getLogger().debug(MessageFormat.format("field={0}", methodName)); - + // Get field Object value = this.getField(this, "BaseContact", methodName); - + // Trace message this.getLogger().trace("value=" + value + " - EXIT!"); - + // Return it return value; } @@ -558,29 +559,38 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { public boolean isValueEqual (final String columnName, final boolean bool) { // Trace message this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool)); - + // Convert column name to field name String methodName = this.convertColumnNameToGetterMethod(columnName, true); - + // Debug message this.getLogger().debug(MessageFormat.format("field={0}", methodName)); - + // Init class instance boolean value = this.getBooleanField(this, "BaseContact", methodName); - + // Debug message this.getLogger().debug(MessageFormat.format("value={0}", value)); - + // Compare it boolean isFound = (bool == value); - + // Trace message this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); - + // Return result return isFound; } + /** + * Returns an iterator of all values from this object + * @return An iterator + */ + @Override + public Iterator iterator () { + return this.fieldIterator(this, "BaseContact"); + } + /** * Shows this contact to the user * @@ -590,19 +600,19 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { public void show (final Client client) { // Trace message this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N - + // The client must be set if (client == null) { // Not set throw new NullPointerException("client is null"); } - + // Display name "box" client.displayNameBox(this); - + // Display address "box" client.displayAddressBox(this); - + // Display other data "box" client.displayOtherDataBox(this); } diff --git a/src/org/mxchange/jcore/contact/Contact.java b/src/org/mxchange/jcore/contact/Contact.java index 974db66..e4a90b5 100644 --- a/src/org/mxchange/jcore/contact/Contact.java +++ b/src/org/mxchange/jcore/contact/Contact.java @@ -24,7 +24,7 @@ import org.mxchange.jcore.client.Client; * * @author Roland Haeder */ -public interface Contact extends FrameworkInterface { +public interface Contact extends FrameworkInterface, Iterable { /** * Some "getter" for translated gender of the contact diff --git a/src/org/mxchange/jcore/contact/Gender.java b/src/org/mxchange/jcore/contact/Gender.java index 2c415dd..46e7f49 100644 --- a/src/org/mxchange/jcore/contact/Gender.java +++ b/src/org/mxchange/jcore/contact/Gender.java @@ -153,7 +153,7 @@ public enum Gender { * * @return the databaseValue */ - protected String getDatabaseValue () { + public String getDatabaseValue () { return this.databaseValue; } @@ -162,7 +162,7 @@ public enum Gender { * * @return the messageKey */ - protected String getMessageKey () { + public String getMessageKey () { return this.messageKey; }