summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/ticket/api/utils/UicEncoderUtils.java
blob: 73f96f1d4d692f6b83a0c257e2031fe5b5015be2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * 
 */
package org.uic.barcode.ticket.api.utils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.uic.barcode.asn1.datatypes.Asn1BigInteger;
import org.uic.barcode.asn1.datatypesimpl.SequenceOfStringIA5;
import org.uic.barcode.asn1.datatypesimpl.SequenceOfStringUTF8;
import org.uic.barcode.ticket.EncodingFormatException;


/**
 * The Class UicEncoderUtils.
 */
public class UicEncoderUtils {
	
	

	/**
	 * Map to int.
	 *
	 * @param o the object to be mapped to an integer
	 * @return the int
	 */
	public static int mapToInt(Object o){

		if (o == null){
			return 0;
		}		

		if (o instanceof Integer) {
			return ((Integer)o).intValue();
		}		
		
		if (o instanceof Long) {
			return ((Long)o).intValue();
		}
		
		if (o instanceof String) {
			
			int i = 0;
			
			try {
				i = Integer.parseInt((String)o);
			} catch (NumberFormatException e) {
				return 0;
			}
			
			
			return i;
		}		
		
		int i = 0;
		
		try {
			i = Integer.parseInt(o.toString());
		} catch (NumberFormatException e) {
			return 0;
		}
		
		
		return i;		
		
	}
	
	/**
	 * Map to string.
	 *
	 * @param number the number
	 * @param text the text
	 * @return the string
	 */
	public static String mapToString(Asn1BigInteger number, String text) {
		if (text != null && text.length() > 0) {
			return text;
		} else {
			if (number != null){
				return number.value().toString();
			} else {
				return null;
			}
		}
	}
	
	/**
	 * Map to string.
	 *
	 * @param number the number
	 * @param text the text
	 * @return the string
	 */
	public static String mapToString(BigInteger number, String text) {
		if (text != null && text.length() > 0) {
			return text;
		} else {
			if (number != null){
				return number.toString();
			} else {
				return null;
			}
		}
	}
	
	/**
	 * Map to string.
	 *
	 * @param number the number
	 * @param text the text
	 * @return the string
	 */
	public static String mapToString(Integer number, String text) {
		
		if (text != null && text.length() > 0) {
			return text;
		} else {
			if (number != null){
				return number.toString();
			} else {
				return null;
			}
		}
	}

	/**
	 * Map to string.
	 *
	 * @param number the number
	 * @param text the text
	 * @return the string
	 */
	public static String mapToString(Long number, String text) {
		
		if (text != null && text.length() > 0) {
			return text;
		} else {
			if (number != null){
				return number.toString();
			} else {
				return null;
			}
		}
	}	
	



	/**
	 * Map to string.
	 *
	 * @param numbers the numbers
	 * @return the collection
	 */
	public static Collection<String> mapToString(Collection<Long> numbers) {
				
		Collection<String> list = new HashSet<String>();		
		
		if (numbers == null || numbers.isEmpty()) {
			return list;	
		}		
		
		for (Long number : numbers){
			list.add(number.toString());
		}

		return list;
	}




	/**
	 * Gets the num.
	 *
	 * @param text the text
	 * @return the num
	 */
	public static Long getNum(String text) {
		
		if (text == null || text.length() == 0) {
			return null;
		}
		
		Long i;
		try {
			i = Long.parseLong(text);
		} catch (NumberFormatException e) {
			return null;
		}
		return i;
	}
	
	/**
	 * Gets the num.
	 *
	 * @param text the text
	 * @return the num
	 */
	public static Asn1BigInteger getLargeNum(String text) {
		
		if (text == null || text.length() == 0) {
			return null;
		}
		
		BigInteger i;
		try {
			i = new BigInteger(text);
		} catch (NumberFormatException e) {
			return null;
		}
		return new Asn1BigInteger(i);
	}
	
