ProtoUtil { public static StringBuilder toUnderscoreCase(String name) { StringBuilder buffer = new StringBuilder(); boolean toLower = false, appendUnderscore = false; for (int i = 0, len = name.length(); i < len;) { char c = name.charAt(i++); if (c == '_') { if (i == len) break; if (buffer.length() != 0) appendUnderscore = true; continue; } if (appendUnderscore) buffer.append('_'); if (c > 96 && c < 123) { buffer.append(c); toLower = true; } else if (c > 64 && c < 91) { if (toLower) { if (!appendUnderscore) buffer.append('_'); toLower = false; } buffer.append((char) (c + 32)); } else { buffer.append(c); toLower = false; } appendUnderscore = false; } return buffer; } private  ProtoUtil(); static void loadFrom(ANTLRReaderStream input, Proto target); static void loadFrom(InputStream in, Proto target); static void loadFrom(Reader reader, Proto target); static Proto parseProto(File file); static void loadFrom(File file, Proto target); static void loadFrom(URL resource, Proto target); static StringBuilder toCamelCase(String name); static StringBuilder toPascalCase(String name); static StringBuilder toUnderscoreCase(String name);  }
@Test public void testToUnderscoreCase() throws Exception { assertEquals("foo_bar_baz", toUnderscoreCase("foo_bar_baz").toString()); assertEquals("foo_bar_baz", toUnderscoreCase("fooBarBaz").toString()); assertEquals("foo_bar_baz", toUnderscoreCase("FooBarBaz").toString()); assertEquals("foo_bar_baz", toUnderscoreCase("foo_bar_baz").toString()); assertEquals("foo_bar_baz", toUnderscoreCase("____Foo____Bar___Baz____").toString()); }