本文整理匯總了Java中java.util.Formattable類的典型用法代碼示例。如果您正苦於以下問題:Java Formattable類的具體用法?Java Formattable怎麽用?Java Formattable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Formattable類屬於java.util包,在下文中一共展示了Formattable類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fmt
import java.util.Formattable; //導入依賴的package包/類
/**
* Wraps {@code obj} in a {@link Formatter} that standardizes formatting for certain objects.
*/
static Formattable fmt(Object obj) {
return new Formattable() {
@Override
public void formatTo(Formatter buf, int flags, int width, int precision) {
if (obj instanceof Throwable) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
((Throwable) obj).printStackTrace(new PrintStream(baos));
buf.format("%s", baos.toString());
} else if (obj instanceof StackTraceElement[]) {
for (StackTraceElement e : (StackTraceElement[]) obj) {
buf.format("\t%s%n", e);
}
} else if (obj instanceof JavaMethod) {
buf.format("%s", str((JavaMethod) obj));
} else {
buf.format("%s", obj);
}
}
};
}
示例2: find
import java.util.Formattable; //導入依賴的package包/類
/**
* Find decor.
* @param key Key for the formatter to be used to fmt the arguments
* @return The type of decor found
* @throws DecorException If some problem
*/
@SuppressWarnings("unchecked")
private static Class<? extends Formattable> find(final String key)
throws DecorException {
final Class<? extends Formattable> type;
if (DecorsManager.DECORS.containsKey(key)) {
type = DecorsManager.DECORS.get(key);
} else {
try {
type = (Class<Formattable>) Class.forName(key);
} catch (final ClassNotFoundException ex) {
throw new DecorException(
ex,
"Decor '%s' not found and class can't be instantiated",
key
);
}
}
return type;
}
示例3: compressesLongText
import java.util.Formattable; //導入依賴的package包/類
/**
* Test for a long text.
*/
@Test
public void compressesLongText() {
final int len = 1000;
final String text = StringUtils.repeat('x', len);
final Formattable fmt = new TextDecor(text);
final StringBuilder output = new StringBuilder(100);
fmt.formatTo(new Formatter(output), 0, 0, 0);
MatcherAssert.assertThat(
output.length(),
Matchers.describedAs(
output.toString(),
Matchers.equalTo(TextDecor.MAX)
)
);
}
示例4: convertsExceptionToText
import java.util.Formattable; //導入依賴的package包/類
/**
* ExceptionDecor can transform exception to text.
* @throws Exception If some problem
*/
@Test
public void convertsExceptionToText() throws Exception {
final Formattable decor = new ExceptionDecor(new IOException("ouch!"));
final Appendable dest = Mockito.mock(Appendable.class);
final Formatter fmt = new Formatter(dest);
decor.formatTo(fmt, 0, 0, 0);
Mockito.verify(dest).append(
MockitoHamcrest.argThat(
Matchers.allOf(
Matchers.containsString(
"java.io.IOException: ouch!"
),
Matchers.containsString(
"at com.jcabi.log.ExceptionDecorTest."
)
)
)
);
}
示例5: printString
import java.util.Formattable; //導入依賴的package包/類
private void printString(Object arg, Locale l) throws IOException {
if (arg == null) {
print("null");
} else if (arg instanceof Formattable) {
Formatter fmt = new Formatter(formatter.out(), l);
/*
if (formatter.locale() != l)
fmt = new F3Formatter(formatter.out(), l);
*/
((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision);
} else {
print(arg.toString());
}
}
示例6: formattable
import java.util.Formattable; //導入依賴的package包/類
/**
* Perform Python-style string formatting, lazily.
*
* @param pattern a format string.
* @param arguments positional arguments.
* @return the formatted string.
*/
public static Formattable formattable(final String pattern, Object... arguments) {
final List<Object> args = Arrays.asList(arguments);
return new Formattable() {
@Override
public String toString() {
return formatWithList(pattern, args);
}
@Override
public void formatTo(Formatter formatter, int flags, int width, int precision) {
Printer.getPrinter(formatter.out()).formatWithList(pattern, args);
}
};
}
示例7: convertsDifferentFormats
import java.util.Formattable; //導入依賴的package包/類
/**
* AbstractDecor can convert object to text.
* @throws Exception If some problem inside
*/
@Test
public final void convertsDifferentFormats() throws Exception {
final Formattable decor = this.decor();
final Appendable dest = Mockito.mock(Appendable.class);
final Formatter fmt = new Formatter(dest);
decor.formatTo(fmt, this.flags, this.width, this.precision);
Mockito.verify(dest).append(this.text);
}
示例8: convertsDocumentToText
import java.util.Formattable; //導入依賴的package包/類
/**
* DocumentDecor can transform Document to text.
* @throws Exception If some problem
*/
@Test
public void convertsDocumentToText() throws Exception {
final Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
doc.appendChild(doc.createElement("root"));
final Formattable decor = new DomDecor(doc);
final Appendable dest = Mockito.mock(Appendable.class);
final Formatter fmt = new Formatter(dest);
decor.formatTo(fmt, 0, 0, 0);
Mockito.verify(dest).append(
MockitoHamcrest.argThat(Matchers.containsString("<root/>"))
);
}
示例9: convertsNullToText
import java.util.Formattable; //導入依賴的package包/類
/**
* DocumentDecor can handle NULL properly.
* @throws Exception If some problem
*/
@Test
public void convertsNullToText() throws Exception {
final Formattable decor = new DomDecor(null);
final Appendable dest = Mockito.mock(Appendable.class);
final Formatter fmt = new Formatter(dest);
decor.formatTo(fmt, 0, 0, 0);
Mockito.verify(dest).append("NULL");
}
示例10: convertsNullToText
import java.util.Formattable; //導入依賴的package包/類
/**
* ExceptionDecor can handle NULL properly.
* @throws Exception If some problem
*/
@Test
public void convertsNullToText() throws Exception {
final Formattable decor = new ExceptionDecor(null);
final Appendable dest = Mockito.mock(Appendable.class);
final Formatter fmt = new Formatter(dest);
decor.formatTo(fmt, 0, 0, 0);
Mockito.verify(dest).append("NULL");
}
示例11: Formatter
import java.util.Formattable; //導入依賴的package包/類
/**
* @tests java.util.Formatter#format(String, Object...) for general
* conversion other cases
*/
public void test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther() {
/*
* In Turkish locale, the upper case of '\u0069' is '\u0130'. The
* following test indicate that '\u0069' is coverted to upper case
* without using the turkish locale.
*/
Formatter f = new Formatter(new Locale("tr"));
f.format("%S", "\u0069");
assertEquals("\u0049", f.toString());
final Object[] input = {
Boolean.FALSE,
Boolean.TRUE,
new Character('c'),
new Byte((byte) 0x01),
new Short((short) 0x0001),
new Integer(1),
new Float(1.1f),
new Double(1.1d),
"",
"string content",
new MockFormattable(),
(Object) null,
};
f = new Formatter(Locale.GERMAN);
for (int i = 0; i < input.length; i++) {
if (!(input[i] instanceof Formattable)) {
try {
f.format("%#s", input[i]);
/*
* fail on RI, spec says if the '#' flag is present and the
* argument is not a Formattable , then a
* FormatFlagsConversionMismatchException will be thrown.
*/
fail("should throw FormatFlagsConversionMismatchException");
} catch (FormatFlagsConversionMismatchException e) {
// expected
}
} else {
f.format("%#s%<-#8s", input[i]);
assertEquals(
"customized format function width: -1 precision: -1customized format function width: 8 precision: -1",
f.toString());
}
}
}
示例12: Mutability
import java.util.Formattable; //導入依賴的package包/類
private Mutability(Formattable annotation) {
this.isFrozen = false;
// Seems unlikely that we'll often lock more than 10 things at once.
this.lockedItems = new IdentityHashMap<>(10);
this.annotation = Preconditions.checkNotNull(annotation);
}
示例13: testSimplestFormat
import java.util.Formattable; //導入依賴的package包/類
@Test
public void testSimplestFormat() {
final Formattable formattable = new SimplestFormattable("foo");
assertThat(FormattableUtils.toString(formattable)).isEqualTo("foo");
}
示例14: decor
import java.util.Formattable; //導入依賴的package包/類
@Override
public Formattable decor() {
return new NanoDecor((Long) this.object());
}
示例15: decor
import java.util.Formattable; //導入依賴的package包/類
@Override
public Formattable decor() {
return new TextDecor(this.object());
}