001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.util;
010:
011: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
012: import com.completex.objective.components.OdalRuntimeException;
013:
014: import java.io.ByteArrayInputStream;
015: import java.io.InputStream;
016: import java.math.BigDecimal;
017: import java.math.BigInteger;
018: import java.sql.Blob;
019: import java.sql.Clob;
020: import java.sql.SQLException;
021: import java.sql.Timestamp;
022: import java.text.DateFormat;
023: import java.text.ParseException;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026: import java.util.GregorianCalendar;
027: import java.util.Map;
028:
029: /**
030: * @author Gennady Krizhevsky
031: */
032: public class TypeUtil {
033:
034: private static SafeDateFormat lyyMMdd = new SafeDateFormat(
035: "1yyMMdd");
036: private static SafeDateFormat yyyy_MM_dd = new SafeDateFormat(
037: "yyyy-MM-dd");
038: private static SafeDateFormat yyyyMMdd = new SafeDateFormat(
039: "yyyyMMdd");
040: private static SafeDateFormat yyyyMMdd_HHmmssSSS = new SafeDateFormat(
041: "yyyy-MM-dd HH:mm:ss.SSS");
042: private static SafeDateFormat yyyyMMdd_HHmmss = new SafeDateFormat(
043: "yyyy-MM-dd HH:mm:ss");
044:
045: public static SimpleDateFormat lyyMMdd() {
046: return (SimpleDateFormat) lyyMMdd.get();
047: }
048:
049: public static SimpleDateFormat yyyy_MM_dd() {
050: return (SimpleDateFormat) yyyy_MM_dd.get();
051: }
052:
053: public static SimpleDateFormat yyyyMMdd() {
054: return (SimpleDateFormat) yyyyMMdd.get();
055: }
056:
057: public static SimpleDateFormat yyyyMMdd_hhmmssSSS() {
058: return (SimpleDateFormat) yyyyMMdd_HHmmssSSS.get();
059: }
060:
061: public static SimpleDateFormat yyyyMMdd_hhmmss() {
062: return (SimpleDateFormat) yyyyMMdd_HHmmss.get();
063: }
064:
065: // Boolean formats
066: public static final int Y_N = 10;
067: public static final int YES_NO = 11;
068: public static final int TRUE_FALSE = 20;
069: public static final int ONE_ZERO = 30;
070:
071: // Boolean:
072: public static Boolean S2B(String value, int booleanFormat) {
073: if (value == null) {
074: return null;
075: }
076: if (booleanFormat == Y_N) {
077: return S2B(value, "Y", "N");
078: } else if (booleanFormat == YES_NO) {
079: return S2B(value, "YES", "NO");
080: } else if (booleanFormat == TRUE_FALSE) {
081: return S2B(value, "true", "false");
082: } else if (booleanFormat == ONE_ZERO) {
083: return S2B(value, "1", "0");
084: } else {
085: throw new IllegalArgumentException(
086: "Unrecognized booleanFormat " + booleanFormat);
087: }
088: }
089:
090: private static Boolean S2B(String value, String TRUE, String FALSE) {
091: if (TRUE.equalsIgnoreCase(value)) {
092: return Boolean.TRUE;
093: } else if (FALSE.equalsIgnoreCase(value)) {
094: return Boolean.FALSE;
095: } else {
096: throw new IllegalArgumentException(
097: "Unsupported boolean value " + value);
098: }
099: }
100:
101: public static boolean S2b(String value, int booleanFormat) {
102: if (value == null) {
103: return false;
104: }
105: return S2B(value, booleanFormat).booleanValue();
106: }
107:
108: public static Boolean N2B(Number value, int booleanFormat) {
109: if (value == null) {
110: return null;
111: }
112: if (booleanFormat == ONE_ZERO) {
113: if (1 == value.intValue()) {
114: return Boolean.TRUE;
115: } else if (0 == value.intValue()) {
116: return Boolean.FALSE;
117: } else {
118: throw new IllegalArgumentException(
119: "Unsupported boolean value " + value);
120: }
121: } else {
122: throw new IllegalArgumentException(
123: "Unrecognized booleanFormat " + booleanFormat);
124: }
125: }
126:
127: public static String b2S(boolean value, int booleanFormat) {
128: if (booleanFormat == Y_N) {
129: return value ? "Y" : "N";
130: } else if (booleanFormat == TRUE_FALSE) {
131: return value ? "true" : "false";
132: } else if (booleanFormat == ONE_ZERO) {
133: return value ? "1" : "0";
134: } else {
135: throw new IllegalArgumentException(
136: "Unrecognized booleanFormat " + booleanFormat);
137: }
138: }
139:
140: public static String B2S(Boolean value, int booleanFormat) {
141: if (value == null) {
142: return null;
143: } else {
144: return b2S(value.booleanValue(), booleanFormat);
145: }
146: }
147:
148: // Dates:
149: public static String D2S(Date date, DateFormat format) {
150: return date == null ? null : format.format(date);
151: }
152:
153: public static Date S2D(String value, DateFormat format)
154: throws ParseException {
155: return value == null ? null : format.parse(value);
156: }
157:
158: public static Long D2L(Date date) {
159: return date == null ? null : new Long(lyyMMdd().format(date));
160: }
161:
162: public static Long D2L(Date date, DateFormat dateFormat) {
163: return date == null ? null : new Long(dateFormat.format(date));
164: }
165:
166: public static long D2long(Date date) {
167: // return date == null ? null : new Long(date.getTime()/1000);
168: return date == null ? 0L : Long.parseLong(lyyMMdd()
169: .format(date));
170: }
171:
172: public static Timestamp date2Timestamp(Date date) {
173: if (date == null || date instanceof Timestamp) {
174: return (Timestamp) date;
175: }
176:
177: return new Timestamp(date.getTime());
178: }
179:
180: public static Date O2D(Object value) {
181: if (value == null) {
182: return null;
183: }
184:
185: if (value instanceof Date) {
186: return (Date) value;
187: } else if (value instanceof String) {
188: return S2T((String) value);
189: } else if (value instanceof BigDecimal) {
190: return new Date(((BigDecimal) value).longValue());
191: } else {
192: throw new RuntimeException(
193: "Cannot getTransactionType to String value "
194: + value);
195: }
196: }
197:
198: private static Timestamp S2T(String dateValue) {
199: if (dateValue == null || dateValue.length() == 0) {
200: return null;
201: }
202:
203: if (dateValue.length() < 8) {
204: throw new NumberFormatException("Cannot parse date");
205: }
206: int year = Integer.parseInt(dateValue.substring(0, 4));
207: if (year == 0) {
208: return null;
209: }
210: int month = Integer.parseInt(dateValue.substring(4, 6));
211: int day = Integer.parseInt(dateValue.substring(6, 8));
212: if (dateValue.length() == 8) {
213: return new Timestamp(
214: new GregorianCalendar(year, month, day)
215: .getTimeInMillis());
216: } else if (dateValue.length() == 14) {
217: int hour = Integer.parseInt(dateValue.substring(8, 10));
218: int min = Integer.parseInt(dateValue.substring(10, 12));
219: int sec = Integer.parseInt(dateValue.substring(12, 14));
220: return new Timestamp(new GregorianCalendar(year, month,
221: day, hour, min, sec).getTimeInMillis());
222: }
223: return null;
224: }
225:
226: public static Long S2L(String number) {
227: if (number == null) {
228: return null;
229: } else {
230: return new Long(number);
231: }
232: }
233:
234: public static BigDecimal S2BigDecimal(String number) {
235: if (number == null) {
236: return null;
237: } else {
238: return new BigDecimal(number);
239: }
240: }
241:
242: // Number:
243: public static Date N2D(Number date, DateFormat dateFormat)
244: throws ParseException {
245: return date == null || date.longValue() == 0 ? null
246: : dateFormat.parse(date.toString().trim());
247: }
248:
249: public static Timestamp N2T(Number date, DateFormat dateFormat)
250: throws ParseException {
251: return date == null || date.longValue() == 0 ? null
252: : new Timestamp(dateFormat
253: .parse(date.toString().trim()).getTime());
254: }
255:
256: public static Double Number2Double(Number number) {
257: return number == null ? null : new Double(number.doubleValue());
258: }
259:
260: public static Long Number2Long(Number number) {
261: return number == null ? null : new Long(number.longValue());
262: }
263:
264: public static BigDecimal number2BigDecimal(Number number) {
265: return number == null ? null
266: : ((number instanceof BigDecimal) ? (BigDecimal) number
267: : new BigDecimal(number.toString()));
268: }
269:
270: public static BigDecimal number2BigDecimal(long number) {
271: return new BigDecimal(number);
272: }
273:
274: public static BigDecimal number2BigDecimal(double number) {
275: return new BigDecimal(number);
276: }
277:
278: //
279: // Primitives - object tranformations :
280: //
281: public static Double double2Double(double value) {
282: return new Double(value);
283: }
284:
285: public static Long long2Long(long value) {
286: return new Long(value);
287: }
288:
289: public static long Number2long(Number number) {
290: return number == null ? 0L : number.longValue();
291: }
292:
293: public static int Number2int(Number number) {
294: return number == null ? 0 : number.intValue();
295: }
296:
297: public static short Number2short(Number number) {
298: return number == null ? 0 : number.shortValue();
299: }
300:
301: public static byte Number2byte(Number number) {
302: return number == null ? 0 : number.byteValue();
303: }
304:
305: public static double Number2double(Number number) {
306: return number == null ? 0L : number.doubleValue();
307: }
308:
309: public static float Number2float(Number number) {
310: return number == null ? 0L : number.floatValue();
311: }
312:
313: public static boolean Boolean2boolean(Boolean value) {
314: return value != null && value.booleanValue();
315: }
316:
317: public static Boolean boolean2Boolean(boolean value) {
318: return Boolean.valueOf(value);
319: }
320:
321: public static BigDecimal cents2dollars(long cents) {
322: return new BigDecimal(new BigInteger(Long.toString(cents)), 2);
323: }
324:
325: public static long dollars2cents(Number dollars) {
326: return TypeUtil.Number2long(dollars) * 100;
327: }
328:
329: public static String N2S(Number number) {
330: return number == null ? null : number.toString();
331: }
332:
333: public static String rpad(String value, int totalLength) {
334: return rpad(value, ' ', totalLength);
335: }
336:
337: private static String rpad(String value, char padding,
338: int totalLength) {
339: if (value == null) {
340: return value;
341: }
342: if (value.length() >= totalLength) {
343: return value;
344: }
345: final StringBuffer sb = new StringBuffer(value);
346: for (int i = 0; i < totalLength - value.length(); i++) {
347: sb.append(padding);
348: }
349: return sb.toString();
350: }
351:
352: public static Object object2inputStream(Object object)
353: throws SQLException {
354: if (object == null) {
355: return null;
356: } else if (object instanceof Clob) {
357: return clob2binary(((Clob) object));
358: } else if (object instanceof Blob) {
359: return blob2binary(((Blob) object));
360: } else if (object instanceof byte[]) {
361: return byteArray2binary(((byte[]) object));
362: } else if (object instanceof InputStream) {
363: return object;
364: } else {
365: throw new IllegalArgumentException(
366: "Cannot transformRead type " + object
367: + " as InputStream");
368: }
369: }
370:
371: public static InputStream clob2binary(Clob object)
372: throws SQLException {
373: if (object == null) {
374: return null;
375: }
376: return object.getAsciiStream();
377: }
378:
379: public static InputStream blob2binary(Blob object)
380: throws SQLException {
381: if (object == null) {
382: return null;
383: }
384: return object.getBinaryStream();
385: }
386:
387: public static InputStream byteArray2binary(byte[] object) {
388: if (object == null) {
389: return null;
390: }
391: return new ByteArrayInputStream(object);
392: }
393:
394: //
395: // Property helpers:
396: //
397: public static Object resolveValue(Object key, Object value,
398: Object defaultValue, boolean required) {
399: if (required && valueIsEmpty(value)
400: && valueIsEmpty(defaultValue)) {
401: throw new IllegalArgumentException("Required property "
402: + key + " missing");
403: }
404: return value == null ? defaultValue : value;
405: }
406:
407: private static boolean valueIsEmpty(Object value) {
408: return (value == null || (value instanceof String && ((String) value)
409: .length() == 0));
410: }
411:
412: public static String getProperty(String key, Object value,
413: boolean required) {
414: return (String) resolveValue(key, value, null, required);
415: }
416:
417: public static String getProperty(Object value, String defaultValue) {
418: String val = getProperty("", value, false);
419: return (val == null) ? defaultValue : val;
420: }
421:
422: public static String getProperty(Object value) {
423: String val = getProperty("", value, false);
424: return (val == null) ? null : val;
425: }
426:
427: public static Boolean getBooleanObj(String key, Object value,
428: boolean required, Boolean defaultValue) {
429: value = resolveValue(key, value, defaultValue, required);
430: if (value == null && !required) {
431: return defaultValue;
432: }
433: try {
434: if (value instanceof Boolean) {
435: return ((Boolean) value);
436: } else if (value == null) {
437: return null;
438: } else {
439: boolean flag = "true"
440: .equalsIgnoreCase(((String) value));
441: return flag ? Boolean.TRUE : Boolean.FALSE;
442: }
443: } catch (ClassCastException e) {
444: throw new IllegalArgumentException(
445: "Invalid class of value for property " + key
446: + " provided");
447: }
448: }
449:
450: public static Boolean getBooleanObj(String key, Object value,
451: boolean required) {
452: return getBooleanObj(key, value, required, null);
453: }
454:
455: public static Boolean getBooleanObj(String key, Object value) {
456: return getBooleanObj(key, value, false);
457: }
458:
459: public static boolean getBoolean(String key, Object value) {
460: return getBoolean(key, value, false);
461: }
462:
463: public static boolean getBoolean(String key, Object value,
464: Boolean defaultValue, boolean required) {
465: value = resolveValue(key, value, defaultValue, required);
466: try {
467: if (value instanceof Boolean) {
468: return ((Boolean) value).booleanValue();
469: } else {
470: return "true".equalsIgnoreCase(((String) value));
471: }
472: } catch (ClassCastException e) {
473: throw new IllegalArgumentException(
474: "Invalid class of value for property " + key
475: + " provided");
476: }
477: }
478:
479: public static boolean getBoolean(String key, Object value,
480: boolean required) {
481: return getBoolean(key, value, Boolean.FALSE, required);
482: }
483:
484: public static Long getLongObj(String key, Object value,
485: Long defaultValue, boolean required) {
486: try {
487: value = resolveValue(key, value, defaultValue, required);
488: return value == null ? null : Long
489: .valueOf(value.toString());
490: } catch (NumberFormatException e) {
491: throw new IllegalArgumentException(
492: "Invalid value for property " + key + " provided");
493: } catch (ClassCastException e) {
494: throw new IllegalArgumentException(
495: "Invalid class of value for property " + key
496: + " provided");
497: }
498: }
499:
500: public static Long getLongObj(String key, Object value) {
501: return getLongObj(key, value, false);
502: }
503:
504: public static Long getLongObj(String key, Object value,
505: boolean required) {
506: return getLongObj(key, value, null, required);
507: }
508:
509: public static Long getLongObj(String key, Object value,
510: Long defaultValue) {
511: return getLongObj(key, value, defaultValue, false);
512: }
513:
514: public static long getLong(String value, Object defaultValue) {
515: return getLong(value, defaultValue, false);
516: }
517:
518: public static long getLong(String key, Object value,
519: boolean required) {
520: try {
521: value = resolveValue(key, value, null, required);
522: return value == null ? 0 : Long.parseLong(value.toString());
523: } catch (NumberFormatException e) {
524: throw new IllegalArgumentException(
525: "Invalid value for property " + key + " provided");
526: } catch (ClassCastException e) {
527: throw new IllegalArgumentException(
528: "Invalid class of value for property " + key
529: + " provided");
530: }
531: }
532:
533: public static long getLong(String key, Object value,
534: long defaultValue) {
535: Long longValue = getLongObj(key, value, new Long(defaultValue));
536: return longValue == null ? 0 : longValue.longValue();
537: }
538:
539: public static Integer getIntObj(String key, Object value,
540: Integer defaultValue, boolean required) {
541: try {
542: value = resolveValue(key, value, defaultValue, required);
543: return value == null ? null : Integer.valueOf(value
544: .toString());
545: } catch (NumberFormatException e) {
546: throw new IllegalArgumentException(
547: "Invalid value for property " + key + " provided");
548: } catch (ClassCastException e) {
549: throw new IllegalArgumentException(
550: "Invalid class of value for property " + key
551: + " provided");
552: }
553: }
554:
555: public static Integer getIntObj(String key, Object value) {
556: return getIntObj(key, value, false);
557: }
558:
559: public static Integer getIntObj(String key, Object value,
560: boolean required) {
561: return getIntObj(key, value, null, required);
562: }
563:
564: public static Integer getIntObj(String key, Object value,
565: Integer defaultValue) {
566: return getIntObj(key, value, defaultValue, false);
567: }
568:
569: public static int getInt(String key, Object value) {
570: return ((int) getLong(key, value));
571: }
572:
573: public static int getInt(String key, Object value, boolean required) {
574: return ((int) getLong(key, value, required));
575: }
576:
577: public static int getInt(String key, Object value, int defaultValue) {
578: return ((int) getLong(key, value, defaultValue));
579: }
580:
581: public static int extractInt(Map map, String key) {
582: BigDecimal value = (BigDecimal) map.get(key);
583: return value == null ? 0 : value.intValue();
584: }
585:
586: public static boolean extractBoolean(Map map, String key) {
587: Object object = map.get(key);
588: if (object != null) {
589: if (object instanceof Boolean) {
590: return ((Boolean) object).booleanValue();
591: } else if (object instanceof String) {
592: String value = (String) object;
593: return "true".equalsIgnoreCase(value);
594: } else {
595: String clazzName = object.getClass().getName();
596: throw new OdalRuntimeException(
597: "extractBoolean: Expected type : String, gotten: "
598: + clazzName);
599: }
600: }
601: return false;
602: }
603:
604: public static void assertNotNull(Object object, String objectName) {
605: if (object == null) {
606: throw new OdalRuntimePersistencyException(objectName
607: + " is not set");
608: }
609: }
610:
611: public static class SafeDateFormat extends ThreadLocal {
612: private String pattern;
613:
614: public SafeDateFormat(String pattern) {
615: this .pattern = pattern;
616: }
617:
618: protected synchronized Object initialValue() {
619: // System.out.println("initialValue ...");
620: return new SimpleDateFormat(pattern);
621: }
622:
623: }
624:
625: }
|