From f2226c632515f9fbb66994903f0673bf2946c94b Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 31 Jul 2015 10:47:20 +0200 Subject: [PATCH] =?utf8?q?Broken=20commit=20towards=20generics=20Signed-of?= =?utf8?q?f-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../database/backend/DatabaseBackend.java | 5 +- .../backend/csv/Base64CsvDatabaseBackend.java | 153 +++++++++--------- .../backend/mysql/MySqlDatabaseBackend.java | 12 +- .../contact/ContactDatabaseFrontend.java | 2 +- 4 files changed, 82 insertions(+), 90 deletions(-) diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java index 70cfd81..dfcf583 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.sql.SQLException; import java.util.Iterator; import org.mxchange.addressbook.FrameworkInterface; -import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.database.storage.Storeable; import org.mxchange.addressbook.exceptions.BadTokenException; @@ -86,8 +85,6 @@ public interface DatabaseBackend extends FrameworkInterface { * * @return Iterator for contacts * @throws org.mxchange.addressbook.exceptions.BadTokenException If the CSV token is badly formulated - * @deprecated The method is to much "contact" specific */ - @Deprecated - public Iterator contactIterator () throws BadTokenException; + public Iterator iterator () throws BadTokenException; } diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java index 12377c6..c0d8460 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java @@ -27,6 +27,7 @@ import java.util.Base64; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; +import org.mxchange.addressbook.FrameworkInterface; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.contact.Gender; import org.mxchange.addressbook.contact.book.BookContact; @@ -88,35 +89,6 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat // Empty body } - /** - * Gets an iterator for contacts - * - * @return Iterator for contacts - * @throws org.mxchange.addressbook.exceptions.BadTokenException If the - * underlaying method has found an invalid token - */ - @Override - @Deprecated - public Iterator contactIterator () throws BadTokenException { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - /* - * Then read the file into RAM (yes, not perfect for >1000 entries ...) - * and get a List back. - */ - List list = this.readContactList(); - - // List must be set - assert (list instanceof List) : "list has not been set."; //NOI18N - - // Trace message - this.getLogger().trace(MessageFormat.format("list.iterator()={0} - EXIT!", list.iterator())); //NOI18N - - // Get iterator from list and return it - return list.iterator(); - } - /** * Shuts down this backend */ @@ -150,7 +122,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat try { // Do a deprecated call // @todo this needs rewrite! - return this.readContactList().size(); + return this.readList().size(); } catch (final BadTokenException ex) { this.abortProgramWithException(ex); } @@ -224,6 +196,33 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat return isFound; } + /** + * Gets an iterator for contacts + * + * @return Iterator for contacts + * @throws org.mxchange.addressbook.exceptions.BadTokenException If the underlaying method has found an invalid token + */ + @Override + public Iterator iterator () throws BadTokenException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + /* + * Then read the file into RAM (yes, not perfect for >1000 entries ...) + * and get a List back. + */ + List list = this.readList(); + + // List must be set + assert (list instanceof List) : "list has not been set."; //NOI18N + + // Trace message + this.getLogger().trace(MessageFormat.format("list.iterator()={0} - EXIT!", list.iterator())); //NOI18N + + // Get iterator from list and return it + return list.iterator(); + } + /** * Get length of underlaying file * @@ -309,15 +308,15 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat /** * Adds given contact to list * - * @param contact Contact instance to add + * @param instance An instance of FrameworkInterface to add * @param list List instance */ - private void addContactToList (final Contact contact, final List list) { + private void addToList (final Storeable instance, final List list) { // Trace message - this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N + this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", instance)); //NOI18N // No null here - if (contact == null) { + if (instance == null) { // Throw exception throw new NullPointerException("contact is null"); //NOI18N } else if (list == null) { @@ -326,15 +325,15 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat } // Debug message - this.getLogger().debug(MessageFormat.format("contact={0}", contact)); //NOI18N + this.getLogger().debug(MessageFormat.format("contact={0}", instance)); //NOI18N // Is the contact read? - if (contact instanceof Contact) { + if (instance instanceof FrameworkInterface) { // Then add it - boolean added = list.add(contact); + boolean added = list.add(instance); // Debug message - this.getLogger().debug(MessageFormat.format("contact={0} added={1}", contact, added)); //NOI18N + this.getLogger().debug(MessageFormat.format("contact={0} added={1}", instance, added)); //NOI18N // Has it been added? if (!added) { @@ -624,15 +623,45 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat return contact; } + /** + * Reads a line from file base + * + * @return Read line from file + */ + private String readLine () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Init input + String input = null; + + try { + // Read single line + String base64 = this.getStorageFile().readLine(); + + // Decode BASE-64 + byte[] decoded = Base64.getDecoder().decode(base64); + + // Convert to string + input = new String(decoded); + } catch (final IOException ex) { + this.getLogger().catching(ex); + } + + // Trace message + this.getLogger().trace(MessageFormat.format("input={0} - EXIT!", input)); //NOI18N + + // Return read string or null + return input; + } + /** * Reads the database file, if available, and adds all read lines into the * list. * * @return A list with Contact instances - * @deprecated Is to much "contacts" specific */ - @Deprecated - private List readContactList () throws BadTokenException { + private List readList () throws BadTokenException { this.getLogger().trace("CALLED!"); //NOI18N // First rewind @@ -646,11 +675,11 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat // Instance list // @TODO The maximum length could be guessed from file size? - List list = new ArrayList<>(lines); + List list = new ArrayList<>(lines); // Init variables String line; - Contact contact = null; + Storeable instance = null; // Read all lines while (!this.isEndOfFile()) { @@ -658,49 +687,17 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat line = this.readLine(); // Parse line - contact = this.parseLineToContact(line); + instance = (Storeable) this.parseLineToContact(line); // The contact instance should be there now - assert (contact instanceof Contact) : MessageFormat.format("contact is not set: {0}", contact); //NOI18N + assert (instance instanceof FrameworkInterface) : MessageFormat.format("instance is not set: {0}", instance); //NOI18N // Add contact - this.addContactToList(contact, list); + this.addToList(instance, list); } // Return finished list this.getLogger().trace(MessageFormat.format("list.size()={0} : EXIT!", list.size())); //NOI18N return list; } - - /** - * Reads a line from file base - * - * @return Read line from file - */ - private String readLine () { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - // Init input - String input = null; - - try { - // Read single line - String base64 = this.getStorageFile().readLine(); - - // Decode BASE-64 - byte[] decoded = Base64.getDecoder().decode(base64); - - // Convert to string - input = new String(decoded); - } catch (final IOException ex) { - this.getLogger().catching(ex); - } - - // Trace message - this.getLogger().trace(MessageFormat.format("input={0} - EXIT!", input)); //NOI18N - - // Return read string or null - return input; - } } diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java index d95afb3..abc3e9b 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java @@ -24,7 +24,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Iterator; -import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.database.backend.BaseDatabaseBackend; import org.mxchange.addressbook.database.backend.DatabaseBackend; import org.mxchange.addressbook.database.storage.Storeable; @@ -107,12 +106,6 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas this.getLogger().trace("EXIT!"); //NOI18N } - @Override - @Deprecated - public Iterator contactIterator () throws BadTokenException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - @Override public void doShutdown () { // This should not happen: @@ -179,6 +172,11 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas } throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } + + @Override + public Iterator iterator () throws BadTokenException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } @Override public long length () { diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java index dc5cdb0..9731d49 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java @@ -178,7 +178,7 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con boolean isFound = false; // Start iteration - Iterator iterator = this.getBackend().contactIterator(); + Iterator iterator = this.getBackend().iterator(); // Check all entries while (iterator.hasNext()) { -- 2.39.2