verhas commented on a change in pull request #446: LANG-1480 getAbbreviatedName refactored to create appropriate length … URL: https://github.com/apache/commons-lang/pull/446#discussion_r325831427
########## File path: src/main/java/org/apache/commons/lang3/ClassUtils.java ########## @@ -433,48 +440,83 @@ public static String getAbbreviatedName(final Class<?> cls, final int len) { * <tr><td>"java.lang.String"</td><td> 5</td><td>"j.l.String"</td></tr> * <tr><td>"java.lang.String"</td><td>15</td><td>"j.lang.String"</td></tr> * <tr><td>"java.lang.String"</td><td>30</td><td>"java.lang.String"</td></tr> + * <tr><td>"org.apache.commons.lang3.ClassUtils"</td><td>18</td><td>"o.a.c.l.ClassUtils"</td></tr> * </table> - * @param className the className to get the abbreviated name for, may be {@code null} - * @param len the desired length of the abbreviated name - * @return the abbreviated name or an empty string - * @throws IllegalArgumentException if len <= 0 + * + * @param className the className to get the abbreviated name for, may be {@code null} + * @param len the desired length of the abbreviated name + * @return the abbreviated name or an empty string if the specified + * class name is {@code null} or empty string. The abbreviated name may be + * longer than the desired length if it cannot be abbreviated to the desired length. + * @throws IllegalArgumentException if {@code len <= 0} * @since 3.4 */ public static String getAbbreviatedName(final String className, final int len) { - if (len <= 0) { - throw new IllegalArgumentException("len must be > 0"); - } - if (className == null) { - return StringUtils.EMPTY; - } - - int availableSpace = len; - final int packageLevels = StringUtils.countMatches(className, '.'); - final String[] output = new String[packageLevels + 1]; - int endIndex = className.length() - 1; - for (int level = packageLevels; level >= 0; level--) { - final int startIndex = className.lastIndexOf('.', endIndex); - final String part = className.substring(startIndex + 1, endIndex + 1); - availableSpace -= part.length(); - if (level > 0) { - // all elements except top level require an additional char space - availableSpace--; + if (len <= 0) { + throw new IllegalArgumentException("len must be > 0"); } - if (level == packageLevels) { - // ClassName is always complete - output[level] = part; - } else { - if (availableSpace > 0) { - output[level] = part; - } else { - // if no space is left still the first char is used - output[level] = part.substring(0, 1); - } + if (className == null) { + return StringUtils.EMPTY; } Review comment: Makes sense. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services