Fix version, move list management from Card/Contact/... into BaseClass
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
index e82ce7cc933ebb864a2b7b405875d09865d12cf9..2622266c259a43c328178ae08c0a68b97c45e27a 100644 (file)
@@ -3,11 +3,11 @@ package be.nikiroo.jvcard;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.io.InputStreamReader;
+import java.security.InvalidParameterException;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -21,63 +21,48 @@ import be.nikiroo.jvcard.parsers.Parser;
  * @author niki
  * 
  */
-public class Card {
-       private List<Contact> contacts;
+public class Card extends BaseClass<Contact> {
        private File file;
-       private boolean dirty;
        private String name;
        private Format format;
 
+       /**
+        * Create a new {@link Card} from the given {@link File} and {@link Format}.
+        * 
+        * @param file
+        *            the file containing the {@link Card} data, must not be NULL
+        * @param format
+        *            the {@link Format} to use to parse it
+        * 
+        * @throws IOException
+        *             in case of IO error
+        * @throws NullPointerException
+        *             if file is NULL
+        * @throws InvalidParameterException
+        *             if format is NULL
+        */
        public Card(File file, Format format) throws IOException {
+               super(load(file, format));
+
                this.file = file;
                this.format = format;
-
-               if (file != null) {
-                       name = file.getName();
-               }
-
-               BufferedReader buffer = new BufferedReader(new FileReader(file));
-               List<String> lines = new LinkedList<String>();
-               for (String line = buffer.readLine(); line != null; line = buffer
-                               .readLine()) {
-                       lines.add(line);
-               }
-               buffer.close();
-
-               load(lines, format);
-               dirty = false; // initial load, so no change yet, so no need to call
-                                               // setPristine()
+               this.name = file.getName();
        }
 
        /**
-        * Return the full list of {@link Contact}s. Please use responsibly (this is
-        * the original list, do not modify the list itself).
+        * Save the {@link Card} to the given {@link File} with the given
+        * {@link Format}.
         * 
-        * @return the list of {@link Contact}s
-        */
-       public List<Contact> getContactsList() {
-               return contacts;
-       }
-
-       /**
-        * Return the list of {@link Contact}s. Note that this list is a copy.
+        * @param file
+        *            the {@link File} to save to
+        * @param format
+        *            the {@link Format} to use
         * 
-        * @return the list of {@link Contact}s
+        * @return TRUE if it was saved
+        * 
+        * @throws IOException
+        *             in case of IO errors
         */
-       public List<Contact> getContacts() {
-               ArrayList<Contact> list = new ArrayList<Contact>(size());
-               list.addAll(contacts);
-               return list;
-       }
-
-       public int size() {
-               return contacts.size();
-       }
-
-       public Contact get(int index) {
-               return contacts.get(index);
-       }
-
        public boolean saveAs(File file, Format format) throws IOException {
                if (file == null)
                        return false;
@@ -93,39 +78,34 @@ public class Card {
                return true;
        }
 
+       /**
+        * Save the {@link Card} to the original {@link File} it was open from.
+        * 
+        * @return TRUE if it was saved
+        * 
+        * @throws IOException
+        *             in case of IO errors
+        */
        public boolean save() throws IOException {
                return saveAs(file, format);
        }
 
+       /**
+        * Return a {@link String} representation of this {@link Card} in the given
+        * {@link Format}.
+        * 
+        * @param format
+        *            the {@link Format} to use
+        * 
+        * @return the {@link String}
+        */
        public String toString(Format format) {
                return Parser.toString(this, format);
        }
 
-       public String toString() {
-               return toString(Format.VCard21);
-       }
-
-       protected void load(String serializedContent, Format format) {
-               // note: fixed size array
-               List<String> lines = Arrays.asList(serializedContent.split("\n"));
-               load(lines, format);
-       }
-
-       protected void load(List<String> lines, Format format) {
-               this.contacts = Parser.parse(lines, format);
-               setDirty();
-
-               for (Contact contact : contacts) {
-                       contact.setParent(this);
-               }
-       }
-
-       public boolean isDirty() {
-               return dirty;
-       }
-
        /**
-        * Return the name of this card.
+        * Return the name of this card (the name of the {@link File} which it was
+        * opened from).
         * 
         * @return the name
         */
@@ -133,21 +113,34 @@ public class Card {
                return name;
        }
 
-       /**
-        * Notify that this element has unsaved changes.
-        */
-       void setDirty() {
-               dirty = true;
+       @Override
+       public String toString() {
+               return toString(Format.VCard21);
        }
 
        /**
-        * Notify this element <i>and all its descendants</i> that it is in pristine
-        * state (as opposed to dirty).
+        * Load the data from the given {@link File} under the given {@link Format}.
+        * 
+        * @param file
+        *            the {@link File} to load from
+        * @param format
+        *            the {@link Format} to load as
+        * 
+        * @return the list of elements
+        * @throws IOException
+        *             in case of IO error
         */
-       void setPristine() {
-               dirty = false;
-               for (Contact contact : contacts) {
-                       contact.setPristine();
+       static private List<Contact> load(File file, Format format)
+                       throws IOException {
+               BufferedReader buffer = new BufferedReader(new InputStreamReader(
+                               new FileInputStream(file), "UTF-8"));
+               List<String> lines = new LinkedList<String>();
+               for (String line = buffer.readLine(); line != null; line = buffer
+                               .readLine()) {
+                       lines.add(line);
                }
+               buffer.close();
+
+               return Parser.parse(lines, format);
        }
 }