	/**
	 * Gets the num list.
	 *
	 * @param list the list
	 * @return the num list
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static List<Long> getNumList(Collection<String> list) throws EncodingFormatException {
		
		if (list== null || list.isEmpty()) {
			return null;
		}
		List<Long> numList = new ArrayList<Long>();
		
		for (String text : list){
			Long num = getNum (text);
			if (num != null) {
				numList.add(num);
			}
		}
		
		
		if (numList.isEmpty()) {
			return null;
		}
		
		return numList;		
	}

	/**
	 * Gets the i a5 non num list.
	 *
	 * @param list the list
	 * @return the i a5 non num list
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static SequenceOfStringIA5 getIA5NonNumList(Collection<String> list) throws EncodingFormatException {
		
		if (list== null || list.isEmpty()) {
			return null;
		}
		SequenceOfStringIA5 ia5List = new SequenceOfStringIA5();
		
		for (String text : list){
			String ia5 = getIA5NonNum (text);
			if (ia5 != null && ia5.length() > 0) {
				ia5List.add(ia5);
			}
		}
		
		
		if (ia5List.isEmpty()) {
			return null;
		}
		
		return ia5List;		
	}
	
	
	
	
	/**
	 * Gets the i a5.
	 *
	 * @param text the text
	 * @return the i a5
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static String getIA5(String text) throws EncodingFormatException {
		
		if (text == null || text.length() == 0) {
			return null;
		}		
		
		for (int i = 0; i < text.length(); i++){
			int index = text.charAt(i);
			if (index < 0 || index > 127) {
				throw new EncodingFormatException("Wrong Characters in IA5 String encoding");
			}
		}

		return text;

	}	

	/**
	 * Gets the i a5 non num.
	 *
	 * @param text the text
	 * @return the i a5 non num
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static String getIA5NonNum(String text) throws EncodingFormatException {
		
		if (text == null || text.length() == 0) {
			return null;
		}		
		
		for (int i = 0; i < text.length(); i++){
			int index = text.charAt(i);
			if (index < 0 || index > 127) {
				throw new EncodingFormatException("Wrong Characters in IA5 String encoding");
			}
		}

		try {
			Long.parseLong(text);
			return null;
		} catch (NumberFormatException e) {
			return text;
		}		

	}

	/**
	 * Gets the restricted int.
	 *
	 * @param value the value
	 * @param min the min
	 * @param max the max
	 * @return the restricted int
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static Long getRestrictedInt(int value, int min, int max) throws EncodingFormatException {
		if (value == 0) return null;
		
		if (value < min || value > max) {
			throw new EncodingFormatException("Integer value exceeds boundaries");
		}
		return new Long(value);
	}	
	
	/**
	 * Gets the restricted int with default.
	 *
	 * @param value the value
	 * @param min the min
	 * @param max the max
	 * @param defaultValue the default value
	 * @return the restricted int with default
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static Long getRestrictedIntWithDefault(int value, int min, int max, int defaultValue) throws EncodingFormatException {
		if (value == defaultValue || value == 0) return null;
		
		if (value < min || value > max) {
			throw new EncodingFormatException("Integer value exceeds boundaries");
		}
		return new Long(value);
	}

	
	/**
	 * Gets the un restricted int.
	 *
	 * @param value the value
	 * @return the un restricted int
	 */
	public static Long getUnRestrictedInt(int value) {
		if (value == 0) return null;
		return new Long(value);
	}	

	/**
	 * Gets the un restricted int list.
	 *
	 * @param intList the int list
	 * @return the un restricted int list
	 */
	public static List<Long> getUnRestrictedIntList(	Collection<Integer> intList) {
		if (intList == null || intList.isEmpty()) return null;
		
		List<Long> list = new ArrayList<Long>();
		
		for (Integer i : intList){
			if (i != 0){
				list.add(i.longValue());
			}
		}
		
		if (list.isEmpty()) return null;
		return list;
	}
	
	/**
	 * Gets the restricted int list.
	 *
	 * @param intList the int list
	 * @param min the min
	 * @param max the max
	 * @return the restricted int list
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static List<Integer> getRestrictedIntList(	Collection<Integer> intList, int min, int max) throws EncodingFormatException {
		if (intList == null || intList.isEmpty()) return null;
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (Integer i : intList){
			if (i != 0){
				
				if (i < min || i > max){
					throw new EncodingFormatException("Integer value exceeds boundaries");
				}
				
				list.add(i);
			}
		}
		
		if (list.isEmpty()) return null;
		return list;
	}
	
	/**
	 * Encode restricted integer collection.
	 *
	 * @param collection the collection
	 * @param min the min
	 * @param max the max
	 * @return the list
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static List<Long> encodeRestrictedIntegerCollection(Collection<Integer> collection, int min, int max) throws EncodingFormatException {
		
		if ( collection == null ||  collection.isEmpty()) {
			return null;
		}
			
		List<Long> list = new ArrayList<Long>();
		for (Integer item : collection){
			Long listItem = getRestrictedInt(item, min, max);
			if (listItem != null){
				list.add(listItem);
			}
		}
		if (list.isEmpty()){
			return null;
		}
		return list;

	}	
	
	/**
	 * Encode integer collection.
	 *
	 * @param collection the collection
	 * @return the list
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static SequenceOfStringUTF8 encodeStringCollection(Collection<String> collection) throws EncodingFormatException {
		
		if ( collection == null ||  collection.isEmpty()) {
			return null;
		}
			
		SequenceOfStringUTF8 list = new SequenceOfStringUTF8();
		for (String item : collection){
			if (item.length() > 0){
				list.add(item);
			}
		}
		if (list.isEmpty()){
			return null;
		}
		return list;

	}	
	
	/**
	 * Encode integer collection.
	 *
	 * @param collection the collection
	 * @return the list
	 * @throws EncodingFormatException the encoding format exception
	 */
	public static List<Long> encodeIntegerCollection(Collection<Integer> collection) throws EncodingFormatException {
		
		if ( collection == null ||  collection.isEmpty()) {
			return null;
		}
			
		List<Long> list = new ArrayList<Long>();
		for (Integer item : collection){
			if (item != null){
				list.add(item.longValue());
			}
		}
		if (list.isEmpty()){
			return null;
		}
		return list;

	}


}