Skip to content

Commit

Permalink
Undeprecate ObjectUtils.toString(Object)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 9, 2025
1 parent 65112a2 commit aafb046
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
11 changes: 3 additions & 8 deletions src/main/java/org/apache/commons/lang3/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1264,8 +1264,7 @@ public static <T> T requireNonEmpty(final T obj, final String message) {
}

/**
* Gets the {@code toString} of an {@link Object} returning
* an empty string ("") if {@code null} input.
* Gets the {@code toString()} of an {@link Object} or the empty string ({@code ""}) if the input is {@code null}.
*
* <pre>
* ObjectUtils.toString(null) = ""
Expand All @@ -1278,14 +1277,10 @@ public static <T> T requireNonEmpty(final T obj, final String message) {
* @see Objects#toString(Object, String)
* @see StringUtils#defaultString(String)
* @see String#valueOf(Object)
* @param obj the Object to {@code toString}, may be null
* @return the passed in Object's toString, or {@code ""} if {@code null} input
* @param obj the Object to {@code toString()}, may be {@code null}.
* @return the input's {@code toString()}, or {@code ""} if the input is {@code null}.
* @since 2.0
* @deprecated this method has been replaced by {@code java.util.Objects.toString(Object)} in Java 7 and will be
* removed in future releases. Note however that said method will return "null" for null references, while this
* method returns an empty String. To preserve behavior use {@code java.util.Objects.toString(myObject, "")}
*/
@Deprecated
public static String toString(final Object obj) {
return Objects.toString(obj, StringUtils.EMPTY);
}
Expand Down
21 changes: 5 additions & 16 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4268,7 +4268,7 @@ public static String join(final Iterator<?> iterator, final char separator) {
if (!iterator.hasNext()) {
return EMPTY;
}
return Streams.of(iterator).collect(LangCollectors.joining(toString(String.valueOf(separator)), EMPTY, EMPTY, StringUtils::toString));
return Streams.of(iterator).collect(LangCollectors.joining(ObjectUtils.toString(String.valueOf(separator)), EMPTY, EMPTY, ObjectUtils::toString));
}

/**
Expand All @@ -4292,7 +4292,7 @@ public static String join(final Iterator<?> iterator, final String separator) {
if (!iterator.hasNext()) {
return EMPTY;
}
return Streams.of(iterator).collect(LangCollectors.joining(toString(separator), EMPTY, EMPTY, StringUtils::toString));
return Streams.of(iterator).collect(LangCollectors.joining(ObjectUtils.toString(separator), EMPTY, EMPTY, ObjectUtils::toString));
}

/**
Expand Down Expand Up @@ -4529,7 +4529,7 @@ public static String join(final Object[] array, final char delimiter, final int
* @return the joined String, {@code null} if null array input
*/
public static String join(final Object[] array, final String delimiter) {
return array != null ? join(array, toString(delimiter), 0, array.length) : null;
return array != null ? join(array, ObjectUtils.toString(delimiter), 0, array.length) : null;
}

/**
Expand Down Expand Up @@ -4569,7 +4569,7 @@ public static String join(final Object[] array, final String delimiter) {
*/
public static String join(final Object[] array, final String delimiter, final int startIndex, final int endIndex) {
return array != null ? Streams.of(array).skip(startIndex).limit(Math.max(0, endIndex - startIndex))
.collect(LangCollectors.joining(delimiter, EMPTY, EMPTY, StringUtils::toString)) : null;
.collect(LangCollectors.joining(delimiter, EMPTY, EMPTY, ObjectUtils::toString)) : null;
}

/**
Expand Down Expand Up @@ -6302,7 +6302,7 @@ public static String replaceChars(final String str, final String searchChars, St
if (isEmpty(str) || isEmpty(searchChars)) {
return str;
}
replaceChars = toString(replaceChars);
replaceChars = ObjectUtils.toString(replaceChars);
boolean modified = false;
final int replaceCharsLength = replaceChars.length();
final int strLength = str.length();
Expand Down Expand Up @@ -8760,17 +8760,6 @@ public static String toString(final byte[] bytes, final String charsetName) {
return new String(bytes, Charsets.toCharset(charsetName));
}

/**
* Returns the result of calling {@code toString} on argument if the argument is not {@code null} and returns the empty String otherwise.
*
* @param o an object
* @return the result of calling {@code toString} on the argument if it is not {@code null} and the empty String otherwise.
* @see Objects#toString(Object)
*/
private static String toString(final Object obj) {
return Objects.toString(obj, EMPTY);
}

/**
* Removes control characters (char &lt;= 32) from both
* ends of this String, handling {@code null} by returning
Expand Down

0 comments on commit aafb046

Please sign in to comment.