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