public static List < String > objectToStrings ( final Object inputValue ) { if ( inputValue == null ) { return Collections . emptyList ( ) ; } else if ( inputValue instanceof List ) { // guava's toString function fails on null objects, so please do not use it return ( ( List < ? > ) inputValue ) . stream ( ) . map ( String :: valueOf ) . collect ( Collectors . toList ( ) ) ; } else if ( inputValue instanceof byte [ ] ) { // convert byte[] to base64 encoded string return Collections . singletonList ( StringUtils . encodeBase64String ( ( byte [ ] ) inputValue ) ) ; } else { return Collections . singletonList ( String . valueOf ( inputValue ) ) ; } } /** * Convert an object to a number. Nulls are treated as zeroes unless * druid.generic.useDefaultValueForNull is set to false. * * @param name field name of the object being converted (may be used for exception messages) * @param inputValue the actual object being converted * * @return a number * * @throws NullPointerException if the string is null * @throws ParseException if the column cannot be converted to a number */ @ Nullable public static Number objectToNumber ( final String name , final Object inputValue ) { if ( inputValue == null ) { return NullHandling . defaultLongValue ( ) ; } if ( inputValue instanceof Number ) { return ( Number ) inputValue ; } else if ( inputValue instanceof String ) { try { String metricValueString = StringUtils . removeChar ( ( ( String ) inputValue ) . trim ( ) , ' ' ) ; // Longs.tryParse() doesn't support leading '+', so we need to trim it ourselves metricValueString = trimLeadingPlusOfLongString ( metricValueString ) ; Long v = Longs . tryParse ( metricValueString ) ; // Do NOT use ternary operator here, because it makes Java to convert Long to Double if ( v != null ) { return v ; } else { return Double . valueOf ( metricValueString ) ; } } catch ( Exception e ) { throw new ParseException ( e , "Unable to parse value[%s] for field[%s]" , inputValue , name ) ; } } else { throw new ParseException ( "Unknown type[%s] for field" , inputValue . getClass ( ) , inputValue ) ; } }
Convert an object to a list of strings .