Java 类java.util.Formattable 实例源码

项目:openjdk-jdk10    文件:HotSpotGraalCompiler.java   
/**
 * 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);
            }
        }
    };
}
项目:graal-core    文件:HotSpotGraalCompiler.java   
/**
 * 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);
            }
        }
    };
}
项目:jcabi-log    文件:DecorsManager.java   
/**
 * 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;
}
项目:jcabi-log    文件:TextDecorTest.java   
/**
 * 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)
        )
    );
}
项目:jcabi-log    文件:ExceptionDecorTest.java   
/**
 * 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."
                )
            )
        )
    );
}
项目:form-follows-function    文件:F3Formatter.java   
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());
    }
}
项目:bazel    文件:Printer.java   
/**
 * 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);
    }
  };
}
项目:jcabi-log    文件:AbstractDecorTest.java   
/**
 * 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);
}
项目:jcabi-log    文件:DomDecorTest.java   
/**
 * 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/>"))
    );
}
项目:jcabi-log    文件:DomDecorTest.java   
/**
 * 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");
}
项目:jcabi-log    文件:ExceptionDecorTest.java   
/**
 * 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");
}
项目:cn1    文件:FormatterTest.java   
/**
 * @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());
        }
    }
}
项目:bazel    文件:Mutability.java   
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);
}
项目:commons-text    文件:FormattableUtilsTest.java   
@Test
public void testSimplestFormat() {
    final Formattable formattable = new SimplestFormattable("foo");

    assertThat(FormattableUtils.toString(formattable)).isEqualTo("foo");
}
项目:jcabi-log    文件:NanoDecorTest.java   
@Override
public Formattable decor() {
    return new NanoDecor((Long) this.object());
}
项目:jcabi-log    文件:TextDecorTest.java   
@Override
public Formattable decor() {
    return new TextDecor(this.object());
}
项目:jcabi-log    文件:TypeDecorTest.java   
@Override
public Formattable decor() {
    return new TypeDecor(this.object());
}
项目:jcabi-log    文件:SizeDecorTest.java   
@Override
public Formattable decor() {
    return new SizeDecor((Long) this.object());
}
项目:jcabi-log    文件:SecretDecorTest.java   
@Override
public Formattable decor() {
    return new SecretDecor(this.object());
}
项目:jcabi-log    文件:MsDecorTest.java   
@Override
public Formattable decor() {
    return new MsDecor((Long) this.object());
}
项目:jcabi-log    文件:ObjectDecorTest.java   
@Override
public Formattable decor() {
    return new ObjectDecor(this.object());
}
项目:jcabi-log    文件:ListDecorTest.java   
@Override
public Formattable decor() throws Exception {
    return new ListDecor(this.object());
}
项目:freeVM    文件:FormatterTest.java   
/**
 * @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());
        }
    }
}
项目:freeVM    文件:FormatterTest.java   
/**
 * @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());
        }
    }
}
项目:gestock    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 *
 * @param formattable the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:VectorAttackScanner    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 * 
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:astor    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 * 
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:astor    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 * 
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:cJUnit-mc626    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 * 
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:gwt-commons-lang3    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 *
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
@GwtIncompatible("incompatible method")
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:EndHQ-Libraries    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 * 
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:commons-text    文件:FormattableUtils.java   
/**
 * Get the default formatted representation of the specified
 * {@code Formattable}.
 *
 * @param formattable  the instance to convert to a string, not null
 * @return the resulting string, not null
 */
public static String toString(final Formattable formattable) {
    return String.format(SIMPLEST_FORMAT, formattable);
}
项目:jcabi-log    文件:AbstractDecorTest.java   
/**
 * Get decor with the object.
 * @return The decor to test
 * @throws Exception If some problem
 */
protected abstract Formattable decor() throws Exception;