@Override protected String doBackward(String s) { return targetFormat.to(sourceFormat, s); }
/** * Enum values can override for performance reasons. */ String convert(CaseFormat format, String s) { // deal with camel conversion StringBuilder out = null; int i = 0; int j = -1; while ((j = wordBoundary.indexIn(s, ++j)) != -1) { if (i == 0) { // include some extra space for separators out = new StringBuilder(s.length() + 4 * wordSeparator.length()); out.append(format.normalizeFirstWord(s.substring(i, j))); } else { out.append(format.normalizeWord(s.substring(i, j))); } out.append(format.wordSeparator); i = j + wordSeparator.length(); } return (i == 0) ? format.normalizeFirstWord(s) : out.append(format.normalizeWord(s.substring(i))).toString(); }
@Override String convert(CaseFormat format, String s) { if (format == LOWER_UNDERSCORE) { return s.replace('-', '_'); } if (format == UPPER_UNDERSCORE) { return Ascii.toUpperCase(s.replace('-', '_')); } return super.convert(format, s); } },
private String normalizeFirstWord(String word) { return (this == LOWER_CAMEL) ? Ascii.toLowerCase(word) : normalizeWord(word); }
@Override String convert(CaseFormat format, String s) { if (format == LOWER_HYPHEN) { return s.replace('_', '-'); } if (format == UPPER_UNDERSCORE) { return Ascii.toUpperCase(s); } return super.convert(format, s); } },
@Override protected String doForward(String s) { return sourceFormat.to(targetFormat, s); }
@Override String convert(CaseFormat format, String s) { if (format == LOWER_HYPHEN) { return Ascii.toLowerCase(s.replace('_', '-')); } if (format == LOWER_UNDERSCORE) { return Ascii.toLowerCase(s); } return super.convert(format, s); } };
/** * Converts a field mask to a Proto3 JSON string, that is converting from snake case to camel * case and joining all paths into one string with commas. */ public static String toJsonString(FieldMask fieldMask) { List<String> paths = new ArrayList<String>(fieldMask.getPathsCount()); for (String path : fieldMask.getPathsList()) { if (path.isEmpty()) { continue; } paths.add(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, path)); } return Joiner.on(FIELD_PATH_SEPARATOR).join(paths); }
/** * Converts the specified {@code String str} from this format to the specified {@code format}. A * "best effort" approach is taken; if {@code str} does not conform to the assumed format, then * the behavior of this method is undefined but we make a reasonable effort at converting anyway. */ public final String to(CaseFormat format, String str) { checkNotNull(format); checkNotNull(str); return (format == this) ? str : convert(format, str); }
/** * Converts a field mask from a Proto3 JSON string, that is splitting the paths along commas and * converting from camel case to snake case. */ public static FieldMask fromJsonString(String value) { Iterable<String> paths = Splitter.on(FIELD_PATH_SEPARATOR).split(value); FieldMask.Builder builder = FieldMask.newBuilder(); for (String path : paths) { if (path.isEmpty()) { continue; } builder.addPaths(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, path)); } return builder.build(); }