1 43 44 package org.exolab.jms.selector; 45 46 47 59 final class TypeCaster { 60 61 64 private TypeCaster() { 65 } 66 67 75 public static SBool castToBool(final SObject obj, final String context) 76 throws TypeMismatchException { 77 78 SBool result = null; 79 if (obj instanceof SBool) { 80 result = (SBool) obj; 81 } else if (obj != null) { 82 typeMismatch(Type.BOOLEAN, obj, context); 83 } 84 return result; 85 } 86 87 95 public static SNumber castToNumber(final SObject obj, final String context) 96 throws TypeMismatchException { 97 98 SNumber result = null; 99 if (obj instanceof SNumber) { 100 result = (SNumber) obj; 101 } else if (obj != null) { 102 typeMismatch(Type.NUMERIC, obj, context); 103 } 104 return result; 105 } 106 107 115 public static SString castToString(final SObject obj, final String context) 116 throws TypeMismatchException { 117 118 SString result = null; 119 if (obj instanceof SString) { 120 result = (SString) obj; 121 } else if (obj != null) { 122 typeMismatch(Type.STRING, obj, context); 123 } 124 return result; 125 } 126 127 136 private static void typeMismatch(final Type expected, final SObject value, 137 final String context) 138 throws TypeMismatchException { 139 140 StringBuffer msg = new StringBuffer (); 141 msg.append("expecting a "); 142 msg.append(expected); 143 msg.append(" expression"); 144 if (context != null) { 145 msg.append(" for "); 146 msg.append(context); 147 } 148 msg.append(", found a "); 149 msg.append(value.type()); 150 throw new TypeMismatchException(msg.toString()); 151 } 152 153 } | Popular Tags |