Source code
Description: source: package: org.apache.commons.lang3.text, class: FormattableUtils

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