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        formatter.format(buf.toString());
111        return formatter;
112    }
113
114    /**
115     * Handles the common {@link Formattable} operations of truncate-pad-append, padding width underflow with spaces.
116     *
117     * @param seq       the string to handle, not null.
118     * @param formatter the destination formatter, not null.
119     * @param flags     the flags for formatting, see {@code Formattable}.
120     * @param width     the width of the output, see {@code Formattable}.
121     * @param precision the precision of the output, see {@code Formattable}.
122     * @param ellipsis  the ellipsis to use when precision dictates truncation, null or empty causes a hard truncation..
123     * @return The {@code formatter} instance, not null
124     * @throws IllegalArgumentException if {@code ellipsis.length() > precision}, given that {@code ellipsis} is not null and {@code precision >= 0}.
125     */
126    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width, final int precision,
127            final CharSequence ellipsis) {
128        return append(seq, formatter, flags, width, precision, ' ', ellipsis);
129    }
130
131    /**
132     * Gets the default formatted representation of the specified {@code Formattable}.
133     *
134     * @param formattable the instance to convert to a string, not null.
135     * @return The resulting string, not null.
136     */
137    public static String toString(final Formattable formattable) {
138        return String.format(SIMPLEST_FORMAT, formattable);
139    }
140
141    /**
142     * {@code FormattableUtils} instances should NOT be constructed in standard programming. Instead, the methods of the class should be invoked statically.
143     *
144     * <p>
145     * This constructor is public to permit tools that require a JavaBean instance to operate.
146     * </p>
147     */
148    public FormattableUtils() {
149    }
150
151}