Performance improvement:
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
index e714b5633a9b5047d4a08df36463bbda05bab2f4..0a405bee47d744e6545b3360579343fdc5466806 100644 (file)
@@ -1,14 +1,10 @@
 package be.nikiroo.jvcard;
 
-import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.security.InvalidParameterException;
-import java.util.LinkedList;
 import java.util.List;
 
 import be.nikiroo.jvcard.parsers.Format;
@@ -26,7 +22,6 @@ public class Card extends BaseClass<Contact> {
        private String name;
        private Format format;
        private long lastModified;
-       private boolean remote;
 
        /**
         * Create a new {@link Card} from the given {@link File} and {@link Format}.
@@ -44,12 +39,10 @@ public class Card extends BaseClass<Contact> {
         *             if format is NULL
         */
        public Card(File file, Format format) throws IOException {
-               this(load(file, format));
+               this(Parser.parseContact(file, format));
 
-               if (file != null) {
-                       if (file.exists()) {
-                               lastModified = file.lastModified();
-                       }
+               if (file != null && file.exists()) {
+                       lastModified = file.lastModified();
                }
 
                this.format = format;
@@ -58,8 +51,7 @@ public class Card extends BaseClass<Contact> {
                        this.file = file;
                        switch (format) {
                        case VCard21:
-                               this.name = file.getName().replaceAll(
-                                               ".[vV][cC][fF]$", "");
+                               this.name = file.getName().replaceAll(".[vV][cC][fF]$", "");
                                break;
                        case Abook:
                        default:
@@ -80,7 +72,7 @@ public class Card extends BaseClass<Contact> {
         * @throws InvalidParameterException
         *             if format is NULL
         */
-       public Card(List<Contact> contacts) throws IOException {
+       public Card(List<Contact> contacts) {
                super(contacts);
 
                lastModified = -1;
@@ -90,7 +82,7 @@ public class Card extends BaseClass<Contact> {
         * Save the {@link Card} to the given {@link File} with the given
         * {@link Format}.
         * 
-        * @param output
+        * @param file
         *            the output to save to
         * @param format
         *            the {@link Format} to use
@@ -100,15 +92,17 @@ public class Card extends BaseClass<Contact> {
         * @throws IOException
         *             in case of IO errors
         */
-       public boolean saveAs(File output, Format format) throws IOException {
-               if (output == null)
+       public boolean saveAs(File file, Format format) throws IOException {
+               if (file == null)
                        return false;
 
-               BufferedWriter writer = new BufferedWriter(new FileWriter(output));
-               writer.append(toString(format));
+               BufferedWriter writer = new BufferedWriter(new FileWriter(file));
+               Parser.write(writer, format, this);
                writer.close();
 
-               if (output.getCanonicalPath().equals(this.file.getCanonicalPath())) {
+               if (this.file != null
+                               && file.getCanonicalPath().equals(this.file.getCanonicalPath())) {
+                       lastModified = file.lastModified();
                        setPristine();
                }
 
@@ -139,22 +133,11 @@ public class Card extends BaseClass<Contact> {
                if (file == null)
                        return false;
 
-               this.replaceListContent(load(file, format));
+               this.replaceListContent(Parser.parseContact(file, format));
+               lastModified = file.lastModified();
                setPristine();
-               return true;
-       }
 
-       /**
-        * 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);
+               return true;
        }
 
        /**
@@ -177,14 +160,23 @@ public class Card extends BaseClass<Contact> {
        }
 
        /**
-        * Return the input which was used to open this {@link Card}.
+        * Return the {@link File} which was used to open this {@link Card}.
         * 
         * @return the input
         */
-       public File getInput() {
+       public File getFile() {
                return file;
        }
 
+       /**
+        * Break the link between this {@link Card} and he {@link File} which was
+        * used to open it if any.
+        */
+       public void unlink() {
+               file = null;
+               lastModified = -1;
+       }
+
        /**
         * Return the date of the last modification for this {@link Card} (or -1 if
         * unknown/new).
@@ -195,61 +187,18 @@ public class Card extends BaseClass<Contact> {
                return lastModified;
        }
 
-       /**
-        * Check if this {@link Card} is remote.
-        * 
-        * @return TRUE if this {@link Card} is remote
-        */
-       public boolean isRemote() {
-               return remote;
-       }
-
-       /**
-        * Set the remote option on this {@link Card}.
-        * 
-        * @param remote
-        *            TRUE if this {@link Card} is remote
-        */
-       public void setRemote(boolean remote) {
-               this.remote = remote;
-       }
-
        @Override
        public String toString() {
-               return toString(Format.VCard21);
+               return "[Card: " + name + "]";
        }
 
-       /**
-        * Load the data from the given {@link File} under the given {@link Format}.
-        * 
-        * @param file
-        *            the input to load from
-        * @param format
-        *            the {@link Format} to load as
-        * 
-        * @return the list of elements
-        * 
-        * @throws IOException
-        *             in case of IO error
-        */
-       private static List<Contact> load(File file, Format format)
-                       throws IOException {
-               List<String> lines = null;
-
-               if (file != null && file.exists()) {
-                       BufferedReader buffer = new BufferedReader(new InputStreamReader(
-                                       new FileInputStream(file), "UTF-8"));
-                       lines = new LinkedList<String>();
-                       for (String line = buffer.readLine(); line != null; line = buffer
-                                       .readLine()) {
-                               lines.add(line);
-                       }
-                       buffer.close();
-               }
-
-               if (lines == null)
-                       return new LinkedList<Contact>();
+       @Override
+       public String getId() {
+               return "" + name;
+       }
 
-               return Parser.parse(lines, format);
+       @Override
+       public String getState() {
+               return ("" + name + format).replace(' ', '_');
        }
 }