| 1 | package be.nikiroo.jvcard; |
| 2 | |
| 3 | import java.security.InvalidParameterException; |
| 4 | import java.util.List; |
| 5 | |
| 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 | */ |
| 13 | public class Data extends BaseClass<TypeInfo> { |
| 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 | |
| 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 |
| 26 | |
| 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 |
| 33 | * its name (<b>MUST NOT</b> be NULL) |
| 34 | * @param value |
| 35 | * its value (<b>MUST NOT</b> be NULL) |
| 36 | * @param group |
| 37 | * its group if any (or NULL if none) |
| 38 | */ |
| 39 | public Data(List<TypeInfo> types, String name, String value, String group) { |
| 40 | super(types); |
| 41 | |
| 42 | this.name = name.toUpperCase(); |
| 43 | this.value = value.toString(); // crash NOW if null |
| 44 | this.group = group; |
| 45 | |
| 46 | b64 = -1; |
| 47 | for (TypeInfo type : this) { |
| 48 | if (type.getName().equals("ENCODING") |
| 49 | && type.getValue().equals("b")) { |
| 50 | b64 = 0; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return the name of this {@link Data} |
| 58 | * |
| 59 | * @return the name |
| 60 | */ |
| 61 | public String getName() { |
| 62 | return name; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return the value of this {@link Data} |
| 67 | * |
| 68 | * @return the value |
| 69 | */ |
| 70 | public String getValue() { |
| 71 | return value; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Change the value of this {@link Data} |
| 76 | * |
| 77 | * @param value |
| 78 | * the new value |
| 79 | */ |
| 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 | |
| 88 | /** |
| 89 | * Return the group of this {@link Data} |
| 90 | * |
| 91 | * @return the group |
| 92 | */ |
| 93 | public String getGroup() { |
| 94 | return group; |
| 95 | } |
| 96 | |
| 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 | |
| 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 | */ |
| 116 | public int getB64Key() { |
| 117 | return b64; |
| 118 | } |
| 119 | |
| 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 | */ |
| 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 | |
| 140 | /** |
| 141 | * Check if this {@link Data} is binary |
| 142 | * |
| 143 | * @return TRUE if it is |
| 144 | */ |
| 145 | public boolean isBinary() { |
| 146 | return b64 >= 0; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public String getId() { |
| 151 | return "" + name; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public String getState() { |
| 156 | return ("" + name + value + group).replace(' ', '_'); |
| 157 | } |
| 158 | } |