1 16 package org.apache.commons.betwixt.strategy; 17 18 import java.io.Serializable ; 19 import java.util.Date ; 20 21 27 public abstract class TypeBindingStrategy { 28 29 34 public static final TypeBindingStrategy DEFAULT = new Default(); 35 36 43 public abstract BindingType bindingType(Class type); 44 45 46 50 public static final class BindingType implements Serializable { 51 52 private static final int COMPLEX_INDICATOR = 1; 53 private static final int PRIMITIVE_INDICATOR = 2; 54 55 60 public static final BindingType COMPLEX = new BindingType(COMPLEX_INDICATOR); 61 62 67 public static final BindingType PRIMITIVE = new BindingType(PRIMITIVE_INDICATOR); 68 69 private int type; 70 71 private BindingType(int type) { 72 this.type = type; 73 } 74 75 76 79 public boolean equals(Object object) { 80 boolean result = false; 81 if (object instanceof BindingType) { 82 BindingType bindingType = (BindingType) object; 83 result = (type == bindingType.type); 84 } 85 return result; 86 } 87 88 91 public int hashCode() { 92 return type; 93 } 94 95 98 public String toString() { 99 StringBuffer buffer = new StringBuffer (); 100 buffer.append("BindingType: "); 101 switch (type) { 102 case (COMPLEX_INDICATOR): 103 buffer.append("COMPLEX"); 104 break; 105 106 case (PRIMITIVE_INDICATOR): 107 buffer.append("PRIMITIVE"); 108 break; 109 } 110 111 return buffer.toString(); 112 } 113 } 114 115 121 public static final class Default extends TypeBindingStrategy { 122 123 132 public BindingType bindingType(Class type) { 133 BindingType result = BindingType.COMPLEX; 134 if (isStandardPrimitive(type)) { 135 result = BindingType.PRIMITIVE; 136 } 137 138 return result; 139 } 140 141 146 protected boolean isStandardPrimitive(Class type) { 147 if ( type == null ) { 148 return false; 149 150 } else if ( type.isPrimitive() ) { 151 return true; 152 153 } else if ( type.equals( Object .class ) ) { 154 return false; 155 } 156 return type.getName().startsWith( "java.lang." ) 157 || Number .class.isAssignableFrom( type ) 158 || String .class.isAssignableFrom( type ) 159 || Date .class.isAssignableFrom( type ) 160 || java.sql.Date .class.isAssignableFrom( type ) 161 || java.sql.Time .class.isAssignableFrom( type ) 162 || java.sql.Timestamp .class.isAssignableFrom( type ) 163 || java.math.BigDecimal .class.isAssignableFrom( type ) 164 || java.math.BigInteger .class.isAssignableFrom( type ); 165 } 166 } 167 } 168 | Popular Tags |