Convert { public static Object stringToJava(String value) { if(value.isEmpty()) { return value; } char first = value.charAt(0); char last = value.charAt(value.length() - 1); Long record; if(AnyStrings.isWithinQuotes(value, TAG_MARKER)) { return value.substring(1, value.length() - 1); } else if(first == '@' && (record = Longs .tryParse(value.substring(1, value.length()))) != null) { return Link.to(record); } else if(first == '@' && last == '@' && STRING_RESOLVABLE_LINK_REGEX.matcher(value).matches()) { String ccl = value.substring(1, value.length() - 1); return ResolvableLink.create(ccl); } else if(value.equalsIgnoreCase("true")) { return true; } else if(value.equalsIgnoreCase("false")) { return false; } else if(first == TAG_MARKER && last == TAG_MARKER) { return Tag.create(value.substring(1, value.length() - 1)); } else if(first == '|' && last == '|') { value = value.substring(1, value.length() - 1); String[] toks = value.split("\\|"); Timestamp timestamp; if(toks.length == 1) { timestamp = Timestamp .fromMicros(NaturalLanguage.parseMicros(value)); } else { DateTimeFormatter formatter = DateTimeFormat .forPattern(toks[1]); timestamp = Timestamp.parse(toks[0], formatter); } return timestamp; } else { if(last == ')') { try { FunctionTree tree = (FunctionTree) ConcourseCompiler.get() .parse(value); FunctionValueSymbol symbol = (FunctionValueSymbol) tree .root(); return symbol.function(); } catch (Exception e) {} } try { return MoreObjects .firstNonNull(AnyStrings.tryParseNumber(value), value); } catch (NumberFormatException e) { return value; } } } private  Convert(); static List<Multimap<String, Object>> anyJsonToJava(String json); static TObject javaToThrift(Object object); static Multimap<String, Object> jsonToJava(String json); @SuppressWarnings("unchecked") static String mapsToJson(Collection<Multimap<String, Object>> list); static String mapToJson(Map<String, ?> map); static String mapToJson(Multimap<String, Object> map); static String operatorToString(Operator operator); @SuppressWarnings("unchecked") static T possibleThriftToJava(Object tobject); static Object stringToJava(String value); static Operator stringToOperator(String symbol); static String stringToResolvableLinkInstruction(String ccl); @Deprecated static String stringToResolvableLinkSpecification(String key,
            String rawValue); static Object thriftToJava(TObject object); static Object toFunctionOrUnescapedValueIfPossible(Object value);  }
