鴻蒙OS Formattable

2022-06-20 14:16 更新

Formattable

public interface Formattable

Formattable 接口必須由任何需要使用 Formatter 的 's' 轉(zhuǎn)換說(shuō)明符執(zhí)行自定義格式化的類(lèi)來(lái)實(shí)現(xiàn)。 該接口允許對(duì)任意對(duì)象進(jìn)行格式化的基本控制。 例如,以下類(lèi)根據(jù)標(biāo)志和長(zhǎng)度限制打印出股票名稱(chēng)的不同表示:


 
   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);
       }
   }

 

當(dāng)與 Formatter 結(jié)合使用時(shí),上述類(lèi)為各種格式字符串生成以下輸出。


 
   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."

 

格式化表對(duì)于多線程訪問(wèn)不一定是安全的。 線程安全是可選的,可以由擴(kuò)展和實(shí)現(xiàn)此接口的類(lèi)強(qiáng)制執(zhí)行。

除非另有說(shuō)明,否則將 null 參數(shù)傳遞給此接口中的任何方法都將導(dǎo)致拋出 NullPointerException。

方法總結(jié)

修飾符和類(lèi)型 方法 描述
void formatTo(Formatter formatter, int flags, int width, int precision) 使用提供的 Formatter 格式化對(duì)象。

方法詳情

formatTo

void formatTo(Formatter formatter, int flags, int width, int precision)

使用提供的 Formatter 格式化對(duì)象。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
formatter 格式化程序。 實(shí)現(xiàn)類(lèi)可以調(diào)用 Formatter#out() 或 Formatter#locale() 來(lái)分別獲取此格式化程序使用的 Appendable 或 Locale。
flags 標(biāo)志修改輸出格式。 該值被解釋為位掩碼。 可以設(shè)置以下標(biāo)志的任意組合:FormattableFlags#LEFT_JUSTIFY、FormattableFlags#UPPERCASE 和 FormattableFlags#ALTERNATE。 如果沒(méi)有設(shè)置標(biāo)志,則應(yīng)用實(shí)現(xiàn)類(lèi)的默認(rèn)格式。
width 要寫(xiě)入輸出的最小字符數(shù)。 如果轉(zhuǎn)換后的值的長(zhǎng)度小于寬度,則輸出將用 ' ' 填充,直到字符總數(shù)等于寬度。 默認(rèn)情況下,填充位于開(kāi)頭。 如果設(shè)置了 FormattableFlags#LEFT_JUSTIFY 標(biāo)志,則填充將在末尾。 如果寬度為-1,則沒(méi)有最小值。
precision 要寫(xiě)入輸出的最大字符數(shù)。 精度在寬度之前應(yīng)用,因此即使寬度大于精度,輸出也會(huì)被截?cái)酁榫茸址?nbsp;如果精度為 -1,則對(duì)字符數(shù)沒(méi)有明確限制。

Throws:

Throw名稱(chēng) Throw描述
IllegalFormatException 如果任何參數(shù)無(wú)效。
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)