| 1 | package be.nikiroo.jvcard; |
| 2 | |
| 3 | import java.security.InvalidParameterException; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 8 | * A data is a piece of information present in a {@link Contact}. It is |
| 9 | * basically a key/value pair with optional types and an optional group name. |
| 10 | * |
| 11 | * @author niki |
| 12 | * |
| 13 | */ |
| 14 | public class Data { |
| 15 | public enum DataPart { |
| 16 | FN_FAMILY, FN_GIVEN, FN_ADDITIONAL, // Name |
| 17 | FN_PRE, FN_POST, // Pre/Post |
| 18 | BDAY_YYYY, BDAY_MM, BDAY_DD, // BDay |
| 19 | ADR_PBOX, ADR_EXTENDED, ADR_STREET, ADR_CITY, ADR_REGION, ADR_POSTAL_CODE, ADR_COUNTRY |
| 20 | // Address |
| 21 | } |
| 22 | |
| 23 | private String name; |
| 24 | private String value; |
| 25 | private String group; |
| 26 | private int b64; // -1 = no, 0 = still not ordered, the rest is order |
| 27 | private List<TypeInfo> types; |
| 28 | private boolean dirty; |
| 29 | private Contact parent; |
| 30 | |
| 31 | /** |
| 32 | * Create a new {@link Data} with the given values. |
| 33 | * |
| 34 | * @param types |
| 35 | * the types of this {@link Data} |
| 36 | * @param name |
| 37 | * its name |
| 38 | * @param value |
| 39 | * its value |
| 40 | * @param group |
| 41 | * its group if any |
| 42 | */ |
| 43 | public Data(List<TypeInfo> types, String name, String value, String group) { |
| 44 | if (types == null) { |
| 45 | types = new LinkedList<TypeInfo>(); |
| 46 | } |
| 47 | |
| 48 | this.types = types; |
| 49 | this.name = name; |
| 50 | this.value = value; |
| 51 | this.group = group; |
| 52 | |
| 53 | b64 = -1; |
| 54 | for (TypeInfo type : types) { |
| 55 | type.setParent(this); |
| 56 | if (type.getName().equals("ENCODING") |
| 57 | && type.getValue().equals("b")) { |
| 58 | b64 = 0; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Return the number of {@link TypeInfo} present in this {@link Data}. |
| 66 | * |
| 67 | * @return the number of {@link TypeInfo}s |
| 68 | */ |
| 69 | public int size() { |
| 70 | return types.size(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Return the {@link TypeInfo} at index <i>index</i>. |
| 75 | * |
| 76 | * @param index |
| 77 | * the index of the {@link TypeInfo} to find |
| 78 | * |
| 79 | * @return the {@link TypeInfo} |
| 80 | * |
| 81 | * @throws IndexOutOfBoundsException |
| 82 | * if the index is < 0 or >= {@link Data#size()} |
| 83 | */ |
| 84 | public TypeInfo get(int index) { |
| 85 | return types.get(index); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Add a new {@link TypeInfo} in this {@link Data}. |
| 90 | * |
| 91 | * @param type |
| 92 | * the new type |
| 93 | */ |
| 94 | public void add(TypeInfo type) { |
| 95 | type.setParent(this); |
| 96 | type.setDirty(); |
| 97 | types.add(type); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Remove the given {@link TypeInfo} from its this {@link Data} if it is in. |
| 102 | * |
| 103 | * @return TRUE in case of success |
| 104 | */ |
| 105 | public boolean remove(TypeInfo type) { |
| 106 | if (types.remove(type)) { |
| 107 | setDirty(); |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Change the {@link TypeInfo}s of this {@link Data}. |
| 115 | * |
| 116 | * @param types |
| 117 | * the new types |
| 118 | */ |
| 119 | @Deprecated |
| 120 | public void setTypes(List<TypeInfo> types) { |
| 121 | // TODO: check if this method is required |
| 122 | this.types.clear(); |
| 123 | for (TypeInfo type : types) { |
| 124 | this.types.add(type); |
| 125 | type.setParent(this); |
| 126 | } |
| 127 | |
| 128 | setDirty(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Return the name of this {@link Data} |
| 133 | * |
| 134 | * @return the name |
| 135 | */ |
| 136 | public String getName() { |
| 137 | return name; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Return the value of this {@link Data} |
| 142 | * |
| 143 | * @return the value |
| 144 | */ |
| 145 | public String getValue() { |
| 146 | return value; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Change the value of this {@link Data} |
| 151 | * |
| 152 | * @param value |
| 153 | * the new value |
| 154 | */ |
| 155 | public void setValue(String value) { |
| 156 | if ((value == null && this.value != null) |
| 157 | || (value != null && !value.equals(this.value))) { |
| 158 | this.value = value; |
| 159 | setDirty(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return the group of this {@link Data} |
| 165 | * |
| 166 | * @return the group |
| 167 | */ |
| 168 | public String getGroup() { |
| 169 | return group; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Change the group of this {@link Data} |
| 174 | * |
| 175 | * @param group |
| 176 | * the new group |
| 177 | */ |
| 178 | public void setGroup(String group) { |
| 179 | if ((group == null && this.group != null) |
| 180 | || (group != null && !group.equals(this.group))) { |
| 181 | this.group = group; |
| 182 | setDirty(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Return the bkey number of this {@link Data} or -1 if it is not binary. |
| 188 | * |
| 189 | * @return the bkey or -1 |
| 190 | */ |
| 191 | public int getB64Key() { |
| 192 | return b64; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Change the bkey of this {@link Data} |
| 197 | * |
| 198 | * @param i |
| 199 | * the new bkey |
| 200 | * |
| 201 | * @throw InvalidParameterException if the {@link Data} is not binary or if |
| 202 | * it is but you try to set a negative bkey |
| 203 | */ |
| 204 | void resetB64Key(int i) { |
| 205 | if (!isBinary()) |
| 206 | throw new InvalidParameterException( |
| 207 | "Cannot add a BKey on a non-binary object"); |
| 208 | if (i < 0) |
| 209 | throw new InvalidParameterException( |
| 210 | "Cannot remove the BKey on a binary object"); |
| 211 | |
| 212 | b64 = i; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Check if this {@link Data} is binary |
| 217 | * |
| 218 | * @return TRUE if it is |
| 219 | */ |
| 220 | public boolean isBinary() { |
| 221 | return b64 >= 0; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Delete this {@link Contact} from its parent {@link Card} if any. |
| 226 | * |
| 227 | * @return TRUE in case of success |
| 228 | */ |
| 229 | public boolean delete() { |
| 230 | if (parent != null) { |
| 231 | return parent.remove(this); |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Check if this {@link Data} has unsaved changes. |
| 239 | * |
| 240 | * @return TRUE if it has |
| 241 | */ |
| 242 | public boolean isDirty() { |
| 243 | return dirty; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Notify that this element has unsaved changes, and notify its parent of |
| 248 | * the same if any. |
| 249 | */ |
| 250 | protected void setDirty() { |
| 251 | this.dirty = true; |
| 252 | if (this.parent != null) |
| 253 | this.parent.setDirty(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Notify this element <i>and all its descendants</i> that it is in pristine |
| 258 | * state (as opposed to dirty). |
| 259 | */ |
| 260 | void setPristine() { |
| 261 | dirty = false; |
| 262 | for (TypeInfo type : types) { |
| 263 | type.setPristine(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Set the parent of this {@link Data}. |
| 269 | * |
| 270 | * @param parent |
| 271 | * the new parent |
| 272 | */ |
| 273 | void setParent(Contact parent) { |
| 274 | this.parent = parent; |
| 275 | } |
| 276 | } |