1 46 package org.mr.api.jms.selector.syntax; 47 48 49 54 final class TypeCaster { 55 56 59 private TypeCaster() { 60 } 62 70 public static MantaBoolean castToBool(final MantaObject obj, final String context) throws TypeMismatchException { 71 72 MantaBoolean result = null; 73 74 if (obj instanceof MantaBoolean) { 75 result = (MantaBoolean) obj; 76 } else if (obj != null) { 78 throwNewTypeMismatchException(Type.BOOLEAN, obj, context); 79 } return result; 81 } 83 84 92 public static MantaNumber castToNumber(final MantaObject obj, final String context) throws TypeMismatchException { 93 94 MantaNumber result = null; 95 if (obj instanceof MantaNumber) { 96 result = (MantaNumber) obj; 97 } else if (obj != null) { 99 throwNewTypeMismatchException(Type.NUMERIC, obj, context); 100 } return result; 102 } 104 105 113 public static MantaString castToString(final MantaObject obj, final String context) throws TypeMismatchException { 114 115 MantaString result = null; 116 if (obj instanceof MantaString) { 117 result = (MantaString) obj; 118 } else if (obj != null) { 120 throwNewTypeMismatchException(Type.STRING, obj, context); 121 } return result; 123 } 125 126 134 private static void throwNewTypeMismatchException(final Type expected, final MantaObject value, final String context) throws TypeMismatchException { 135 136 StringBuffer msg = new StringBuffer (); 137 msg.append("expecting a "); 138 msg.append(expected); 139 msg.append(" expression"); 140 141 if (context != null) { 142 msg.append(" for "); 143 msg.append(context); 144 } msg.append(", found a "); 146 msg.append(value.type()); 147 throw new TypeMismatchException(msg.toString()); 148 } 150 } | Popular Tags |