當前位置: 首頁>>代碼示例>>Java>>正文


Java Formattable類代碼示例

本文整理匯總了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);
            }
        }
    };
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:HotSpotGraalCompiler.java

示例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;
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:26,代碼來源:DecorsManager.java

示例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)
        )
    );
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:19,代碼來源:TextDecorTest.java

示例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."
                )
            )
        )
    );
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:24,代碼來源:ExceptionDecorTest.java

示例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());
    }
}
 
開發者ID:unktomi,項目名稱:form-follows-function,代碼行數:15,代碼來源:F3Formatter.java

示例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);
    }
  };
}
 
開發者ID:bazelbuild,項目名稱:bazel,代碼行數:22,代碼來源:Printer.java

示例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);
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:13,代碼來源:AbstractDecorTest.java

示例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/>"))
    );
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:18,代碼來源:DomDecorTest.java

示例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");
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:13,代碼來源:DomDecorTest.java

示例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");
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:13,代碼來源:ExceptionDecorTest.java

示例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());
        }
    }
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:51,代碼來源:FormatterTest.java

示例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);
}
 
開發者ID:bazelbuild,項目名稱:bazel,代碼行數:7,代碼來源:Mutability.java

示例13: testSimplestFormat

import java.util.Formattable; //導入依賴的package包/類
@Test
public void testSimplestFormat() {
    final Formattable formattable = new SimplestFormattable("foo");

    assertThat(FormattableUtils.toString(formattable)).isEqualTo("foo");
}
 
開發者ID:apache,項目名稱:commons-text,代碼行數:7,代碼來源:FormattableUtilsTest.java

示例14: decor

import java.util.Formattable; //導入依賴的package包/類
@Override
public Formattable decor() {
    return new NanoDecor((Long) this.object());
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:5,代碼來源:NanoDecorTest.java

示例15: decor

import java.util.Formattable; //導入依賴的package包/類
@Override
public Formattable decor() {
    return new TextDecor(this.object());
}
 
開發者ID:jcabi,項目名稱:jcabi-log,代碼行數:5,代碼來源:TextDecorTest.java


注:本文中的java.util.Formattable類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。