JavaTM Platform
Standard Ed. 6

java.util
인터페이스 Formattable



public interface Formattable

Formattable 인터페이스를 구현할 필요가 있는 것은,Formatter's' 변환 지시자를 사용해 커스텀 서식을 설정할 필요가 있는 클래스입니다. 이 인터페이스는, 임의의 객체를 서식 설정하기 위한 기본 컨트롤로서 사용할 수 있습니다. 예를 들어, 다음의 클래스는, 플래그 및 길이 제한에 근거해 유명 주식의 다양한 표현을 출력합니다.

   import java.nio.CharBuffer;
   import java.util.Formatter;
   import java.util.Formattable;
   import java.util.Locale;
   import static java.util.FormattableFlags. *;

  ...
 
   public class StockName implements Formattable {
       private String symbol, companyName, frenchCompanyName;
       public StockName(String symbol, String companyName,
                        String frenchCompanyName) {
           ...
       }

       ...

       public void formatTo(Formatter fmt, int f, int width, int precision) {
           StringBuilder sb = new StringBuilder();

           // decide form of name 
           String name = companyName;
           if (fmt.locale(). equals(Locale.FRANCE))
               name = frenchCompanyName;
           boolean alternate = (f & ALTERNATE) == ALTERNATE;
           boolean usesymbol = alternate || (precision ! = -1 && precision < 10);
           String out = (usesymbol ?  symbol : name);

           // apply precision
           if (precision == -1 || out.length() < precision) {
               // write it all
               sb.append(out);
           } else {
               sb.append(out.substring(0, precision - 1)). append('*');
           }

           // apply width and justification
           int len = sb.length(); 
           if (len < width)
               for (int i = 0; i < width - len; i++)
                   if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
                       sb.append(' ');
                   else
                       sb.insert(0, ' ');

           fmt.format(sb.toString());
       }

       public String toString() {
           return String.format("%s - %s", symbol, companyName);
       }
   }
 

Formatter 와 병용 하는 경우, 전술의 클래스는 다음의 다양한 서식 캐릭터 라인을 출력합니다.

   Formatter fmt = new Formatter();
   StockName sn = new StockName("HUGE", "Huge Fruit, Inc. ",
                                "Fruit Titanesque, Inc. ");
   fmt.format("%s", sn);                   //   -> "Huge Fruit, Inc. "
   fmt.format("%s", sn.toString());        //   -> "HUGE - Huge Fruit, Inc. "
   fmt.format("%#s", sn);                  //   -> "HUGE"
   fmt.format("%-10. 8s", sn);              //   -> "HUGE      "
   fmt.format("%. 12s", sn);                //   -> "Huge Fruit,*"
   fmt.format(Locale.FRANCE, "%25s", sn);  //   -> "   Fruit Titanesque, Inc. " 
 

multi-thread 액세스를 실행하는 경우, Formattable 는 반드시 안전하지는 않습니다. thread의 안전성은, 이 인터페이스를 확장 및 구현하는 클래스에 의해 옵션으로 보증됩니다.

특히 지정되어 있지 않은 한, 이 클래스의 인터페이스내의 메소드에 null 인수를 건네주면(자),NullPointerException 가 throw 됩니다.

도입된 버젼:
1.5

메소드의 개요
 void formatTo (Formatter  formatter, int flags, int width, int precision)
          지정된포매터 를 사용해 객체의 서식을 설정합니다.
 

메소드의 상세

formatTo

void formatTo(Formatter  formatter,
              int flags,
              int width,
              int precision)
지정된포매터 를 사용해 객체의 서식을 설정합니다.

파라미터:
formatter - 포매터 . 클래스의 구현에 의해 formatter.out() 또는 formatter.locale() 가 불려 가 이 formatter 로 사용되는 Appendable 또는 Locale 가 각각 취득되는
flags - 플래그에 의해 출력 서식이 변경된다. 값은 비트 마스크로서 해석된다. 플래그 FormattableFlags.LEFT_JUSTIFY ,FormattableFlags.UPPERCASE , 및 FormattableFlags.ALTERNATE 를 임의에 조합해 설정할 수 있다. 플래그가 설정되어 있지 않은 경우, 구현하는 클래스의 디폴트 서식이 적용되는
width - 출력에 기입해지는 최소 문자수. 변환 후의 값의 길이가 width 보다 작은 경우, 총문자수가 width 에 동일해질 때까지 출력에 '  ' 가 패딩 된다. 디폴트에서는, 패딩은 선두에 대해서 행해진다. FormattalbeFlags#LEFT_JUSTIFY 플래그가 설정되었을 경우, 패딩은 끝에 대해서 행해진다. width-1 의 경우, 최소치는 존재하지 않는
precision - 출력에 기입해지는 최대 문자수. precision 는 width 의 전에 적용되기 (위해)때문에,width 의 값이 precision 보다 큰 경우에서도, 출력은 precision 로 지정된 문자수에 절약할 수 있다. precision-1 의 경우, 문자수에 명시적인 제한은 존재하지 않는다
예외:
IllegalFormatException - 어느 파라미터도 부정한 경우. 가능성이 있는 서식 에러 모든 자세한 것은, formatter 클래스 스펙의「상세」섹션을 참조

JavaTM Platform
Standard Ed. 6

버그의 보고와 기능의 요청
한층 더 자세한 API 레퍼런스 및 개발자 문서에 대해서는,Java SE 개발자용 문서를 참조해 주세요. 개발자전용의 상세한 해설, 개념의 개요, 용어의 정의, 버그의 회피책, 및 코드 실례가 포함되어 있습니다.

Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy 도 참조해 주세요.