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;
// 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<Contact> 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<Contact> 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
*/
try {
// Do a deprecated call
// @todo this needs rewrite!
- return this.readContactList().size();
+ return this.readList().size();
} catch (final BadTokenException ex) {
this.abortProgramWithException(ex);
}
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<? extends Storeable> 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<Storeable> 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
*
/**
* 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<Contact> list) {
+ private void addToList (final Storeable instance, final List<Storeable> 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) {
}
// 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) {
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<Contact> readContactList () throws BadTokenException {
+ private List<? extends Storeable> readList () throws BadTokenException {
this.getLogger().trace("CALLED!"); //NOI18N
// First rewind
// Instance list
// @TODO The maximum length could be guessed from file size?
- List<Contact> list = new ArrayList<>(lines);
+ List<Storeable> list = new ArrayList<>(lines);
// Init variables
String line;
- Contact contact = null;
+ Storeable instance = null;
// Read all lines
while (!this.isEndOfFile()) {
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;
- }
}