@Test public void testFilter() { assertThat(FormatOptions.getDefault().filter(ALL_FLAGS, true, true)).isDefault(); FormatOptions options = parse("+- #0,123.456", true); assertThat(options.filter(0, false, false)).isDefault(); assertThat(options.filter(ALL_FLAGS, true, true)).isSameInstanceAs(options); int flags = FLAG_LEFT_ALIGN | FLAG_SHOW_ALT_FORM | FLAG_SHOW_GROUPING | FLAG_SHOW_LEADING_ZEROS; FormatOptions filtered = options.filter(flags, true, false); assertThat(filtered).shouldLeftAlign(); assertThat(filtered).shouldShowAltForm(); assertThat(filtered).shouldShowGrouping(); assertThat(filtered).shouldShowLeadingZeros(); assertThat(filtered).shouldntPrefixSpaceForPositiveValues(); assertThat(filtered).shouldntPrefixPlusForPositiveValues(); assertThat(filtered).hasWidth(123); assertThat(filtered).hasPrecision(UNSET); assertThat(filtered).shouldntUpperCase(); int otherFlags = FLAG_PREFIX_PLUS_FOR_POSITIVE_VALUES | FLAG_PREFIX_SPACE_FOR_POSITIVE_VALUES | FLAG_UPPER_CASE; filtered = options.filter(otherFlags, false, true); assertThat(filtered).shouldntLeftAlign(); assertThat(filtered).shouldntShowAltForm(); assertThat(filtered).shouldntShowGrouping(); assertThat(filtered).shouldntShowLeadingZeros(); assertThat(filtered).shouldPrefixSpaceForPositiveValues(); assertThat(filtered).shouldPrefixPlusForPositiveValues(); assertThat(filtered).hasWidth(UNSET); assertThat(filtered).hasPrecision(456); assertThat(filtered).shouldUpperCase(); }
public FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision) { if (isDefault()) { return this; } int newFlags = allowedFlags & flags; int newWidth = allowWidth ? width : UNSET; int newPrecision = allowPrecision ? precision : UNSET; if (newFlags == 0 && newWidth == UNSET && newPrecision == UNSET) { return DEFAULT; } if (newFlags == flags && newWidth == width && newPrecision == precision) { return this; } return new FormatOptions(newFlags, newWidth, newPrecision); }
FormatOptions { public FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision) { if (isDefault()) { return this; } int newFlags = allowedFlags & flags; int newWidth = allowWidth ? width : UNSET; int newPrecision = allowPrecision ? precision : UNSET; if (newFlags == 0 && newWidth == UNSET && newPrecision == UNSET) { return DEFAULT; } if (newFlags == flags && newWidth == width && newPrecision == precision) { return this; } return new FormatOptions(newFlags, newWidth, newPrecision); } }
FormatOptions { public FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision) { if (isDefault()) { return this; } int newFlags = allowedFlags & flags; int newWidth = allowWidth ? width : UNSET; int newPrecision = allowPrecision ? precision : UNSET; if (newFlags == 0 && newWidth == UNSET && newPrecision == UNSET) { return DEFAULT; } if (newFlags == flags && newWidth == width && newPrecision == precision) { return this; } return new FormatOptions(newFlags, newWidth, newPrecision); } private  FormatOptions(int flags, int width, int precision); }
FormatOptions { public FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision) { if (isDefault()) { return this; } int newFlags = allowedFlags & flags; int newWidth = allowWidth ? width : UNSET; int newPrecision = allowPrecision ? precision : UNSET; if (newFlags == 0 && newWidth == UNSET && newPrecision == UNSET) { return DEFAULT; } if (newFlags == flags && newWidth == width && newPrecision == precision) { return this; } return new FormatOptions(newFlags, newWidth, newPrecision); } private  FormatOptions(int flags, int width, int precision); static FormatOptions getDefault(); static FormatOptions of(int flags, int width, int precision); static FormatOptions parse(String message, int pos, int end, boolean isUpperCase); FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision); @SuppressWarnings("ReferenceEquality") boolean isDefault(); int getWidth(); int getPrecision(); boolean validate(int allowedFlags, boolean allowPrecision); boolean areValidFor(FormatChar formatChar); int getFlags(); boolean shouldLeftAlign(); boolean shouldShowAltForm(); boolean shouldShowLeadingZeros(); boolean shouldPrefixPlusForPositiveValues(); boolean shouldPrefixSpaceForPositiveValues(); boolean shouldShowGrouping(); boolean shouldUpperCase(); StringBuilder appendPrintfOptions(StringBuilder out); @Override boolean equals(@NullableDecl Object o); @Override int hashCode(); }
FormatOptions { public FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision) { if (isDefault()) { return this; } int newFlags = allowedFlags & flags; int newWidth = allowWidth ? width : UNSET; int newPrecision = allowPrecision ? precision : UNSET; if (newFlags == 0 && newWidth == UNSET && newPrecision == UNSET) { return DEFAULT; } if (newFlags == flags && newWidth == width && newPrecision == precision) { return this; } return new FormatOptions(newFlags, newWidth, newPrecision); } private  FormatOptions(int flags, int width, int precision); static FormatOptions getDefault(); static FormatOptions of(int flags, int width, int precision); static FormatOptions parse(String message, int pos, int end, boolean isUpperCase); FormatOptions filter(int allowedFlags, boolean allowWidth, boolean allowPrecision); @SuppressWarnings("ReferenceEquality") boolean isDefault(); int getWidth(); int getPrecision(); boolean validate(int allowedFlags, boolean allowPrecision); boolean areValidFor(FormatChar formatChar); int getFlags(); boolean shouldLeftAlign(); boolean shouldShowAltForm(); boolean shouldShowLeadingZeros(); boolean shouldPrefixPlusForPositiveValues(); boolean shouldPrefixSpaceForPositiveValues(); boolean shouldShowGrouping(); boolean shouldUpperCase(); StringBuilder appendPrintfOptions(StringBuilder out); @Override boolean equals(@NullableDecl Object o); @Override int hashCode(); static final int FLAG_PREFIX_SPACE_FOR_POSITIVE_VALUES; static final int FLAG_SHOW_ALT_FORM; static final int FLAG_USE_PARENS_FOR_NEGATIVE_VALUES; static final int FLAG_PREFIX_PLUS_FOR_POSITIVE_VALUES; static final int FLAG_SHOW_GROUPING; static final int FLAG_LEFT_ALIGN; static final int FLAG_SHOW_LEADING_ZEROS; static final int FLAG_UPPER_CASE; static final int ALL_FLAGS; static final int UNSET; }