jDoc, possible leak on crash, new depth option
[jvcard.git] / src / be / nikiroo / jvcard / Data.java
index d6ba628d3353fa2f1b22d0f1440790a06c268631..0484adef9c5b339d270c7b8ad6256a80625299d3 100644 (file)
@@ -5,19 +5,21 @@ import java.util.LinkedList;
 import java.util.List;
 
 /**
- * A data is a piece of information present in a {@link Contact}. It is
+ * A {@link Data} is a piece of information present in a {@link Contact}. It is
  * basically a key/value pair with optional types and an optional group name.
+ * <p>
+ * A {@link Data} can also be binary encoded: in this case, it has an associated
+ * BKey number to identify it.
  * 
  * @author niki
- *
  */
 public class Data extends BaseClass<TypeInfo> {
        public enum DataPart {
                FN_FAMILY, FN_GIVEN, FN_ADDITIONAL, // Name
                FN_PRE, FN_POST, // Pre/Post
                BDAY_YYYY, BDAY_MM, BDAY_DD, // BDay
+               // Address:
                ADR_PBOX, ADR_EXTENDED, ADR_STREET, ADR_CITY, ADR_REGION, ADR_POSTAL_CODE, ADR_COUNTRY
-               // Address
        }
 
        private String name;
@@ -71,25 +73,33 @@ public class Data extends BaseClass<TypeInfo> {
        public String getValue() {
                return unescape(value);
        }
-       
+
        /**
-        * Return the RAW value of this {@link Data}
+        * Change the value of this {@link Data}
         * 
-        * @return the RAW value
+        * @param value
+        *            the new value
+        */
+       public void setValue(String value) {
+               setRawValue(escape(value));
+       }
+
+       /**
+        * Return the raw value of this {@link Data}
+        * 
+        * @return the raw value
         */
        public String getRawValue() {
                return value;
        }
 
        /**
-        * Change the value of this {@link Data}
+        * Change the raw value of this {@link Data}
         * 
         * @param value
-        *            the new value
+        *            the new raw value
         */
-       public void setValue(String value) {
-               value = escape(value);
-
+       public void setRawValue(String value) {
                if ((value == null && this.value != null)
                                || (value != null && !value.equals(this.value))) {
                        this.value = value;
@@ -161,15 +171,18 @@ public class Data extends BaseClass<TypeInfo> {
 
        /**
         * Return the bkey number of this {@link Data} or -1 if it is not binary.
+        * <p>
+        * For binary data, as long as the BKey is not processed, it will be 0.
         * 
-        * @return the bkey or -1
+        * @return the bkey, 0 or -1
         */
        public int getB64Key() {
                return b64;
        }
 
        /**
-        * Check if this {@link Data} is binary
+        * Check if this {@link Data} is binary (in this case, the BKey will be
+        * present).
         * 
         * @return TRUE if it is
         */
@@ -178,18 +191,23 @@ public class Data extends BaseClass<TypeInfo> {
        }
 
        /**
-        * Check if this {@link Data} has the "preferred" flag.
+        * Return the preferred value of this {@link Data}, or
+        * {@link Integer#MAX_VALUE} if none.
         * 
-        * @return TRUE if it has
+        * @return the preferred value
         */
-       public boolean isPreferred() {
+       public int getPreferred() {
                for (TypeInfo type : this) {
-                       if (type.getName().equals("TYPE") && type.getValue().equals("pref")) {
-                               return true;
+                       if (type.getName().equals("PRE")) {
+                               try {
+                                       return Integer.parseInt(type.getValue());
+                               } catch (NumberFormatException e) {
+                                       e.printStackTrace();
+                               }
                        }
                }
 
-               return false;
+               return Integer.MAX_VALUE;
        }
 
        /**
@@ -215,6 +233,9 @@ public class Data extends BaseClass<TypeInfo> {
        /**
         * Return the {@link List} of sep-listed values from this {@link String}
         * data.
+        * <p>
+        * Will take the backslash character into account (i.e., a backslash can
+        * escape the given separator).
         * 
         * @param value
         *            the data
@@ -232,11 +253,13 @@ public class Data extends BaseClass<TypeInfo> {
                        for (int i = 0; i < value.length(); i++) {
                                if (value.charAt(i) == sep
                                                && (i == 0 || value.charAt(i - 1) != '\\')) {
-                                       rep.add(value.substring(last, i - last));
+                                       rep.add(value.substring(last, i));
+                                       last = i + 1;
                                }
                        }
 
-                       rep.add(value.substring(last));
+                       if (last < value.length())
+                               rep.add(value.substring(last));
                }
 
                return rep;
@@ -278,4 +301,17 @@ public class Data extends BaseClass<TypeInfo> {
        public String getState() {
                return ("" + name + value + group).replace(' ', '_');
        }
+
+       @Override
+       public String toString() {
+               String out = name + ": " + value;
+               if (group != null && !group.isEmpty()) {
+                       out += " (" + group + ")";
+               }
+               if (b64 >= 0) {
+                       out += " [" + b64 + "]";
+               }
+
+               return out;
+       }
 }