FormattableUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.text;

  18. import java.util.Formattable;
  19. import java.util.FormattableFlags;
  20. import java.util.Formatter;

  21. import org.apache.commons.lang3.ObjectUtils;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.apache.commons.lang3.Validate;

  24. /**
  25.  * Provides utilities for working with the {@link Formattable} interface.
  26.  *
  27.  * <p>The {@link Formattable} interface provides basic control over formatting
  28.  * when using a {@link Formatter}. It is primarily concerned with numeric precision
  29.  * and padding, and is not designed to allow generalised alternate formats.</p>
  30.  *
  31.  * @since 3.0
  32.  * @deprecated As of 3.6, use Apache Commons Text
  33.  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/FormattableUtils.html">
  34.  * FormattableUtils</a> instead
  35.  */
  36. @Deprecated
  37. public class FormattableUtils {

  38.     /**
  39.      * A format that simply outputs the value as a string.
  40.      */
  41.     private static final String SIMPLEST_FORMAT = "%s";

  42.     /**
  43.      * Handles the common {@link Formattable} operations of truncate-pad-append,
  44.      * with no ellipsis on precision overflow, and padding width underflow with
  45.      * spaces.
  46.      *
  47.      * @param seq  the string to handle, not null
  48.      * @param formatter  the destination formatter, not null
  49.      * @param flags  the flags for formatting, see {@link Formattable}
  50.      * @param width  the width of the output, see {@link Formattable}
  51.      * @param precision  the precision of the output, see {@link Formattable}
  52.      * @return the {@code formatter} instance, not null
  53.      */
  54.     public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
  55.             final int precision) {
  56.         return append(seq, formatter, flags, width, precision, ' ', null);
  57.     }

  58.     /**
  59.      * Handles the common {@link Formattable} operations of truncate-pad-append,
  60.      * with no ellipsis on precision overflow.
  61.      *
  62.      * @param seq  the string to handle, not null
  63.      * @param formatter  the destination formatter, not null
  64.      * @param flags  the flags for formatting, see {@link Formattable}
  65.      * @param width  the width of the output, see {@link Formattable}
  66.      * @param precision  the precision of the output, see {@link Formattable}
  67.      * @param padChar  the pad character to use
  68.      * @return the {@code formatter} instance, not null
  69.      */
  70.     public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
  71.             final int precision, final char padChar) {
  72.         return append(seq, formatter, flags, width, precision, padChar, null);
  73.     }

  74.     /**
  75.      * Handles the common {@link Formattable} operations of truncate-pad-append.
  76.      *
  77.      * @param seq  the string to handle, not null
  78.      * @param formatter  the destination formatter, not null
  79.      * @param flags  the flags for formatting, see {@link Formattable}
  80.      * @param width  the width of the output, see {@link Formattable}
  81.      * @param precision  the precision of the output, see {@link Formattable}
  82.      * @param padChar  the pad character to use
  83.      * @param ellipsis  the ellipsis to use when precision dictates truncation, null or
  84.      *  empty causes a hard truncation
  85.      * @return the {@code formatter} instance, not null
  86.      */
  87.     public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
  88.             final int precision, final char padChar, final CharSequence ellipsis) {
  89.         Validate.isTrue(ellipsis == null || precision < 0 || ellipsis.length() <= precision,
  90.                 "Specified ellipsis '%1$s' exceeds precision of %2$s", ellipsis, Integer.valueOf(precision));
  91.         final StringBuilder buf = new StringBuilder(seq);
  92.         if (precision >= 0 && precision < seq.length()) {
  93.             final CharSequence actualEllipsis = ObjectUtils.defaultIfNull(ellipsis, StringUtils.EMPTY);
  94.             buf.replace(precision - actualEllipsis.length(), seq.length(), actualEllipsis.toString());
  95.         }
  96.         final boolean leftJustify = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY;
  97.         for (int i = buf.length(); i < width; i++) {
  98.             buf.insert(leftJustify ? i : 0, padChar);
  99.         }
  100.         formatter.format(buf.toString());
  101.         return formatter;
  102.     }

  103.     /**
  104.      * Handles the common {@link Formattable} operations of truncate-pad-append,
  105.      * padding width underflow with spaces.
  106.      *
  107.      * @param seq  the string to handle, not null
  108.      * @param formatter  the destination formatter, not null
  109.      * @param flags  the flags for formatting, see {@link Formattable}
  110.      * @param width  the width of the output, see {@link Formattable}
  111.      * @param precision  the precision of the output, see {@link Formattable}
  112.      * @param ellipsis  the ellipsis to use when precision dictates truncation, null or
  113.      *  empty causes a hard truncation
  114.      * @return the {@code formatter} instance, not null
  115.      */
  116.     public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
  117.             final int precision, final CharSequence ellipsis) {
  118.         return append(seq, formatter, flags, width, precision, ' ', ellipsis);
  119.     }

  120.     /**
  121.      * Gets the default formatted representation of the specified
  122.      * {@link Formattable}.
  123.      *
  124.      * @param formattable  the instance to convert to a string, not null
  125.      * @return the resulting string, not null
  126.      */
  127.     public static String toString(final Formattable formattable) {
  128.         return String.format(SIMPLEST_FORMAT, formattable);
  129.     }

  130.     /**
  131.      * {@link FormattableUtils} instances should NOT be constructed in
  132.      * standard programming. Instead, the methods of the class should be invoked
  133.      * statically.
  134.      *
  135.      * <p>This constructor is public to permit tools that require a JavaBean
  136.      * instance to operate.</p>
  137.      */
  138.     public FormattableUtils() {
  139.     }

  140. }