1 package com.instantbank.common.utilcomponents; 2 3 import java.io.Serializable; 4 5 /** 6 * Utility class allowing manipulation of pairs "code,description". 7 * 8 * @author InstantBank (Rodrigo Lopez). 9 * @created August 2002 10 */ 11 public class CodeDescription 12 implements Cloneable, Serializable { 13 14 /** 15 * A numeric code with a related {@link #description}. 16 */ 17 private long code; 18 19 /** 20 * A readable description of the {@link #code}. 21 */ 22 private String description; 23 24 25 /** 26 * Default constructor. 27 */ 28 public CodeDescription() { } 29 30 31 /** 32 * Constructor . <br> 33 * <br> 34 * 35 * @param cod The code value. 36 * @param d The code description. 37 */ 38 public CodeDescription(long cod, String d) { 39 code = cod; 40 description = d; 41 } 42 43 44 /** 45 * Locates a <code>CodigoDescription</code> by its <code>code</code> in an 46 * array of <code>CodigoDescription</code>s. 47 * 48 * @param cod <code>code</code> used as search criteria. 49 * @param cdArray Array where the <code>CodigoDescription</code> object is 50 * looked for. 51 * @return <ul> 52 * <li> The first <code>CodigoDescription</code> inside <code>arreglo</code> 53 * whose <code>code</code> matches <code>cod</code>, if it is found. 54 * </li> 55 * <li> <code>null</code> Otherwise.</li> 56 * </ul> 57 */ 58 public static CodeDescription assocCode(long cod, CodeDescription[] cdArray) { 59 if(cdArray == null) { 60 return null; 61 } 62 for(int k = 0; k < cdArray.length; k++) { 63 if(cdArray[k].code == cod) { 64 return cdArray[k]; 65 } 66 } 67 return null; 68 } 69 70 71 /** 72 * Locates a <code>CodigoDescription</code> by its <code>description</code> 73 * in an array of <code>CodigoDescription</code>s. 74 * 75 * @param descr <code>description</code> used as search criteria. 76 * @param cdArray Array where the <code>CodigoDescription</code> object is 77 * looked for. 78 * @return <ul> 79 * <li> The first <code>CodigoDescription</code> inside <code>arreglo</code> 80 * whose <code>description</code> matches <code>desrc</code>, if it is 81 * found. </li> 82 * <li> <code>null</code> Otherwise.</li> 83 * </ul> 84 */ 85 public static CodeDescription assocDescription(String descr, CodeDescription[] cdArray) { 86 if(cdArray == null) { 87 return null; 88 } 89 for(int k = 0; k < cdArray.length; k++) { 90 if(cdArray[k].description.compareTo(descr) == 0) { 91 return cdArray[k]; 92 } 93 } 94 return null; 95 } 96 97 98 /** 99 * Creates a clone of <code>this</code>. 100 * 101 * @return El clon creado. 102 */ 103 public Object clone() { 104 try { 105 return super.clone(); 106 } 107 catch(CloneNotSupportedException e) { 108 return null; 109 } 110 } 111 112 113 /** 114 * Standard equal comparison. 115 * 116 * @param cd The comparison object. <br> 117 * <br> 118 * Comparison restricted to <code>code</code>. Descriptions are not 119 * compared!!. 120 * @return <lu> 121 * <li> <code>true</code> If codes are equal. </li> 122 * <li> <code>false</code> Otherwise.</li> </lu> 123 */ 124 public boolean equals(CodeDescription cd) { 125 if(cd == this) { 126 return true; 127 } 128 129 if(cd.code != this.code) { 130 return false; 131 } 132 if(cd.description.compareTo(this.description) != 0) { 133 return false; 134 } 135 136 return true; 137 } 138 139 140 /** 141 * Delivers an array with "descriptions" starting from an array of <code>CodeDescriptions</code> 142 * . 143 * 144 * @param cd An array of <code>CodeDescriptions</code>. 145 * @return An array of descriptions or null if the parameter is null. 146 */ 147 public static String[] toDescriptions(CodeDescription[] cd) { 148 if(cd == null) { 149 return null; 150 } 151 152 String[] res = new String[cd.length]; 153 154 for(int i = 0; i < res.length; i++) { 155 res[i] = cd[i].description; 156 } 157 158 return res; 159 } 160 161 162 /** 163 * Delivers an array with "descriptions" starting from an array of <code>CodeDescriptions</code> 164 * . This array starts with a String corresponding to the "undefined" code. 165 * 166 * @param cd An array of <code>CodeDescriptions</code>. 167 * @param undef A description for the "undefined" code. 168 * @return An array of descriptions, starting with a description for the 169 * "undefined" code, or null if the cd parameter is null. 170 */ 171 public static String[] toDescriptionsWithUndef(CodeDescription[] cd, String undef) { 172 if(cd == null) { 173 return null; 174 } 175 176 String[] res = new String[cd.length + 1]; 177 178 for(int i = 0; i < cd.length; i++) { 179 res[i + 1] = cd[i].description; 180 } 181 182 res[0] = undef; 183 return res; 184 } 185 186 187 /** 188 * Delivers an array with "codes" starting from an array of <code>CodeDescriptions</code> 189 * . 190 * 191 * @param cd An array of <code>CodeDescriptions</code>. 192 * @return An array of codes, or null if the cd parameter is null. 193 */ 194 public static long[] toCodes(CodeDescription[] cd) { 195 if(cd == null) { 196 return null; 197 } 198 199 long[] res = new long[cd.length]; 200 201 for(int i = 0; i < cd.length; i++) { 202 res[i] = cd[i].code; 203 } 204 205 return res; 206 } 207 208 209 /** 210 * Setter method for <code>code</code>. 211 * 212 * @param code The new code value 213 */ 214 public void setCode(long code) { 215 this.code = code; 216 } 217 218 219 /** 220 * Setter method for <code>description</code>. 221 * 222 * @param description The new description value 223 */ 224 public void setDescription(String description) { 225 this.description = description; 226 } 227 228 229 /** 230 * Getter method for <code>code</code>. 231 * 232 * @return The code value 233 */ 234 public long getCode() { 235 return (this.code); 236 } 237 238 239 /** 240 * Getter method for <code>description</code>. 241 * 242 * @return The description value 243 */ 244 public String getDescription() { 245 return (this.description); 246 } 247 248 249 /** 250 * Gets the index of the first entry in a CodeDescription array having a 251 * specific "code" value. 252 * 253 * @param code The code searched for. 254 * @param cd The array where the code is searched. 255 * @return <ul> 256 * <li> The index of the first entry having the searched code. 257 * <li> -1 If there is no entry with the searched code. 258 * </ul> 259 */ 260 public static int getIndexCode(long code, CodeDescription[] cd) { 261 262 for(int i = 0; i < cd.length; i++) { 263 if(code == cd[i].code) { 264 return i; 265 } 266 } 267 return -1; 268 } 269 270 271 /** 272 * Gets the index of the first entry in a CodeDescription array having a 273 * specific "description" value. 274 * 275 * @param desc The description searched for. 276 * @param cd The array where the description is searched. 277 * @return <ul> 278 * <li> The index of the first entry having the searched description. 279 * 280 * <li> -1 If there is no entry with the searched description. 281 * </ul> 282 */ 283 public static int getIndexDescription(String desc, CodeDescription[] cd) { 284 for(int i = 0; i < cd.length; i++) { 285 if(cd[i].description.equals(desc)) { 286 return i; 287 } 288 } 289 return -1; 290 } 291 } 292