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.text;

  18. import static java.util.FormattableFlags.LEFT_JUSTIFY;

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

  21. import org.apache.commons.lang3.StringUtils;

  22. /**
  23.  * Provides utilities for working with the {@code Formattable} interface.
  24.  *
  25.  * <p>The {@link Formattable} interface provides basic control over formatting
  26.  * when using a {@code Formatter}. It is primarily concerned with numeric precision
  27.  * and padding, and is not designed to allow generalized alternate formats.</p>
  28.  *
  29.  * @since 1.0
  30.  */
  31. public class FormattableUtils {

  32.     /**
  33.      * A format that simply outputs the value as a string.
  34.      */
  35.     private static final String SIMPLEST_FORMAT = "%s";

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

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

  68.     /**
  69.      * Handles the common {@link Formattable} operations of truncate-pad-append.
  70.      *
  71.      * @param seq  the string to handle, not null
  72.      * @param formatter  the destination formatter, not null
  73.      * @param flags  the flags for formatting, see {@code Formattable}
  74.      * @param width  the width of the output, see {@code Formattable}
  75.      * @param precision  the precision of the output, see {@code Formattable}
  76.      * @param padChar  the pad character to use
  77.      * @param truncateEllipsis  the ellipsis to use when precision dictates truncation, null or
  78.      *  empty causes a hard truncation
  79.      * @return The {@code formatter} instance, not null
  80.      * @throws IllegalArgumentException if {@code ellipsis.length() > precision},
  81.      *  given that {@code ellipsis} is not null and {@code precision >= 0}
  82.      */
  83.     public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
  84.             final int precision, final char padChar, final CharSequence truncateEllipsis) {
  85.         if (!(truncateEllipsis == null || precision < 0 || truncateEllipsis.length() <= precision)) {
  86.             throw new IllegalArgumentException(
  87.                     String.format("Specified ellipsis '%s' exceeds precision of %s",
  88.                             truncateEllipsis,
  89.                             precision));
  90.         }
  91.         final StringBuilder buf = new StringBuilder(seq);
  92.         if (precision >= 0 && precision < seq.length()) {
  93.             final CharSequence ellipsis;
  94.             if (truncateEllipsis == null) {
  95.                 ellipsis = StringUtils.EMPTY;
  96.             } else {
  97.                 ellipsis = truncateEllipsis;
  98.             }
  99.             buf.replace(precision - ellipsis.length(), seq.length(), ellipsis.toString());
  100.         }
  101.         final boolean leftJustify = (flags & LEFT_JUSTIFY) == LEFT_JUSTIFY;
  102.         for (int i = buf.length(); i < width; i++) {
  103.             buf.insert(leftJustify ? i : 0, padChar);
  104.         }
  105.         formatter.format(buf.toString());
  106.         return formatter;
  107.     }

  108.     /**
  109.      * Handles the common {@link Formattable} operations of truncate-pad-append,
  110.      * padding width underflow with spaces.
  111.      *
  112.      * @param seq  the string to handle, not null
  113.      * @param formatter  the destination formatter, not null
  114.      * @param flags  the flags for formatting, see {@code Formattable}
  115.      * @param width  the width of the output, see {@code Formattable}
  116.      * @param precision  the precision of the output, see {@code Formattable}
  117.      * @param ellipsis  the ellipsis to use when precision dictates truncation, null or
  118.      *  empty causes a hard truncation
  119.      * @return The {@code formatter} instance, not null
  120.      * @throws IllegalArgumentException if {@code ellipsis.length() > precision},
  121.      *  given that {@code ellipsis} is not null and {@code precision >= 0}
  122.      */
  123.     public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
  124.             final int precision, final CharSequence ellipsis) {
  125.         return append(seq, formatter, flags, width, precision, ' ', ellipsis);
  126.     }

  127.     /**
  128.      * Gets the default formatted representation of the specified
  129.      * {@code Formattable}.
  130.      *
  131.      * @param formattable  the instance to convert to a string, not null
  132.      * @return The resulting string, not null
  133.      */
  134.     public static String toString(final Formattable formattable) {
  135.         return String.format(SIMPLEST_FORMAT, formattable);
  136.     }

  137.     /**
  138.      * {@code FormattableUtils} instances should NOT be constructed in
  139.      * standard programming. Instead, the methods of the class should be invoked
  140.      * statically.
  141.      *
  142.      * <p>This constructor is public to permit tools that require a JavaBean
  143.      * instance to operate.</p>
  144.      */
  145.     public FormattableUtils() {
  146.     }

  147. }