View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class BasicTypeConverter 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:45 $ 11 * 12 * $Id: BasicTypeConverter.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.data.converter; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import net.jbeans.lang.*; 27 import net.jbeans.data.*; 28 29 /*==================================================================== 30 Implementation of class BasicTypeConverter 31 ====================================================================*/ 32 /*** 33 * generally describe BasicTypeConverter in here 34 * 35 * @version $Revision: 1.1.1.1 $ 36 * @author Masahiro Takatsuka (masa@jbeans.net) 37 * @see DataConverter 38 */ 39 40 public final class BasicTypeConverter extends DataConverter { 41 private static final int basicTypeCount = 8; 42 43 private static final int BYTE = 0; 44 private static final int DOUBLE = 1; 45 private static final int FLOAT = 2; 46 private static final int INTEGER = 3; 47 private static final int LONG = 4; 48 private static final int SHORT = 5; 49 private static final int BOOLEAN = 6; 50 private static final int STRING = 7; 51 52 /*** 53 * Initializes acceptable soruce and target types. 54 */ 55 protected void setDataTypes() { 56 this.sourceTypes = new Class[basicTypeCount]; 57 this.sourceTypes[BYTE] = java.lang.Byte.class; 58 this.sourceTypes[DOUBLE] = java.lang.Double.class; 59 this.sourceTypes[FLOAT] = java.lang.Float.class; 60 this.sourceTypes[INTEGER] = java.lang.Integer.class; 61 this.sourceTypes[LONG] = java.lang.Long.class; 62 this.sourceTypes[SHORT] = java.lang.Short.class; 63 this.sourceTypes[BOOLEAN] = java.lang.Boolean.class; 64 this.sourceTypes[STRING] = java.lang.String.class; 65 this.targetTypes = new Class[this.sourceTypes.length]; 66 for (int i = 0; i < this.sourceTypes.length; i++) { 67 this.targetTypes[i] = this.sourceTypes[i]; 68 } 69 } 70 71 /*** 72 * Converts the specified object "obj" into an object of a class "class1". 73 */ 74 public final Object convert(Object obj, Class class1) throws InvalidDataTypeException, UnsupportedTypeException { 75 Class objClass = obj.getClass(); 76 if (!isSourceDataTypeSupported(objClass)) { 77 throw new InvalidDataTypeException(objClass.getName() + " invalid data type for BasicTypeConverter"); 78 } 79 if (ClassUtil.isAssignable(class1, this.targetTypes[BYTE])) { // Byte 80 if (obj instanceof String) { 81 Double d = new Double((String)obj); 82 return new Byte(d.byteValue()); 83 } else if (obj instanceof Number) { 84 return new Byte(((Number)obj).byteValue()); 85 } else { 86 return new Byte((byte)((((Boolean)obj).booleanValue() == true)?1:0)); 87 } 88 } else if (ClassUtil.isAssignable(class1, this.targetTypes[DOUBLE])) { // Double 89 if (obj instanceof String) { 90 return new Double((String)obj); 91 } else if (obj instanceof Number) { 92 return new Double(((Number)obj).doubleValue()); 93 } else { 94 return new Double((((Boolean)obj).booleanValue() == true)?1.0:0.0); 95 } 96 } else if(ClassUtil.isAssignable(class1, this.targetTypes[FLOAT])) { // Float 97 if (obj instanceof String) { 98 return new Float((String)obj); 99 } else if (obj instanceof Number) { 100 return new Float(((Number)obj).floatValue()); 101 } else { 102 return new Float((((Boolean)obj).booleanValue() == true)?1.0F:0.0F); 103 } 104 } else if(ClassUtil.isAssignable(class1, this.targetTypes[INTEGER])) { // Integer 105 if (obj instanceof String) { 106 Double d = new Double((String)obj); 107 return new Integer(d.intValue()); 108 } else if (obj instanceof Number) { 109 return new Integer(((Number)obj).intValue()); 110 } else { 111 return new Integer((((Boolean)obj).booleanValue() == true)?1:0); 112 } 113 } else if(ClassUtil.isAssignable(class1, this.targetTypes[LONG])) { // Long 114 if (obj instanceof String) { 115 Double d = new Double((String)obj); 116 return new Long(d.longValue()); 117 } else if (obj instanceof Number) { 118 return new Long(((Number)obj).longValue()); 119 } else { 120 return new Long((((Boolean)obj).booleanValue() == true)?1:0); 121 } 122 } else if(ClassUtil.isAssignable(class1, this.targetTypes[SHORT])) { // Short 123 if (obj instanceof String) { 124 Double d = new Double((String)obj); 125 return new Short(d.shortValue()); 126 } else if (obj instanceof Number) { 127 return new Short(((Number)obj).shortValue()); 128 } else { 129 return new Short((short)((((Boolean)obj).booleanValue() == true)?1:0)); 130 } 131 } else if(ClassUtil.isAssignable(class1, this.targetTypes[BOOLEAN])) { // String 132 if (obj instanceof String) { 133 return new Boolean((String)obj); 134 } else if (obj instanceof Number) { 135 return new Boolean((((Number)obj).doubleValue() == 1.0)?true:false); 136 } else { 137 return new Boolean(((Boolean)obj).booleanValue()); 138 } 139 } else if(ClassUtil.isAssignable(class1, this.targetTypes[STRING])) { // String 140 if (obj instanceof String) { 141 return new String((String)obj); 142 } else { 143 return obj.toString(); 144 } 145 } else { 146 throw new UnsupportedTypeException("MtBasicTypeConverter: Unsupported output type"); 147 } 148 } 149 }

This page was automatically generated by Maven