@Test(expected = IllegalArgumentException.class) public void testConvertStringToTimestampNotMatchingFormat() { String string = "|December 30, 1987|MM/dd/yyyy|"; Assert.assertTrue(Convert.stringToJava(string) instanceof Timestamp); }
@Test public void testConvertStringDoubleInScientificNotation() { List<String> values = ImmutableList.of("5.15501576938E-4", "5.15501576938E+4", "1e10"); values.forEach(value -> { double expected = Double.parseDouble(value); Object actual = Convert.stringToJava(value); if(actual instanceof Float) { actual = ((Float) actual).doubleValue(); } Assert.assertEquals(expected, actual); }); }
@Test public void testConvertBigNumberStringStaysAsString() { String expected = "20191009162729766293175"; Object actual = Convert.stringToJava(expected); Assert.assertEquals(expected, actual); }
@Test public void testConvertStringToIndexFunction() { IndexFunction expected = new IndexFunction("average", "age"); Object actual = Convert.stringToJava("average(age)"); Assert.assertEquals(expected, actual); }
@Test public void testDontConvertQuotedIndexFunction() { String expected = "average(age)"; Object actual = Convert.stringToJava("'average(age)'"); Assert.assertEquals(expected, actual); }
@Test public void testCannotConvertLinkFromBooleanValue() { Boolean number = Random.getBoolean(); String value = MessageFormat.format("{0}{1}{0}", "@", number.toString()); Assert.assertFalse(Convert.stringToJava(value) instanceof Link); }
@Test public void testCannotConvertLinkFromDoubleValue() { Number number = Random.getDouble(); String value = MessageFormat.format("{0}{1}{0}", "@", number.toString()); Assert.assertFalse(Convert.stringToJava(value) instanceof Link); }
@Test public void testCannotConvertLinkFromFloatValue() { Number number = Random.getFloat(); String value = MessageFormat.format("{0}{1}{0}", "@", number.toString()); Assert.assertFalse(Convert.stringToJava(value) instanceof Link); }
@Test public void testCannotConvertLinkFromStringValue() { String number = Random.getString(); while (StringUtils.isNumeric(number)) { number = Random.getString(); } String value = MessageFormat.format("{0}{1}{0}", "@", number.toString()); Assert.assertFalse(Convert.stringToJava(value) instanceof Link); }
@Test public void testConvertBoolean() { Boolean bool = Random.getBoolean(); String boolString = scrambleCase(bool.toString()); Assert.assertEquals(bool, Convert.stringToJava(boolString)); }
@Test public void testConvertDouble() { Number number = Random.getDouble(); String string = number.toString() + "D"; Assert.assertEquals(number, Convert.stringToJava(string)); }
@Test public void testConvertFloat() { Number number = Random.getFloat(); String string = number.toString(); Assert.assertEquals(number, Convert.stringToJava(string)); }
@Test public void testConvertForcedStringDoubleQuotes() { Object object = Random.getObject(); String value = MessageFormat.format("{0}{1}{0}", "\"", object.toString()); Assert.assertEquals(Convert.stringToJava(value), object.toString()); }
@Test public void testConvertForcedStringSingleQuotes() { Object object = Random.getObject(); String value = MessageFormat.format("{0}{1}{0}", "'", object.toString()); Assert.assertEquals(Convert.stringToJava(value), object.toString()); }
@Test public void testConvertInteger() { Number number = Random.getInt(); String string = number.toString(); Assert.assertEquals(number, Convert.stringToJava(string)); }
@Test public void testConvertLinkFromIntValue() { Number number = Random.getInt(); String value = MessageFormat.format("{0}{1}", "@", number.toString()); Link link = (Link) Convert.stringToJava(value); Assert.assertEquals(number.intValue(), link.intValue()); }
@Test public void testConvertLinkFromLongValue() { Number number = Random.getLong(); String value = MessageFormat.format("{0}{1}", "@", number.toString()); Link link = (Link) Convert.stringToJava(value); Assert.assertEquals(number.longValue(), link.longValue()); }
@Test public void testConvertLong() { Number number = null; while (number == null || (Long) number <= Integer.MAX_VALUE) { number = Random.getLong(); } String string = number.toString(); Assert.assertEquals(number, Convert.stringToJava(string)); }
@Test public void testConvertTag() { String string = Random.getString(); while (string.contains("`")) { string = Random.getString(); } String value = MessageFormat.format("{0}{1}{0}", "`", string); Assert.assertTrue(Convert.stringToJava(value) instanceof Tag); }
@Test public void testConvertStringLinkToJava() { long record = Random.getLong(); String strLink = "@" + record; Link link = (Link) Convert.stringToJava(strLink); Assert.assertEquals(record, link.longValue()); }
@Test public void testConvertEmptyString() { Assert.assertEquals("", Convert.stringToJava("")); }
@Test public void testConvertStringToTimestamp() { String string = "|December 30, 1987|"; Assert.assertTrue(Convert.stringToJava(string) instanceof Timestamp); }
@Test(expected = IllegalArgumentException.class) public void testConvertStringToTimestampInvalidFormat() { String string = "|December 30, 1987|fsfsaf|"; Assert.assertTrue(Convert.stringToJava(string) instanceof Timestamp); }