/** Returns the string literal representing {@code value}, including wrapping double quotes. */ static String stringLiteralWithDoubleQuotes(String value, String indent) { StringBuilder result = new StringBuilder(value.length() + 2); result.append('"'); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); // trivial case: single quote must not be escaped if (c == '\'') { result.append("'"); continue; } // trivial case: double quotes must be escaped if (c == '\"') { result.append("\\\""); continue; } // default case: just let character literal do its work result.append(characterLiteralWithoutSingleQuotes(c)); // need to append indent after linefeed? if (c == '\n' && i + 1 < value.length()) { result.append("\"\n").append(indent).append(indent).append("+ \""); } } result.append('"'); return result.toString(); } }
/** * Delegates to {@link #addMember(String, String, Object...)}, with parameter {@code format} * depending on the given {@code value} object. Falls back to {@code "$L"} literal format if * the class of the given {@code value} object is not supported. */ Builder addMemberForValue(String memberName, Object value) { checkNotNull(memberName, "memberName == null"); checkNotNull(value, "value == null, constant non-null value expected for %s", memberName); checkArgument(SourceVersion.isName(memberName), "not a valid name: %s", memberName); if (value instanceof Class<?>) { return addMember(memberName, "$T.class", value); } if (value instanceof Enum) { return addMember(memberName, "$T.$L", value.getClass(), ((Enum<?>) value).name()); } if (value instanceof String) { return addMember(memberName, "$S", value); } if (value instanceof Float) { return addMember(memberName, "$Lf", value); } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } return addMember(memberName, "$L", value); }
/** Returns the string literal representing {@code value}, including wrapping double quotes. */ static String stringLiteralWithDoubleQuotes(String value, String indent) { StringBuilder result = new StringBuilder(value.length() + 2); result.append('"'); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); // trivial case: single quote must not be escaped if (c == '\'') { result.append("'"); continue; } // trivial case: double quotes must be escaped if (c == '\"') { result.append("\\\""); continue; } // default case: just let character literal do its work result.append(characterLiteralWithoutSingleQuotes(c)); // need to append indent after linefeed? if (c == '\n' && i + 1 < value.length()) { result.append("\"\n").append(indent).append(indent).append("+ \""); } } result.append('"'); return result.toString(); } }
/** Returns the string literal representing {@code value}, including wrapping double quotes. */ static String stringLiteralWithDoubleQuotes(String value, String indent) { StringBuilder result = new StringBuilder(value.length() + 2); result.append('"'); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); // trivial case: single quote must not be escaped if (c == '\'') { result.append("'"); continue; } // trivial case: double quotes must be escaped if (c == '\"') { result.append("\\\""); continue; } // default case: just let character literal do its work result.append(characterLiteralWithoutSingleQuotes(c)); // need to append indent after linefeed? if (c == '\n' && i + 1 < value.length()) { result.append("\"\n").append(indent).append(indent).append("+ \""); } } result.append('"'); return result.toString(); } }
/** * Delegates to {@link #addMember(String, String, Object...)}, with parameter {@code format} * depending on the given {@code value} object. Falls back to {@code "$L"} literal format if * the class of the given {@code value} object is not supported. */ Builder addMemberForValue(String memberName, Object value) { checkNotNull(memberName, "memberName == null"); checkNotNull(value, "value == null, constant non-null value expected for %s", memberName); if (value instanceof Class<?>) { return addMember(memberName, "$T.class", value); } if (value instanceof Enum) { return addMember(memberName, "$T.$L", value.getClass(), ((Enum<?>) value).name()); } if (value instanceof String) { return addMember(memberName, "$S", value); } if (value instanceof Float) { return addMember(memberName, "$Lf", value); } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } return addMember(memberName, "$L", value); }
/** * Delegates to {@link #addMember(String, String, Object...)}, with parameter {@code format} * depending on the given {@code value} object. Falls back to {@code "$L"} literal format if * the class of the given {@code value} object is not supported. */ Builder addMemberForValue(String memberName, Object value) { checkNotNull(memberName, "memberName == null"); checkNotNull(value, "value == null, constant non-null value expected for %s", memberName); if (value instanceof Class<?>) { return addMember(memberName, "$T.class", value); } if (value instanceof Enum) { return addMember(memberName, "$T.$L", value.getClass(), ((Enum<?>) value).name()); } if (value instanceof String) { return addMember(memberName, "$S", value); } if (value instanceof Float) { return addMember(memberName, "$Lf", value); } if (value instanceof Character) { return addMember(memberName, "'$L'", characterLiteralWithoutSingleQuotes((char) value)); } return addMember(memberName, "$L", value); }