JavaTM 2 Platform
Standard Ed. 5.0

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 ¿Í º´¿ë ÇÏ´Â °æ¿ì, Àü¼úÀÇ Å¬·¡½º´Â ´ÙÀ½ÀÇ ´Ù¾çÇÑ ¼­½Ä ij¸¯ÅÍ ¶óÀÎÀ» Ãâ·ÂÇÕ´Ï´Ù.

 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À» ¹ß»ý½Ãŵ´Ï´Ù.

µµÀÔµÈ ¹öÀü :
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 - ¾î´À ÆĶó¹ÌÅ͵µ ºÎÁ¤ÇÑ °æ¿ì. °¡´ÉÇÑ ¸ðµç ¼­½Ä ¿¡·¯ÀÇ »ç¾ç¿¡ ´ëÇؼ­´Â Æ÷¸ÅÅÍ Å¬·¡½ºÀÇ »ç¾ç¿¡ ´ëÇØ ¼³¸íÇÑ¡¸»ó¼¼¡¹¸¦ ÂüÁ¶

JavaTM 2 Platform
Standard Ed. 5.0

Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy µµ ÂüÁ¶ÇϽʽÿÀ.