Remote: jdoc + description + some fixes:
[jvcard.git] / src / be / nikiroo / jvcard / Data.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard;
2
3import java.security.InvalidParameterException;
a3b510ab
NR
4import java.util.List;
5
78e4af97
NR
6/**
7 * A data is a piece of information present in a {@link Contact}. It is
8 * basically a key/value pair with optional types and an optional group name.
9 *
10 * @author niki
11 *
12 */
26d2bd05 13public class Data extends BaseClass<TypeInfo> {
78e4af97
NR
14 public enum DataPart {
15 FN_FAMILY, FN_GIVEN, FN_ADDITIONAL, // Name
16 FN_PRE, FN_POST, // Pre/Post
17 BDAY_YYYY, BDAY_MM, BDAY_DD, // BDay
18 ADR_PBOX, ADR_EXTENDED, ADR_STREET, ADR_CITY, ADR_REGION, ADR_POSTAL_CODE, ADR_COUNTRY
19 // Address
20 }
21
a3b510ab
NR
22 private String name;
23 private String value;
24 private String group;
25 private int b64; // -1 = no, 0 = still not ordered, the rest is order
a3b510ab 26
78e4af97
NR
27 /**
28 * Create a new {@link Data} with the given values.
29 *
30 * @param types
31 * the types of this {@link Data}
32 * @param name
1c03abaf 33 * its name (<b>MUST NOT</b> be NULL)
78e4af97 34 * @param value
1c03abaf 35 * its value (<b>MUST NOT</b> be NULL)
78e4af97 36 * @param group
1c03abaf 37 * its group if any (or NULL if none)
78e4af97 38 */
a3b510ab 39 public Data(List<TypeInfo> types, String name, String value, String group) {
26d2bd05 40 super(types);
a3b510ab 41
1c03abaf
NR
42 this.name = name.toUpperCase();
43 this.value = value.toString(); // crash NOW if null
a3b510ab
NR
44 this.group = group;
45
46 b64 = -1;
26d2bd05 47 for (TypeInfo type : this) {
a3b510ab
NR
48 if (type.getName().equals("ENCODING")
49 && type.getValue().equals("b")) {
50 b64 = 0;
51 break;
52 }
53 }
54 }
55
78e4af97
NR
56 /**
57 * Return the name of this {@link Data}
58 *
59 * @return the name
60 */
a3b510ab
NR
61 public String getName() {
62 return name;
63 }
64
78e4af97
NR
65 /**
66 * Return the value of this {@link Data}
67 *
68 * @return the value
69 */
a3b510ab
NR
70 public String getValue() {
71 return value;
72 }
73
78e4af97
NR
74 /**
75 * Change the value of this {@link Data}
76 *
77 * @param value
78 * the new value
79 */
bcb54330
NR
80 public void setValue(String value) {
81 if ((value == null && this.value != null)
82 || (value != null && !value.equals(this.value))) {
83 this.value = value;
84 setDirty();
85 }
86 }
87
78e4af97
NR
88 /**
89 * Return the group of this {@link Data}
90 *
91 * @return the group
92 */
a3b510ab
NR
93 public String getGroup() {
94 return group;
95 }
96
176a8327
NR
97 /**
98 * Change the group of this {@link Data}
99 *
100 * @param group
101 * the new group
102 */
103 public void setGroup(String group) {
104 if ((group == null && this.group != null)
105 || (group != null && !group.equals(this.group))) {
106 this.group = group;
107 setDirty();
108 }
109 }
110
78e4af97
NR
111 /**
112 * Return the bkey number of this {@link Data} or -1 if it is not binary.
113 *
114 * @return the bkey or -1
115 */
a3b510ab
NR
116 public int getB64Key() {
117 return b64;
118 }
119
78e4af97
NR
120 /**
121 * Change the bkey of this {@link Data}
122 *
123 * @param i
124 * the new bkey
125 *
126 * @throw InvalidParameterException if the {@link Data} is not binary or if
127 * it is but you try to set a negative bkey
128 */
a3b510ab
NR
129 void resetB64Key(int i) {
130 if (!isBinary())
131 throw new InvalidParameterException(
132 "Cannot add a BKey on a non-binary object");
133 if (i < 0)
134 throw new InvalidParameterException(
135 "Cannot remove the BKey on a binary object");
136
137 b64 = i;
138 }
139
78e4af97
NR
140 /**
141 * Check if this {@link Data} is binary
142 *
143 * @return TRUE if it is
144 */
a3b510ab
NR
145 public boolean isBinary() {
146 return b64 >= 0;
147 }
e253bd50
NR
148
149 @Override
150 public String getId() {
151 return "" + name;
152 }
153
154 @Override
155 public String getState() {
e4444b0b 156 return ("" + name + value + group).replace(' ', '_');
e253bd50 157 }
a3b510ab 158}