diff options
author | Gary D. Gregory <ggregory@apache.org> | 2011-04-22 21:53:14 +0000 |
---|---|---|
committer | Gary D. Gregory <ggregory@apache.org> | 2011-04-22 21:53:14 +0000 |
commit | b168f29106d112da0bbdb3a070be30f9ee3cb36e (patch) | |
tree | 7f0e4b1a15f123b54bdd570096137bc06e0e9af0 /src/main/java/org/apache/commons/lang3/util | |
parent | 95008b9efd82caafd01c675c3050b09e22baf997 (diff) | |
download | apache-commons-lang-b168f29106d112da0bbdb3a070be30f9ee3cb36e.tar.gz |
Move new FormattableUtils class to .text from .util.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1096058 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/util')
-rw-r--r-- | src/main/java/org/apache/commons/lang3/util/FormattableUtils.java | 142 |
1 files changed, 0 insertions, 142 deletions
diff --git a/src/main/java/org/apache/commons/lang3/util/FormattableUtils.java b/src/main/java/org/apache/commons/lang3/util/FormattableUtils.java deleted file mode 100644 index 9b3c10c70..000000000 --- a/src/main/java/org/apache/commons/lang3/util/FormattableUtils.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.lang3.util; - -import static java.util.FormattableFlags.LEFT_JUSTIFY; - -import java.util.Formattable; -import java.util.Formatter; - -import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.Validate; - -/** - * Provides utilities for working with {@link Formattable}s. - * - * @since Lang 3.0 - * @version $Id$ - */ -public class FormattableUtils { - - private static final String SIMPLEST_FORMAT = "%1$s"; - - /** - * <p>{@link FormattableUtils} instances should NOT be constructed in - * standard programming. Instead, the methods of the class should be invoked - * statically.</p> - * - * <p>This constructor is public to permit tools that require a JavaBean - * instance to operate.</p> - */ - public FormattableUtils() { - super(); - } - - /** - * Get the default formatted representation of the specified - * {@link Formattable}. - * - * @param formattable - * @return String - */ - public static String toString(Formattable formattable) { - return String.format(SIMPLEST_FORMAT, formattable); - } - - /** - * Handles the common {@link Formattable} operations of truncate-pad-append, - * with no ellipsis on precision overflow, and padding width underflow with - * spaces. - * - * @param seq to handle - * @param formatter destination - * @param flags for formatting - * @param width of output - * @param precision of output - * @return {@code formatter} - */ - public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, - int precision) { - return append(seq, formatter, flags, width, precision, ' ', null); - } - - /** - * Handles the common {@link Formattable} operations of truncate-pad-append, - * with no ellipsis on precision overflow. - * - * @param seq to handle - * @param formatter destination - * @param flags for formatting - * @param width of output - * @param precision of output - * @param padChar to use - * @return {@code formatter} - */ - public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, - int precision, char padChar) { - return append(seq, formatter, flags, width, precision, padChar, null); - } - - /** - * Handles the common {@link Formattable} operations of truncate-pad-append, - * padding width underflow with spaces. - * - * @param seq to handle - * @param formatter destination - * @param flags for formatting - * @param width of output - * @param precision of output - * @param ellipsis to use when precision dictates truncation; a {@code null} - * or empty value causes a hard truncation - * @return {@code formatter} - */ - public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, - int precision, CharSequence ellipsis) { - return append(seq, formatter, flags, width, precision, ' ', ellipsis); - } - - /** - * Handles the common {@link Formattable} operations of truncate-pad-append. - * - * @param seq to handle - * @param formatter destination - * @param flags for formatting - * @param width of output - * @param precision of output - * @param padChar to use - * @param ellipsis to use when precision dictates truncation; a {@code null} - * or empty value causes a hard truncation - * @return {@code formatter} - */ - public static Formatter append(CharSequence seq, Formatter formatter, int flags, int width, - int precision, char padChar, CharSequence ellipsis) { - Validate.isTrue(ellipsis == null || precision < 0 || ellipsis.length() <= precision, - "Specified ellipsis '%1$s' exceeds precision of %2$s", ellipsis, precision); - StringBuilder buf = new StringBuilder(seq); - if (precision >= 0 && precision < seq.length()) { - CharSequence _ellipsis = ObjectUtils.defaultIfNull(ellipsis, StringUtils.EMPTY); - buf.replace(precision - _ellipsis.length(), seq.length(), _ellipsis.toString()); - } - boolean leftJustify = (flags & LEFT_JUSTIFY) == LEFT_JUSTIFY; - for (int i = buf.length(); i < width; i++) { - buf.insert(leftJustify ? i : 0, padChar); - } - formatter.format(buf.toString()); - return formatter; - } -} |