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.lang3.text;
018
019import java.util.Formattable;
020import java.util.FormattableFlags;
021import java.util.Formatter;
022
023import org.apache.commons.lang3.ObjectUtils;
024import org.apache.commons.lang3.StringUtils;
025import org.apache.commons.lang3.Validate;
026
027/**
028 * Provides utilities for working with the {@link Formattable} interface.
029 *
030 * <p>
031 * The {@link Formattable} interface provides basic control over formatting
032 * when using a {@link Formatter}. It is primarily concerned with numeric precision
033 * and padding, and is not designed to allow generalized alternate formats.
034 * </p>
035 *
036 * @since 3.0
037 * @deprecated As of <a href="https://commons.apache.org/proper/commons-lang/changes-report.html#a3.6">3.6</a>, use Apache Commons Text
038 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/FormattableUtils.html">
039 * FormattableUtils</a>.
040 */
041@Deprecated
042public class FormattableUtils {
043
044    /**
045     * A format that simply outputs the value as a string.
046     */
047    private static final String SIMPLEST_FORMAT = "%s";
048
049    /**
050     * Handles the common {@link Formattable} operations of truncate-pad-append,
051     * with no ellipsis on precision overflow, and padding width underflow with
052     * spaces.
053     *
054     * @param seq  The string to handle, not null.
055     * @param formatter  The destination formatter, not null.
056     * @param flags  The flags for formatting, see {@link Formattable}.
057     * @param width  The width of the output, see {@link Formattable}.
058     * @param precision  The precision of the output, see {@link Formattable}.
059     * @return The {@code formatter} instance, not null.
060     */
061    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
062            final int precision) {
063        return append(seq, formatter, flags, width, precision, ' ', null);
064    }
065
066    /**
067     * Handles the common {@link Formattable} operations of truncate-pad-append,
068     * with no ellipsis on precision overflow.
069     *
070     * @param seq  The string to handle, not null.
071     * @param formatter  The destination formatter, not null.
072     * @param flags  The flags for formatting, see {@link Formattable}.
073     * @param width  The width of the output, see {@link Formattable}.
074     * @param precision  The precision of the output, see {@link Formattable}.
075     * @param padChar  The pad character to use.
076     * @return The {@code formatter} instance, not null.
077     */
078    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
079            final int precision, final char padChar) {
080        return append(seq, formatter, flags, width, precision, padChar, null);
081    }
082
083    /**
084     * Handles the common {@link Formattable} operations of truncate-pad-append.
085     *
086     * @param seq  The string to handle, not null.
087     * @param formatter  The destination formatter, not null.
088     * @param flags  The flags for formatting, see {@link Formattable}.
089     * @param width  The width of the output, see {@link Formattable}.
090     * @param precision  The precision of the output, see {@link Formattable}.
091     * @param padChar  The pad character to use.
092     * @param ellipsis  The ellipsis to use when precision dictates truncation, null or
093     *  empty causes a hard truncation.
094     * @return The {@code formatter} instance, not null.
095     */
096    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
097            final int precision, final char padChar, final CharSequence ellipsis) {
098        Validate.isTrue(ellipsis == null || precision < 0 || ellipsis.length() <= precision,
099                "Specified ellipsis '%1$s' exceeds precision of %2$s", ellipsis, Integer.valueOf(precision));
100        final StringBuilder buf = new StringBuilder(seq);
101        if (precision >= 0 && precision < seq.length()) {
102            final CharSequence actualEllipsis = ObjectUtils.getIfNull(ellipsis, StringUtils.EMPTY);
103            buf.replace(precision - actualEllipsis.length(), seq.length(), actualEllipsis.toString());
104        }
105        final boolean leftJustify = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY;
106        if (width > buf.length()) {
107            final String padding = StringUtils.repeat(padChar, width - buf.length());
108            buf.insert(leftJustify ? buf.length() : 0, padding);
109        }
110        return formatter.format(SIMPLEST_FORMAT, buf.toString());
111    }
112
113    /**
114     * Handles the common {@link Formattable} operations of truncate-pad-append,
115     * 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 {@link Formattable}.
120     * @param width  The width of the output, see {@link Formattable}.
121     * @param precision  The precision of the output, see {@link Formattable}.
122     * @param ellipsis  The ellipsis to use when precision dictates truncation, null or
123     *  empty causes a hard truncation.
124     * @return The {@code formatter} instance, not null.
125     */
126    public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
127            final int precision, final CharSequence ellipsis) {
128        return append(seq, formatter, flags, width, precision, ' ', ellipsis);
129    }
130
131    /**
132     * Gets the default formatted representation of the specified
133     * {@link Formattable}.
134     *
135     * @param formattable  The instance to convert to a string, not null.
136     * @return The resulting string, not null.
137     */
138    public static String toString(final Formattable formattable) {
139        return String.format(SIMPLEST_FORMAT, formattable);
140    }
141
142    /**
143     * {@link FormattableUtils} instances should NOT be constructed in
144     * standard programming. Instead, the methods of the class should be invoked
145     * statically.
146     *
147     * <p>
148     * This constructor is public to permit tools that require a JavaBean
149     * instance to operate.
150     * </p>
151     */
152    public FormattableUtils() {
153    }
154
155}