View Javadoc

1   package com.workingdogs.village;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.math.BigDecimal;
23  import java.sql.Blob;
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.sql.Time;
28  import java.sql.Timestamp;
29  import java.sql.Types;
30  import java.util.Calendar;
31  
32  /***
33   * A Value represents a single cell in a database table. In other words,
34   * it is the cross between a row and column and contains the
35   * information held there.
36   *
37   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
38   * @version $Revision: 568 $
39   */
40  public class Value
41  {
42      /*** the object that is stored in this object */
43      private Object valueObject;
44  
45      /*** the column number that this object came from */
46      private final int columnNumber;
47  
48      /*** what sql type of object is this? */
49      private final int type;
50  
51      /***
52       * Creates a new Value object based on the ResultSet, columnNumber and
53       * type
54       *
55       * @param rs
56       * @param columnNumber
57       * @param type
58       *
59       * @exception SQLException
60       */
61      public Value(ResultSet rs, int columnNumber, int type)
62              throws SQLException
63      {
64          this.columnNumber = columnNumber;
65          this.type = type;
66          this.valueObject = null;
67  
68          if (rs == null)
69          {
70              return;
71          }
72  
73          switch (type())
74          {
75          case Types.BIT:
76  
77              String tmp = rs.getString(columnNumber);
78  
79              if (tmp == null)
80              {
81                  valueObject = Boolean.FALSE;
82              }
83              else if (isTrue(tmp))
84              {
85                  valueObject = Boolean.TRUE;
86              }
87              else
88              {
89                  valueObject = Boolean.FALSE;
90              }
91  
92              break;
93  
94          case Types.TINYINT:
95              valueObject = new Byte(rs.getByte(columnNumber));
96  
97              break;
98  
99          case Types.BIGINT:
100             valueObject = new Long(rs.getLong(columnNumber));
101 
102             break;
103 
104         case Types.SMALLINT:
105             valueObject = new Short(rs.getShort(columnNumber));
106 
107             break;
108 
109         case Types.INTEGER:
110             valueObject = new Integer(rs.getInt(columnNumber));
111 
112             break;
113 
114         case Types.REAL:
115             valueObject = new Float(rs.getFloat(columnNumber));
116 
117             break;
118 
119         case Types.FLOAT:
120         case Types.DOUBLE:
121             valueObject = new Double(rs.getDouble(columnNumber));
122 
123             break;
124 
125         case Types.NUMERIC:
126         case Types.DECIMAL:
127 
128             String number = rs.getString(columnNumber);
129 
130             if (number == null)
131             {
132                 valueObject = null;
133             }
134             else
135             {
136                 valueObject = new BigDecimal(number);
137             }
138 
139             break;
140 
141         case Types.LONGVARBINARY:
142         case Types.VARBINARY:
143         case Types.BINARY:
144             valueObject = rs.getBytes(columnNumber);
145 
146             break;
147 
148         case Types.BLOB:
149             Blob blob = rs.getBlob(columnNumber);
150             valueObject = blob != null ? blob.getBytes(1, (int) blob.length()) : null;
151 
152             break;
153 
154         case Types.LONGVARCHAR:
155         case Types.CHAR:
156         case Types.VARCHAR:
157         case Types.OTHER:
158             valueObject = rs.getString(columnNumber);
159 
160             break;
161 
162         case Types.DATE:
163             valueObject = rs.getDate(columnNumber);
164 
165             break;
166 
167         case Types.TIME:
168             valueObject = rs.getTime(columnNumber);
169 
170             break;
171 
172         case Types.TIMESTAMP:
173             valueObject = rs.getTimestamp(columnNumber);
174 
175             break;
176 
177         case Types.NULL:
178             valueObject = null;
179 
180             break;
181 
182         default:
183             valueObject = rs.getString(columnNumber);
184 
185             break;
186         }
187 
188         if (rs.wasNull())
189         {
190             valueObject = null;
191         }
192 
193         return;
194     }
195 
196     /***
197      * Sets the value of this object
198      *
199      * @param value
200      */
201     void setValue(Object value)
202     {
203         this.valueObject = value;
204     }
205 
206     /***
207      * Gets the object from this Value
208      *
209      * @return the object from this Value
210      */
211     Object getValue()
212     {
213         return this.valueObject;
214     }
215 
216     /***
217      * This is used in Record in order to do a saveWithInsert/Update/Delete
218      *
219      * @param stmt
220      * @param stmtNumber
221      *
222      * @exception DataSetException
223      * @exception SQLException
224      */
225     void setPreparedStatementValue(PreparedStatement stmt, int stmtNumber)
226             throws DataSetException, SQLException
227     {
228         if (isNull())
229         {
230             stmt.setNull(stmtNumber, type());
231 
232             return;
233         }
234 
235         switch (type())
236         {
237         case Types.BIT:
238             stmt.setBoolean(stmtNumber, this.asBoolean());
239 
240             break;
241 
242         case Types.TINYINT:
243             stmt.setByte(stmtNumber, this.asByte());
244 
245             break;
246 
247         case Types.BIGINT:
248             stmt.setLong(stmtNumber, this.asLong());
249 
250             break;
251 
252         case Types.SMALLINT:
253             stmt.setShort(stmtNumber, this.asShort());
254 
255             break;
256 
257         case Types.INTEGER:
258             stmt.setInt(stmtNumber, this.asInt());
259 
260             break;
261 
262         case Types.REAL:
263             stmt.setFloat(stmtNumber, this.asFloat());
264 
265             break;
266 
267         case Types.FLOAT:
268         case Types.DOUBLE:
269             stmt.setDouble(stmtNumber, this.asDouble());
270 
271             break;
272 
273         case Types.NUMERIC:
274         case Types.DECIMAL:
275             stmt.setBigDecimal(stmtNumber, this.asBigDecimal());
276 
277             break;
278 
279         case Types.LONGVARBINARY:
280         case Types.VARBINARY:
281         case Types.BINARY:
282         case Types.BLOB:
283 
284             // The following form is reported to work and be necessary for
285             // Oracle when the blob exceeds 4k.
286             byte [] value = this.asBytes();
287             stmt.setBinaryStream(stmtNumber,
288                     new java.io.ByteArrayInputStream(value), value.length);
289 
290             break;
291 
292         case Types.LONGVARCHAR:
293         case Types.CHAR:
294         case Types.VARCHAR:
295         case Types.OTHER:
296             stmt.setString(stmtNumber, this.asString());
297 
298             break;
299 
300         case Types.DATE:
301             stmt.setDate(stmtNumber, this.asDate());
302 
303             break;
304 
305         case Types.TIME:
306             stmt.setTime(stmtNumber, this.asTime());
307 
308             break;
309 
310         case Types.TIMESTAMP:
311             stmt.setTimestamp(stmtNumber, this.asTimestamp());
312 
313             break;
314 
315         case Types.NULL:
316             stmt.setNull(stmtNumber, 0);
317 
318             break;
319 
320         default:
321             stmt.setString(stmtNumber, this.asString());
322 
323             break;
324         }
325     }
326 
327     /***
328      * Returns the string representation of this object
329      *
330      * @return a string
331      */
332     public String toString()
333     {
334         return this.asString();
335     }
336 
337     /***
338      * Returns the string representation of this object
339      *
340      * @return a string
341      */
342     public String asString()
343     {
344         if (isNull())
345         {
346             return null;
347         }
348         else if (isString())
349         {
350             return (String) valueObject;
351         }
352         else if (isBytes())
353         {
354             return new String((byte []) valueObject);
355         }
356         else
357         {
358             return valueObject.toString();
359         }
360     }
361 
362     /***
363      * Get the value as a BigDecimal
364      *
365      * @return a BigDecimal
366      *
367      * @exception DataSetException
368      */
369     public BigDecimal asBigDecimal()
370             throws DataSetException
371     {
372         try
373         {
374             if (isNull())
375             {
376                 return null;
377             }
378             else if (isBigDecimal())
379             {
380                 return (BigDecimal) valueObject;
381             }
382             else if (isDouble())
383             {
384                 return new BigDecimal(((Double) valueObject).doubleValue());
385             }
386             else if (isFloat())
387             {
388                 return new BigDecimal(((Float) valueObject).doubleValue());
389             }
390             else if (isString() || isInt() || isLong() || isShort() || isByte())
391             {
392                 return new BigDecimal(asString());
393             }
394             else
395             {
396                 return null;
397             }
398         }
399         catch (Exception e)
400         {
401             throw new DataSetException("Illegal conversion: " + e.toString());
402         }
403     }
404 
405     /***
406      * Get the value as a BigDecimal
407      *
408      * @param scale TODO: DOCUMENT ME!
409      *
410      * @return a BigDecimal
411      *
412      * @exception DataSetException
413      */
414     public BigDecimal asBigDecimal(int scale)
415             throws DataSetException
416     {
417         try
418         {
419             if (isNull())
420             {
421                 return null;
422             }
423             else if (isBigDecimal())
424             {
425                 return ((BigDecimal) valueObject).setScale(scale);
426             }
427             else if (isDouble())
428             {
429                 return new BigDecimal(((Double) valueObject).doubleValue())
430                                                              .setScale(scale);
431             }
432             else if (isFloat())
433             {
434                 return new BigDecimal(((Float) valueObject).doubleValue())
435                                 .setScale(scale);
436             }
437             else if (isString() || isInt() || isLong() || isShort() || isByte())
438             {
439                 return new BigDecimal(asString()).setScale(scale);
440             }
441             else
442             {
443                 return null;
444             }
445         }
446         catch (Exception e)
447         {
448             throw new DataSetException("Bad conversion: " + e.toString());
449         }
450     }
451 
452     /***
453      * Get the value as a asBoolean
454      *
455      * @return a boolean
456      *
457      * @exception DataSetException
458      */
459     public boolean asBoolean()
460             throws DataSetException
461     {
462         try
463         {
464             if (isNull())
465             {
466                 return false;
467             }
468             else if (isBoolean())
469             {
470                 return ((Boolean) valueObject).booleanValue();
471             }
472 
473             String check = asString();
474 
475             return (check == null) ? false : isTrue(check);
476         }
477         catch (Exception e)
478         {
479             throw new DataSetException("Bad conversion: " + e.toString());
480         }
481     }
482 
483     /***
484      * Get the value as a Boolean object
485      *
486      * @return a Boolean
487      *
488      * @exception DataSetException
489      */
490     public Boolean asBooleanObj()
491             throws DataSetException
492     {
493         try
494         {
495             if (isNull())
496             {
497                 return null;
498             }
499             else if (isBoolean())
500             {
501                 return (Boolean) valueObject;
502             }
503 
504             String check = asString();
505 
506             if (check == null)
507             {
508                 return null;
509             }
510             else if (isTrue(check))
511             {
512                 return Boolean.TRUE;
513             }
514             else
515             {
516                 return Boolean.FALSE;
517             }
518         }
519         catch (Exception e)
520         {
521             throw new DataSetException("Bad conversion: " + e.toString());
522         }
523     }
524 
525     /***
526      * Get the value as a asInt
527      *
528      * @return an int
529      *
530      * @exception DataSetException
531      */
532     public int asInt()
533             throws DataSetException
534     {
535         try
536         {
537             if (isNull())
538             {
539                 return 0;
540             }
541             else if (isInt())
542             {
543                 return ((Integer) valueObject).intValue();
544             }
545             else if (isString())
546             {
547                 return Integer.valueOf((String) valueObject).intValue();
548             }
549             else if (isLong())
550             {
551                 return ((Long) valueObject).intValue();
552             }
553             else if (isDouble())
554             {
555                 return ((Double) valueObject).intValue();
556             }
557             else if (isFloat())
558             {
559                 return ((Float) valueObject).intValue();
560             }
561             else if (isBigDecimal())
562             {
563                 return ((BigDecimal) valueObject).intValue();
564             }
565             else
566             {
567                 return Integer.valueOf(asString()).intValue();
568             }
569         }
570         catch (Exception e)
571         {
572             throw new DataSetException("Bad conversion: " + e.toString());
573         }
574     }
575 
576     /***
577      * Get the value as a Integer Ojbect
578      *
579      * @return an Integer
580      *
581      * @exception DataSetException
582      */
583     public Integer asIntegerObj()
584             throws DataSetException
585     {
586         try
587         {
588             if (isNull())
589             {
590                 return null;
591             }
592             else if (isInt())
593             {
594                 return ((Integer) valueObject);
595             }
596             else if (isString() || isDouble() || isFloat() || isBigDecimal()
597                      || isLong() || isShort() || isByte())
598             {
599                 return new Integer(asString());
600             }
601             else
602             {
603                 throw new DataSetException("Invalid type for Integer");
604             }
605         }
606         catch (Exception e)
607         {
608             throw new DataSetException("Illegal conversion: " + e.toString());
609         }
610     }
611 
612     /***
613      * Get the value as a asByte
614      *
615      * @return a byte
616      *
617      * @exception DataSetException
618      */
619     public byte asByte()
620             throws DataSetException
621     {
622         try
623         {
624             if (isNull())
625             {
626                 return 0;
627             }
628             else if (isByte())
629             {
630                 return ((Byte) valueObject).byteValue();
631             }
632             else if (isString())
633             {
634                 return Integer.valueOf((String) valueObject).byteValue();
635             }
636             else if (isShort())
637             {
638                 return ((Short) valueObject).byteValue();
639             }
640             else if (isInt())
641             {
642                 return ((Integer) valueObject).byteValue();
643             }
644             else if (isLong())
645             {
646                 return ((Long) valueObject).byteValue();
647             }
648             else if (isDouble())
649             {
650                 return ((Double) valueObject).byteValue();
651             }
652             else if (isFloat())
653             {
654                 return ((Float) valueObject).byteValue();
655             }
656             else if (isBigDecimal())
657             {
658                 return ((BigDecimal) valueObject).byteValue();
659             }
660             else
661             {
662                 return Integer.valueOf(asString()).byteValue();
663             }
664         }
665         catch (Exception e)
666         {
667             throw new DataSetException("Bad conversion: " + e.toString());
668         }
669     }
670 
671     /***
672      * Get the value as a Byte Object
673      *
674      * @return a Byte
675      *
676      * @exception DataSetException
677      */
678     public Byte asByteObj()
679             throws DataSetException
680     {
681         try
682         {
683             if (isNull())
684             {
685                 return null;
686             }
687             else if (isByte())
688             {
689                 return ((Byte) valueObject);
690             }
691             else if (isString() || isDouble() || isFloat() || isInt()
692                     || isLong() || isShort() || isBigDecimal())
693             {
694                 return new Byte(asString());
695             }
696             else
697             {
698                 throw new DataSetException("Invalid type for Byte");
699             }
700         }
701         catch (Exception e)
702         {
703             throw new DataSetException("Illegal conversion: " + e.toString());
704         }
705     }
706 
707     /***
708      * Get the value as a asBytes
709      *
710      * @return a byte array
711      *
712      * @exception DataSetException
713      */
714     public byte [] asBytes()
715             throws DataSetException
716     {
717         try
718         {
719             if (isNull())
720             {
721                 return new byte[0];
722             }
723             else if (isBytes())
724             {
725                 return (byte []) valueObject;
726             }
727             else if (isString())
728             {
729                 return ((String) valueObject).getBytes();
730             }
731         }
732         catch (Exception e)
733         {
734             throw new DataSetException("Bad conversion: " + e.toString());
735         }
736 
737         return new byte[0];
738     }
739 
740     /***
741      * Get the value as a asShort
742      *
743      * @return a short
744      *
745      * @exception DataSetException
746      */
747     public short asShort()
748             throws DataSetException
749     {
750         try
751         {
752             if (isNull())
753             {
754                 return 0;
755             }
756             else if (isShort())
757             {
758                 return ((Short) valueObject).shortValue();
759             }
760             else if (isString())
761             {
762                 return Integer.valueOf((String) valueObject).shortValue();
763             }
764             else if (isInt())
765             {
766                 return ((Integer) valueObject).shortValue();
767             }
768             else if (isLong())
769             {
770                 return ((Long) valueObject).shortValue();
771             }
772             else if (isDouble())
773             {
774                 return ((Double) valueObject).shortValue();
775             }
776             else if (isFloat())
777             {
778                 return ((Float) valueObject).shortValue();
779             }
780             else if (isBigDecimal())
781             {
782                 return ((BigDecimal) valueObject).shortValue();
783             }
784             else
785             {
786                 return Integer.valueOf(asString()).shortValue();
787             }
788         }
789         catch (Exception e)
790         {
791             throw new DataSetException("Bad conversion: " + e.toString());
792         }
793     }
794 
795     /***
796      * Get the value as a Short Object
797      *
798      * @return a Short
799      *
800      * @exception DataSetException
801      */
802     public Short asShortObj()
803             throws DataSetException
804     {
805         try
806         {
807             if (isNull())
808             {
809                 return null;
810             }
811             else if (isShort())
812             {
813                 return ((Short) valueObject);
814             }
815             else if (isString() || isDouble() || isFloat() || isInt()
816                     || isLong() || isBigDecimal() || isByte())
817             {
818                 return new Short(asString());
819             }
820             else
821             {
822                 throw new DataSetException("Invalid type for Short");
823             }
824         }
825         catch (Exception e)
826         {
827             throw new DataSetException("Illegal conversion: " + e.toString());
828         }
829     }
830 
831     /***
832      * Get the value as a asLong
833      *
834      * @return a long
835      *
836      * @exception DataSetException
837      */
838     public long asLong()
839             throws DataSetException
840     {
841         try
842         {
843             if (isNull())
844             {
845                 return 0;
846             }
847             else if (isLong())
848             {
849                 return ((Long) valueObject).longValue();
850             }
851             else if (isString())
852             {
853                 return Integer.valueOf((String) valueObject).longValue();
854             }
855             else if (isShort())
856             {
857                 return ((Short) valueObject).longValue();
858             }
859             else if (isInt())
860             {
861                 return ((Integer) valueObject).longValue();
862             }
863             else if (isDouble())
864             {
865                 return ((Double) valueObject).longValue();
866             }
867             else if (isFloat())
868             {
869                 return ((Float) valueObject).longValue();
870             }
871             else if (isBigDecimal())
872             {
873                 return ((BigDecimal) valueObject).longValue();
874             }
875             else
876             {
877                 return Integer.valueOf(asString()).longValue();
878             }
879         }
880         catch (Exception e)
881         {
882             throw new DataSetException("Bad conversion: " + e.toString());
883         }
884     }
885 
886     /***
887      * Get the value as a Long Object
888      *
889      * @return a Long
890      *
891      * @exception DataSetException
892      */
893     public Long asLongObj()
894             throws DataSetException
895     {
896         try
897         {
898             if (isNull())
899             {
900                 return null;
901             }
902             else if (isLong())
903             {
904                 return ((Long) valueObject);
905             }
906             else if (isString() || isDouble() || isFloat() || isInt()
907                     || isBigDecimal() || isShort() || isByte())
908             {
909                 return new Long(asString());
910             }
911             else
912             {
913                 throw new DataSetException("Invalid type for Long");
914             }
915         }
916         catch (Exception e)
917         {
918             throw new DataSetException("Illegal conversion: " + e.toString());
919         }
920     }
921 
922     /***
923      * Get the value as a asDouble
924      *
925      * @return a double
926      *
927      * @exception DataSetException
928      */
929     public double asDouble()
930             throws DataSetException
931     {
932         try
933         {
934             if (isNull())
935             {
936                 return 0.0D;
937             }
938             else if (isDouble())
939             {
940                 return ((Double) valueObject).doubleValue();
941             }
942             else if (isString())
943             {
944                 return Integer.valueOf((String) valueObject).doubleValue();
945             }
946             else if (isShort())
947             {
948                 return ((Short) valueObject).doubleValue();
949             }
950             else if (isInt())
951             {
952                 return ((Integer) valueObject).doubleValue();
953             }
954             else if (isLong())
955             {
956                 return ((Long) valueObject).doubleValue();
957             }
958             else if (isFloat())
959             {
960                 return ((Float) valueObject).doubleValue();
961             }
962             else if (isBigDecimal())
963             {
964                 return ((BigDecimal) valueObject).doubleValue();
965             }
966             else
967             {
968                 return Integer.valueOf(asString()).doubleValue();
969             }
970         }
971         catch (Exception e)
972         {
973             throw new DataSetException("Bad conversion: " + e.toString());
974         }
975     }
976 
977     /***
978      * Get the value as a Double Object
979      *
980      * @return a Double
981      *
982      * @exception DataSetException
983      */
984     public Double asDoubleObj()
985             throws DataSetException
986     {
987         try
988         {
989             if (isNull())
990             {
991                 return null;
992             }
993             else if (isDouble())
994             {
995                 return ((Double) valueObject);
996             }
997             else if (isString() || isBigDecimal() || isFloat() || isInt()
998                     || isLong() || isShort() || isByte())
999             {
1000                 return new Double(asString());
1001             }
1002             else
1003             {
1004                 throw new DataSetException("Invalid type for Double");
1005             }
1006         }
1007         catch (Exception e)
1008         {
1009             throw new DataSetException("Illegal conversion: " + e.toString());
1010         }
1011     }
1012 
1013     /***
1014      * Get the value as a asFloat
1015      *
1016      * @return a float
1017      *
1018      * @exception DataSetException
1019      */
1020     public float asFloat()
1021             throws DataSetException
1022     {
1023         try
1024         {
1025             if (isNull())
1026             {
1027                 return 0.0F;
1028             }
1029             else if (isFloat())
1030             {
1031                 return ((Float) valueObject).floatValue();
1032             }
1033             else if (isString())
1034             {
1035                 return Integer.valueOf((String) valueObject).floatValue();
1036             }
1037             else if (isShort())
1038             {
1039                 return ((Short) valueObject).floatValue();
1040             }
1041             else if (isInt())
1042             {
1043                 return ((Integer) valueObject).floatValue();
1044             }
1045             else if (isLong())
1046             {
1047                 return ((Long) valueObject).floatValue();
1048             }
1049             else if (isDouble())
1050             {
1051                 return ((Double) valueObject).floatValue();
1052             }
1053             else if (isBigDecimal())
1054             {
1055                 return ((BigDecimal) valueObject).floatValue();
1056             }
1057             else
1058             {
1059                 return Integer.valueOf(asString()).floatValue();
1060             }
1061         }
1062         catch (Exception e)
1063         {
1064             throw new DataSetException("Bad conversion: " + e.toString());
1065         }
1066     }
1067 
1068     /***
1069      * Get the value as a Float Obj
1070      *
1071      * @return a Float
1072      *
1073      * @exception DataSetException
1074      */
1075     public Float asFloatObj()
1076             throws DataSetException
1077     {
1078         try
1079         {
1080             if (isNull())
1081             {
1082                 return null;
1083             }
1084             else if (isFloat())
1085             {
1086                 return ((Float) valueObject);
1087             }
1088             else if (isString() || isDouble() || isBigDecimal() || isInt()
1089                     || isLong() || isShort() || isByte())
1090             {
1091                 return new Float(asString());
1092             }
1093             else
1094             {
1095                 throw new DataSetException("Invalid type for Float");
1096             }
1097         }
1098         catch (Exception e)
1099         {
1100             throw new DataSetException("Illegal conversion: " + e.toString());
1101         }
1102     }
1103 
1104     /***
1105      * Get the value as a asTime
1106      *
1107      * @return a Time
1108      *
1109      * @exception DataSetException
1110      */
1111     public Time asTime()
1112             throws DataSetException
1113     {
1114         try
1115         {
1116             if (isNull())
1117             {
1118                 return null;
1119             }
1120             else if (isTime())
1121             {
1122                 return (Time) valueObject;
1123             }
1124 
1125             Calendar cal = Calendar.getInstance();
1126 
1127             if (isTimestamp())
1128             {
1129                 cal.setTime((Timestamp) valueObject);
1130 
1131                 return new Time(cal.getTime().getTime());
1132             }
1133             else if (isUtilDate())
1134             {
1135                 cal.setTime((java.util.Date) valueObject);
1136 
1137                 return new Time(cal.getTime().getTime());
1138             }
1139             else if (isString())
1140             {
1141                 return Time.valueOf((String) valueObject);
1142             }
1143             else
1144             {
1145                 return Time.valueOf(asString());
1146             }
1147         }
1148         catch (IllegalArgumentException a)
1149         {
1150             throw new DataSetException("Bad date value - "
1151                     + "Java Time Objects cannot be earlier than 1/1/70");
1152         }
1153         catch (Exception b)
1154         {
1155             throw new DataSetException("Bad conversion: " + b.toString());
1156         }
1157     }
1158 
1159     /***
1160      * Get the value as a asTimestamp
1161      *
1162      * @return a Timestamp
1163      *
1164      * @exception DataSetException
1165      */
1166     public Timestamp asTimestamp()
1167             throws DataSetException
1168     {
1169         try
1170         {
1171             if (isNull())
1172             {
1173                 return null;
1174             }
1175             else if (isTimestamp())
1176             {
1177                 return (Timestamp) valueObject;
1178             }
1179 
1180             if (isTime())
1181             {
1182                 Calendar cal = Calendar.getInstance();
1183                 cal.setTime((Time) valueObject);
1184 
1185                 return new Timestamp(cal.getTime().getTime());
1186             }
1187             else if (isUtilDate())
1188             {
1189                 return new Timestamp(((java.util.Date) valueObject).getTime());
1190             }
1191             else if (isString())
1192             {
1193                 return Timestamp.valueOf((String) valueObject);
1194             }
1195             else
1196             {
1197                 return Timestamp.valueOf(asString());
1198             }
1199         }
1200         catch (IllegalArgumentException a)
1201         {
1202             throw new DataSetException("Bad date value - "
1203                     + "Java Timestamp Objects cannot be earlier than 1/1/70");
1204         }
1205         catch (Exception b)
1206         {
1207             throw new DataSetException("Bad conversion: " + b.toString());
1208         }
1209     }
1210 
1211     /***
1212      * Get the value as a asDate
1213      *
1214      * @return a java.sql.Date
1215      *
1216      * @exception DataSetException
1217      */
1218     public java.sql.Date asDate()
1219             throws DataSetException
1220     {
1221         try
1222         {
1223             if (isNull())
1224             {
1225                 return null;
1226             }
1227             else if (isDate())
1228             {
1229                 return (java.sql.Date) valueObject;
1230             }
1231 
1232             Calendar cal = Calendar.getInstance();
1233 
1234             if (isTimestamp())
1235             {
1236                 Timestamp ts = (Timestamp) valueObject;
1237                 long date = ts.getTime();
1238                 int nanos = ts.getNanos();
1239 
1240                 return new java.sql.Date(date + (nanos / 1000000));
1241             }
1242             else if (isTime())
1243             {
1244                 cal.setTime((Time) valueObject);
1245 
1246                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-"
1247                         + leadingZero(cal.get(Calendar.MONTH) + 1) + "-"
1248                         + leadingZero(cal.get(Calendar.DAY_OF_MONTH)));
1249             }
1250             else if (isUtilDate())
1251             {
1252                 cal.setTime((java.util.Date) valueObject);
1253 
1254                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-"
1255                         + leadingZero(cal.get(Calendar.MONTH) + 1) + "-"
1256                         + leadingZero(cal.get(Calendar.DAY_OF_MONTH)));
1257             }
1258             else if (isString())
1259             {
1260                 return java.sql.Date.valueOf((String) valueObject);
1261             }
1262             else
1263             {
1264                 return java.sql.Date.valueOf(asString());
1265             }
1266         }
1267         catch (IllegalArgumentException a)
1268         {
1269             throw new DataSetException("Bad date value - "
1270                     + "Java Timestamp Objects cannot be earlier than 1/1/70");
1271         }
1272         catch (Exception b)
1273         {
1274             throw new DataSetException("Bad conversion: " + b.toString());
1275         }
1276     }
1277 
1278     /***
1279      * Get the value as a asUtilDate
1280      *
1281      * @return a java.util.Date
1282      *
1283      * @exception DataSetException
1284      */
1285     public java.util.Date asUtilDate()
1286             throws DataSetException
1287     {
1288         try
1289         {
1290             if (isNull())
1291             {
1292                 return null;
1293             }
1294             else if (isUtilDate())
1295             {
1296                 return (java.util.Date) valueObject;
1297             }
1298 
1299             Calendar cal = Calendar.getInstance();
1300 
1301             if (isTimestamp())
1302             {
1303                 Timestamp ts = (Timestamp) valueObject;
1304                 long date = ts.getTime();
1305                 int nanos = ts.getNanos();
1306 
1307                 return new java.util.Date(date + (nanos / 1000000));
1308             }
1309             else if (isTime())
1310             {
1311                 cal.setTime((Time) valueObject);
1312 
1313                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-"
1314                         + leadingZero(cal.get(Calendar.MONTH) + 1) + "-"
1315                         + leadingZero(cal.get(Calendar.DAY_OF_MONTH)));
1316             }
1317             else if (isUtilDate())
1318             {
1319                 cal.setTime((java.util.Date) valueObject);
1320 
1321                 return java.sql.Date.valueOf(cal.get(Calendar.YEAR) + "-"
1322                         + leadingZero(cal.get(Calendar.MONTH) + 1) + "-"
1323                         + leadingZero(cal.get(Calendar.DAY_OF_MONTH)));
1324             }
1325             else
1326             {
1327                 return null;
1328             }
1329         }
1330         catch (IllegalArgumentException a)
1331         {
1332             throw new DataSetException("Bad date value - "
1333                     + "Java java.util.Date Objects cannot be earlier than 1/1/70");
1334         }
1335         catch (Exception b)
1336         {
1337             throw new DataSetException("Bad conversion: " + b.toString());
1338         }
1339     }
1340 
1341     /***
1342      * Is the value a isBigDecimal
1343      *
1344      * @return true if BigDecimal
1345      */
1346     public boolean isBigDecimal()
1347     {
1348         return valueObject instanceof BigDecimal;
1349     }
1350 
1351     /***
1352      * Is the value a isByte
1353      *
1354      * @return true if is Byte
1355      */
1356     public boolean isByte()
1357     {
1358         return valueObject instanceof Byte;
1359     }
1360 
1361     /***
1362      * Is the value a isBytes
1363      *
1364      * @return true if is byte[]
1365      */
1366     public boolean isBytes()
1367     {
1368         return valueObject instanceof byte [];
1369     }
1370 
1371     /***
1372      * Is the value a isDate
1373      *
1374      * @return true if is java.sql.Date
1375      */
1376     public boolean isDate()
1377     {
1378         return valueObject instanceof java.sql.Date;
1379     }
1380 
1381     /***
1382      * Is the value a isShort
1383      *
1384      * @return true if is Short
1385      */
1386     public boolean isShort()
1387     {
1388         return valueObject instanceof Short;
1389     }
1390 
1391     /***
1392      * Is the value a isInt
1393      *
1394      * @return true if is Integer
1395      */
1396     public boolean isInt()
1397     {
1398         return valueObject instanceof Integer;
1399     }
1400 
1401     /***
1402      * Is the value a isLong
1403      *
1404      * @return true if is Long
1405      */
1406     public boolean isLong()
1407     {
1408         return valueObject instanceof Long;
1409     }
1410 
1411     /***
1412      * Is the value a isDouble
1413      *
1414      * @return true if is Double
1415      */
1416     public boolean isDouble()
1417     {
1418         return valueObject instanceof Double;
1419     }
1420 
1421     /***
1422      * Is the value a isFloat
1423      *
1424      * @return true if is Float
1425      */
1426     public boolean isFloat()
1427     {
1428         return valueObject instanceof Float;
1429     }
1430 
1431     /***
1432      * Is the value a isBoolean
1433      *
1434      * @return true if is Boolean
1435      */
1436     public boolean isBoolean()
1437     {
1438         return valueObject instanceof Boolean;
1439     }
1440 
1441     /***
1442      * Is the value a isNull
1443      *
1444      * @return true if is null
1445      */
1446     public boolean isNull()
1447     {
1448         return valueObject == null;
1449     }
1450 
1451     /***
1452      * Is the value a isString
1453      *
1454      * @return true if is String
1455      */
1456     public boolean isString()
1457     {
1458         return valueObject instanceof String;
1459     }
1460 
1461     /***
1462      * Is the value a isTime
1463      *
1464      * @return true if is java.sql.Time
1465      */
1466     public boolean isTime()
1467     {
1468         return valueObject instanceof java.sql.Time;
1469     }
1470 
1471     /***
1472      * Is the value a isTimestamp
1473      *
1474      * @return true if is java.sql.Timestamp
1475      */
1476     public boolean isTimestamp()
1477     {
1478         return valueObject instanceof java.sql.Timestamp;
1479     }
1480 
1481     /***
1482      * Is the value a isUtilDate
1483      *
1484      * @return true if is java.util.Date
1485      */
1486     public boolean isUtilDate()
1487     {
1488         return valueObject instanceof java.util.Date;
1489     }
1490 
1491     /***
1492      * Return the type of this value
1493      *
1494      * @return the type of this value
1495      */
1496     public int type()
1497     {
1498         return this.type;
1499     }
1500 
1501     /***
1502      * Gets the columnNumber which this value represents.
1503      *
1504      * @return an int
1505      */
1506     int columnNumber()
1507     {
1508         return this.columnNumber;
1509     }
1510 
1511     /***
1512      * DOCUMENT ME!
1513      *
1514      * @param value TODO: DOCUMENT ME!
1515      *
1516      * @return true if (true || t | yes | y | 1)
1517      */
1518     private boolean isTrue(String value)
1519     {
1520         return (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t")
1521                 || value.equalsIgnoreCase("yes")
1522         || value.equalsIgnoreCase("y") || value.equals("1"));
1523     }
1524 
1525     /***
1526      * Convert an int to a two digit String with a leading zero where necessary.
1527      *
1528      * @param val The value to be converted.
1529      * @return A two character String with leading zero.
1530      */
1531     private String leadingZero(int val)
1532     {
1533         return (val < 10 ? "0" : "") + val;
1534     }
1535 }