001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.text;
018
019import static java.util.FormattableFlags.LEFT_JUSTIFY;
020
021import java.util.Formattable;
022import java.util.Formatter;
023
024import org.apache.commons.lang3.StringUtils;
025
026/**
027 * Provides utilities for working with the {@code Formattable} interface.
028 *
029 * <p>
030 * The {@link Formattable} interface provides basic control over formatting when using a {@code Formatter}. It is primarily concerned with numeric precision and
031 * padding, and is not designed to allow generalized alternate formats.
032 * </p>
033 *
034 * @since 1.0
035 */
036public class FormattableUtils {
037
038    /**
039     * A format that simply outputs the value as a string.
040     */
041    private static final String SIMPLEST_FORMAT = "%s";
042
043    /**
044     * Handles the common {@code Formattable} operations of truncate-pad-append, with no ellipsis on precision overflow, and padding width underflow with
045     * spaces.
046     *
047     * @param seq       The string to handle, not null.
048     * @param formatter The destination formatter, not null.
049     * @param flags     The flags for formatting, see {@code Formattable}.
050     * @param width     The width of the output, see {@code Formattable}.
051     * @param precision The precision of the output, see {@code Formattable}.
052     * @return The {@code formatter} instance, not null.
053     */
054    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
055            final int precision) {
056        return append(seq, formatter, flags, width, precision, ' ', null);
057    }
058
059    /**
060     * Handles the common {@link Formattable} operations of truncate-pad-append, with no ellipsis on precision overflow.
061     *
062     * @param seq       The string to handle, not null.
063     * @param formatter The destination formatter, not null.
064     * @param flags     The flags for formatting, see {@code Formattable}.
065     * @param width     The width of the output, see {@code Formattable}.
066     * @param precision The precision of the output, see {@code Formattable}.
067     * @param padChar   The pad character to use.
068     * @return The {@code formatter} instance, not null.
069     */
070    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
071            final int precision, final char padChar) {
072        return append(seq, formatter, flags, width, precision, padChar, null);
073    }
074
075    /**
076     * Handles the common {@link Formattable} operations of truncate-pad-append.
077     *
078     * @param seq              The string to handle, not null.
079     * @param formatter        The destination formatter, not null.
080     * @param flags            The flags for formatting, see {@code Formattable}.
081     * @param width            The width of the output, see {@code Formattable}.
082     * @param precision        The precision of the output, see {@code Formattable}.
083     * @param padChar          The pad character to use.
084     * @param truncateEllipsis The ellipsis to use when precision dictates truncation, null or empty causes a hard truncation.
085     * @return The {@code formatter} instance, not null.
086     * @throws IllegalArgumentException if {@code ellipsis.length() > precision}, given that {@code ellipsis} is not null and {@code precision >= 0}.
087     */
088    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
089            final int precision, final char padChar, final CharSequence truncateEllipsis) {
090        if (!(truncateEllipsis == null || precision < 0 || truncateEllipsis.length() <= precision)) {
091            throw new IllegalArgumentException(
092                    String.format("Specified ellipsis '%s' exceeds precision of %s",
093                            truncateEllipsis,
094                            precision));
095        }
096        final StringBuilder buf = new StringBuilder(seq);
097        if (precision >= 0 && precision < seq.length()) {
098            final CharSequence ellipsis;
099            if (truncateEllipsis == null) {
100                ellipsis = StringUtils.EMPTY;
101            } else {
102                ellipsis = truncateEllipsis;
103            }
104            buf.replace(precision - ellipsis.length(), seq.length(), ellipsis.toString());
105        }
106        final boolean leftJustify = (flags & LEFT_JUSTIFY) == LEFT_JUSTIFY;
107        for (int i = buf.length(); i < width; i++) {
108            buf.insert(leftJustify ? i : 0, padChar);
109        }
110        return formatter.format(SIMPLEST_FORMAT, buf.toString());
111    }
112
113    /**
114     * Handles the common {@link Formattable} operations of truncate-pad-append, padding width underflow with spaces.
115     *
116     * @param seq       The string to handle, not null.
117     * @param formatter The destination formatter, not null.
118     * @param flags     The flags for formatting, see {@code Formattable}.
119     * @param width     The width of the output, see {@code Formattable}.
120     * @param precision The precision of the output, see {@code Formattable}.
121     * @param ellipsis  The ellipsis to use when precision dictates truncation, null or empty causes a hard truncation..
122     * @return The {@code formatter} instance, not null
123     * @throws IllegalArgumentException if {@code ellipsis.length() > precision}, given that {@code ellipsis} is not null and {@code precision >= 0}.
124     */
125    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width, final int precision,
126            final CharSequence ellipsis) {
127        return append(seq, formatter, flags, width, precision, ' ', ellipsis);
128    }
129
130    /**
131     * Gets the default formatted representation of the specified {@code Formattable}.
132     *
133     * @param formattable The instance to convert to a string, not null.
134     * @return The resulting string, not null.
135     */
136    public static String toString(final Formattable formattable) {
137        return String.format(SIMPLEST_FORMAT, formattable);
138    }
139
140    /**
141     * {@code FormattableUtils} instances should NOT be constructed in standard programming. Instead, the methods of the class should be invoked statically.
142     *
143     * <p>
144     * This constructor is public to permit tools that require a JavaBean instance to operate.
145     * </p>
146     */
147    public FormattableUtils() {
148    }
149
150}