Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard; |
2 | ||
3 | import java.security.InvalidParameterException; | |
4 | import java.util.LinkedList; | |
5 | import java.util.List; | |
6 | ||
7 | public class Data { | |
8 | private String name; | |
9 | private String value; | |
10 | private String group; | |
11 | private int b64; // -1 = no, 0 = still not ordered, the rest is order | |
12 | private List<TypeInfo> types; | |
13 | private boolean dirty; | |
14 | private Contact parent; | |
15 | ||
16 | public Data(List<TypeInfo> types, String name, String value, String group) { | |
17 | if (types == null) { | |
18 | types = new LinkedList<TypeInfo>(); | |
19 | } | |
20 | ||
21 | this.types = types; | |
22 | this.name = name; | |
23 | this.value = value; | |
24 | this.group = group; | |
25 | ||
26 | b64 = -1; | |
27 | for (TypeInfo type : types) { | |
28 | if (type.getName().equals("ENCODING") | |
29 | && type.getValue().equals("b")) { | |
30 | b64 = 0; | |
31 | break; | |
32 | } | |
33 | } | |
34 | } | |
35 | ||
36 | public List<TypeInfo> getTypes() { | |
37 | return types; | |
38 | } | |
39 | ||
40 | public String getName() { | |
41 | return name; | |
42 | } | |
43 | ||
44 | public String getValue() { | |
45 | return value; | |
46 | } | |
47 | ||
bcb54330 NR |
48 | public void setValue(String value) { |
49 | if ((value == null && this.value != null) | |
50 | || (value != null && !value.equals(this.value))) { | |
51 | this.value = value; | |
52 | setDirty(); | |
53 | } | |
54 | } | |
55 | ||
a3b510ab NR |
56 | public String getGroup() { |
57 | return group; | |
58 | } | |
59 | ||
60 | public int getB64Key() { | |
61 | return b64; | |
62 | } | |
63 | ||
64 | void resetB64Key(int i) { | |
65 | if (!isBinary()) | |
66 | throw new InvalidParameterException( | |
67 | "Cannot add a BKey on a non-binary object"); | |
68 | if (i < 0) | |
69 | throw new InvalidParameterException( | |
70 | "Cannot remove the BKey on a binary object"); | |
71 | ||
72 | b64 = i; | |
73 | } | |
74 | ||
75 | public boolean isBinary() { | |
76 | return b64 >= 0; | |
77 | } | |
78 | ||
79 | public boolean isDirty() { | |
80 | return dirty; | |
81 | } | |
82 | ||
bcb54330 NR |
83 | /** |
84 | * Notify that this element has unsaved changes, and notify its parent of | |
85 | * the same if any. | |
86 | */ | |
87 | protected void setDirty() { | |
88 | this.dirty = true; | |
89 | if (this.parent != null) | |
90 | this.parent.setDirty(); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Notify this element <i>and all its descendants</i> that it is in pristine | |
95 | * state (as opposed to dirty). | |
96 | */ | |
97 | void setPristine() { | |
98 | dirty = false; | |
99 | for (TypeInfo type : types) { | |
100 | // TODO ? | |
101 | } | |
102 | } | |
103 | ||
104 | void setParent(Contact parent) { | |
a3b510ab NR |
105 | this.parent = parent; |
106 | } | |
bcb54330 | 107 | |
a3b510ab | 108 | } |