Java Code Examples for java.util.FormattableFlags#LEFT_JUSTIFY
The following examples show how to use
java.util.FormattableFlags#LEFT_JUSTIFY .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleMessageFormatter.java From flogger with Apache License 2.0 | 6 votes |
/** * Returns a string representation of the user supplied formattable, accounting for any possible * runtime exceptions. * * @param value the value to be formatted. * @return a best-effort string representation of the given value, even if exceptions were thrown. */ private static void safeFormatTo(Formattable value, StringBuilder out, FormatOptions options) { // Only care about 3 specific flags for Formattable. int formatFlags = options.getFlags() & (FLAG_LEFT_ALIGN | FLAG_UPPER_CASE | FLAG_SHOW_ALT_FORM); if (formatFlags != 0) { // TODO: Maybe re-order the options flags to make this step easier or use a lookup table. // Note that reordering flags would require a rethink of how they are parsed. formatFlags = ((formatFlags & FLAG_LEFT_ALIGN) != 0 ? FormattableFlags.LEFT_JUSTIFY : 0) | ((formatFlags & FLAG_UPPER_CASE) != 0 ? FormattableFlags.UPPERCASE : 0) | ((formatFlags & FLAG_SHOW_ALT_FORM) != 0 ? FormattableFlags.ALTERNATE : 0); } // We may need to undo an arbitrary amount of appending if there is an error. int originalLength = out.length(); Formatter formatter = new Formatter(out, FORMAT_LOCALE); try { value.formatTo(formatter, formatFlags, options.getWidth(), options.getPrecision()); } catch (RuntimeException e) { out.setLength(originalLength); // We only use a StringBuilder to create the Formatter instance. try { formatter.out().append(getErrorString(value, e)); } catch (IOException impossible) { } } }
Example 2
Source File: FakeUser.java From SkyBot with GNU Affero General Public License v3.0 | 6 votes |
@Override public void formatTo(Formatter formatter, int flags, int width, int precision) { final boolean alt = (flags & FormattableFlags.ALTERNATE) == FormattableFlags.ALTERNATE; final boolean upper = (flags & FormattableFlags.UPPERCASE) == FormattableFlags.UPPERCASE; final boolean leftJustified = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY; String out; if (!alt) { out = getAsMention(); } else if (upper) { out = getAsTag().toUpperCase(); } else { out = getAsTag(); } MiscUtil.appendTo(formatter, width, precision, leftJustified, out); }
Example 3
Source File: UserImpl.java From JDA with Apache License 2.0 | 6 votes |
@Override public void formatTo(Formatter formatter, int flags, int width, int precision) { boolean alt = (flags & FormattableFlags.ALTERNATE) == FormattableFlags.ALTERNATE; boolean upper = (flags & FormattableFlags.UPPERCASE) == FormattableFlags.UPPERCASE; boolean leftJustified = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY; String out; if (!alt) out = getAsMention(); else if (upper) out = getAsTag().toUpperCase(); else out = getAsTag(); MiscUtil.appendTo(formatter, width, precision, leftJustified, out); }
Example 4
Source File: Nameable.java From Javacord with Apache License 2.0 | 5 votes |
@Override default void formatTo(Formatter formatter, int flags, int width, int precision) { boolean alternate = (flags & FormattableFlags.ALTERNATE) != 0; String representation = alternate ? this.toString() : this.getName(); boolean uppercase = (flags & FormattableFlags.UPPERCASE) != 0; boolean leftAlign = (flags & FormattableFlags.LEFT_JUSTIFY) != 0; boolean doPad = representation.length() < width; String padString = null; if (doPad) { char[] spaces = new char[width - representation.length()]; Arrays.fill(spaces, ' '); padString = new String(spaces); } try { if (doPad && !leftAlign) { formatter.out().append(padString); } formatter.out().append(uppercase ? representation.toUpperCase() : representation); if (doPad && leftAlign) { formatter.out().append(padString); } } catch (IOException e) { ExceptionLogger.getConsumer().accept(e); } }
Example 5
Source File: Nameable.java From Javacord with Apache License 2.0 | 5 votes |
@Override default void formatTo(Formatter formatter, int flags, int width, int precision) { boolean alternate = (flags & FormattableFlags.ALTERNATE) != 0; String representation = alternate ? this.toString() : this.getName(); boolean uppercase = (flags & FormattableFlags.UPPERCASE) != 0; boolean leftAlign = (flags & FormattableFlags.LEFT_JUSTIFY) != 0; boolean doPad = representation.length() < width; String padString = null; if (doPad) { char[] spaces = new char[width - representation.length()]; Arrays.fill(spaces, ' '); padString = new String(spaces); } try { if (doPad && !leftAlign) { formatter.out().append(padString); } formatter.out().append(uppercase ? representation.toUpperCase() : representation); if (doPad && leftAlign) { formatter.out().append(padString); } } catch (IOException e) { ExceptionLogger.getConsumer().accept(e); } }
Example 6
Source File: IMentionable.java From JDA with Apache License 2.0 | 5 votes |
@Override default void formatTo(Formatter formatter, int flags, int width, int precision) { boolean leftJustified = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY; boolean upper = (flags & FormattableFlags.UPPERCASE) == FormattableFlags.UPPERCASE; String out = upper ? getAsMention().toUpperCase(formatter.locale()) : getAsMention(); MiscUtil.appendTo(formatter, width, precision, leftJustified, out); }
Example 7
Source File: TextChannel.java From JDA with Apache License 2.0 | 5 votes |
@Override default void formatTo(Formatter formatter, int flags, int width, int precision) { boolean leftJustified = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY; boolean upper = (flags & FormattableFlags.UPPERCASE) == FormattableFlags.UPPERCASE; boolean alt = (flags & FormattableFlags.ALTERNATE) == FormattableFlags.ALTERNATE; String out; if (alt) out = "#" + (upper ? getName().toUpperCase(formatter.locale()) : getName()); else out = getAsMention(); MiscUtil.appendTo(formatter, width, precision, leftJustified, out); }
Example 8
Source File: AbstractMessage.java From JDA with Apache License 2.0 | 5 votes |
@Override public void formatTo(Formatter formatter, int flags, int width, int precision) { boolean upper = (flags & FormattableFlags.UPPERCASE) == FormattableFlags.UPPERCASE; boolean leftJustified = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY; String out = content; if (upper) out = out.toUpperCase(formatter.locale()); appendFormat(formatter, width, precision, leftJustified, out); }
Example 9
Source File: Strings.java From sis with Apache License 2.0 | 4 votes |
/** * Formats the given character sequence to the given formatter. This method takes in account * the {@link FormattableFlags#UPPERCASE} and {@link FormattableFlags#LEFT_JUSTIFY} flags. * * @param formatter the formatter in which to format the value. * @param flags the formatting flags. * @param width minimal number of characters to write, padding with {@code ' '} if necessary. * @param precision number of characters to keep before truncation, or -1 if no limit. * @param value the text to format. */ public static void formatTo(final Formatter formatter, final int flags, int width, int precision, String value) { /* * Converting to upper cases may change the string length in some locales. * So we need to perform this conversion before to check the length. */ boolean isUpperCase = (flags & FormattableFlags.UPPERCASE) != 0; if (isUpperCase && (width > 0 || precision >= 0)) { value = value.toUpperCase(formatter.locale()); isUpperCase = false; // Because conversion has already been done. } /* * If the string is longer than the specified "precision", truncate * and add "…" for letting user know that there is missing characters. * This loop counts the number of Unicode code points rather than characters. */ int length = value.length(); if (precision >= 0) { for (int i=0,n=0; i<length; i += n) { if (--precision < 0) { /* * Found the amount of characters to keep. The 'n' variable can be * zero only if precision == 0, in which case the string is empty. */ if (n == 0) { value = ""; } else { length = (i -= n) + 1; final StringBuilder buffer = new StringBuilder(length); value = buffer.append(value, 0, i).append('…').toString(); } break; } n = Character.charCount(value.codePointAt(i)); } } /* * If the string is shorter than the minimal width, add spaces on the left or right side. * We double check with `width > length` since it is faster than codePointCount(…). */ final String format; final Object[] args; if (width > length && (width -= value.codePointCount(0, length)) > 0) { format = "%s%s"; args = new Object[] {value, value}; args[(flags & FormattableFlags.LEFT_JUSTIFY) != 0 ? 1 : 0] = CharSequences.spaces(width); } else { format = isUpperCase ? "%S" : "%s"; args = new Object[] {value}; } formatter.format(format, args); }