Force a data object to a String, if possible. Any simple (atomic) type can be forced to a String including ByteArray. Complex types cannot be forced to a String. This isn't particularly efficient, so if you already know that the object you have is a String you should just cast it. @param o
(Object o,byte type)
| 1311 | * @throws ExecException if the type can't be forced to a String. |
| 1312 | */ |
| 1313 | public static String toString(Object o,byte type) throws ExecException { |
| 1314 | try { |
| 1315 | switch (type) { |
| 1316 | case INTEGER: |
| 1317 | return ((Integer)o).toString(); |
| 1318 | |
| 1319 | case LONG: |
| 1320 | return ((Long)o).toString(); |
| 1321 | |
| 1322 | case FLOAT: |
| 1323 | return ((Float)o).toString(); |
| 1324 | |
| 1325 | case DOUBLE: |
| 1326 | return ((Double)o).toString(); |
| 1327 | |
| 1328 | case DATETIME: |
| 1329 | return ((DateTime)o).toString(); |
| 1330 | |
| 1331 | case BYTEARRAY: |
| 1332 | return ((DataByteArray)o).toString(); |
| 1333 | |
| 1334 | case CHARARRAY: |
| 1335 | return ((String)o); |
| 1336 | |
| 1337 | case BIGINTEGER: |
| 1338 | return ((BigInteger)o).toString(); |
| 1339 | |
| 1340 | case BIGDECIMAL: |
| 1341 | return ((BigDecimal)o).toString(); |
| 1342 | |
| 1343 | case NULL: |
| 1344 | return null; |
| 1345 | |
| 1346 | case BOOLEAN: |
| 1347 | return ((Boolean)o).toString(); |
| 1348 | |
| 1349 | case BYTE: |
| 1350 | return ((Byte)o).toString(); |
| 1351 | |
| 1352 | case MAP: |
| 1353 | case INTERNALMAP: |
| 1354 | case TUPLE: |
| 1355 | case BAG: |
| 1356 | case UNKNOWN: |
| 1357 | default: |
| 1358 | int errCode = 1071; |
| 1359 | String msg = "Cannot convert a " + findTypeName(o) + |
| 1360 | " to a String"; |
| 1361 | throw new ExecException(msg, errCode, PigException.INPUT); |
| 1362 | } |
| 1363 | } catch (ClassCastException cce) { |
| 1364 | throw cce; |
| 1365 | } catch (ExecException ee) { |
| 1366 | throw ee; |
| 1367 | } catch (Exception e) { |
| 1368 | int errCode = 2054; |
| 1369 | String msg = "Internal error. Could not convert " + o + " to String."; |
| 1370 | throw new ExecException(msg, errCode, PigException.BUG); |