Java Code Examples for java.util.FormattableFlags#ALTERNATE

The following examples show how to use java.util.FormattableFlags#ALTERNATE . 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 vote down vote up
/**
 * 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 vote down vote up
@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 vote down vote up
@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 vote down vote up
@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 vote down vote up
@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: TextChannel.java    From JDA with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: ByteSizeStrings.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link Formattable} object that can be used with {@link String#format(String, Object...)}.
 * <p>
 * When used as the argument for a {@literal %s} format string element, the {@literal bytes} value
 * will be formatted using the current {@link ByteSizeStrings} values, or if the alternative
 * flag is set (using the {@literal %#s} format string) it will use the {@link ByteSizeStrings#metric()}
 * formatter. Finally, the precision of the formatted value can be adjusted using format string
 * argumenbts like {@literal %.6s}.
 *
 * @see http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
 */
public Formattable formatted(final long bytes) {
    return new Formattable() {
        @Override
        public void formatTo(Formatter formatter, int flags, int width, int precision) {
            boolean alternate = (flags & FormattableFlags.ALTERNATE) == FormattableFlags.ALTERNATE;
            ByteSizeStrings strings = alternate ? ByteSizeStrings.metric() : ByteSizeStrings.this;
            if (precision != -1) {
                formatter.format("%s", strings.makeSizeString(bytes, precision));
            } else {
                formatter.format("%s", strings.makeSizeString(bytes));
            }
        }
    };
}
 
Example 8
Source File: AbstractIdentifiedObject.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Formats the name or identifier of this object using the provider formatter.
 * This method is invoked when an {@code IdentifiedObject} object is formatted
 * using the {@code "%s"} conversion specifier of {@link java.util.Formatter}.
 * Users don't need to invoke this method explicitly.
 *
 * <p>If the alternate flags is present (as in {@code "%#s"}), then this method
 * will format the identifier (if present) instead than the object name.</p>
 *
 * @param  formatter  the formatter in which to format this identified object.
 * @param  flags      whether to apply left alignment, use upper-case letters and/or use alternate form.
 * @param  width      minimal number of characters to write, padding with {@code ' '} if necessary.
 * @param  precision  maximal number of characters to write, or -1 if no limit.
 *
 * @see IdentifiedObjects#getName(IdentifiedObject, Citation)
 * @see IdentifiedObjects#getIdentifierOrName(IdentifiedObject)
 *
 * @since 1.1
 */
@Override
public void formatTo(final java.util.Formatter formatter, final int flags, final int width, final int precision) {
    final String value;
    if ((flags & FormattableFlags.ALTERNATE) != 0) {
        value = IdentifiedObjects.getIdentifierOrName(this);
    } else {
        value = IdentifiedObjects.getName(this, null);
    }
    Strings.formatTo(formatter, flags, width, precision, value);
}