public interface Formattable
The Formattable interface must be implemented by any class that
needs to perform custom formatting using the 's' conversion
specifier of Formatter. This interface allows basic
control for formatting arbitrary objects.
For example, the following class prints out different representations of a
stock's name depending on the flags and length constraints:
import java.nio.CharBuffer; import java.util.Formatter; import java.util.Formattable; import java.util.Locale; import static java.util.FormattableFlags.*; ... ... } ... // decide form of name String name = companyName; name = frenchCompanyName; boolean alternate = (f & ALTERNATE) == ALTERNATE; boolean usesymbol = alternate || (precision != -1 && precision < 10); // 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()); } } }
When used in conjunction with the Formatter, the above
class produces the following output for various format strings.
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,*"
Formattables are not necessarily safe for multithreaded access. Thread safety is optional and may be enforced by classes that extend and implement this interface.
Unless otherwise specified, passing a null argument to
any method in this interface will cause a NullPointerException to be thrown.
| Method Summary | |
|---|---|
void |
formatTo(Formatter formatter,
int flags,
int width,
int precision)
Formats the object using the provided formatter. |
| Method Detail |
|---|
void formatTo(Formatter formatter, int flags, int width, int precision)
formatter.
formatter - The formatter. Implementing classes may call
formatter.out() or formatter.locale() to obtain the Appendable or Locale used by this
formatter respectively.flags - The flags modify the output format. The value is interpreted as
a bitmask. Any combination of the following flags may be set:
FormattableFlags.LEFT_JUSTIFY, FormattableFlags.UPPERCASE, and FormattableFlags.ALTERNATE. If no flags are set, the default
formatting of the implementing class will apply.width - The minimum number of characters to be written to the output.
If the length of the converted value is less than the
width then the output will be padded by
' ' until the total number of characters
equals width. The padding is at the beginning by default. If
the FormattableFlags.LEFT_JUSTIFY flag is set then the
padding will be at the end. If width is -1
then there is no minimum.precision - The maximum number of characters to be written to the output.
The precision is applied before the width, thus the output will
be truncated to precision characters even if the
width is greater than the precision. If
precision is -1 then there is no explicit
limit on the number of characters.
IllegalFormatException - If any of the parameters are invalid. For specification of all
possible formatting errors, see the Details section of the
formatter class specification.Ces informations proviennent du site de http://java.sun.com
Le contenu de cette page provient du site de Sun, et est généré depuis un cache sur l'infobrol après certains traitements automatisés. La présentation peut donc différer du document original, mais le contenu aussi. Vous pouvez utiliser ce bouton pour afficher la page originale du site de Sun :
Maintenir les pages en cache sur différents sites peut offrir plus de disponibilité.
Chaque page est indexée dans la base de donnée, ce qui permet de retrouver facilement les informations, au moyen des sommaires, du moteur de recherche interne, etc.
Des facilités sont mises en place pour que les membres de l'infobrol puissent effectuer des traductions en français des différents documents. Ceci devrait permettre aux débutants en programmation Java de consulter les API en français s'ils maîtrisent moins bien la langue de Shakespeare. Dans le cas où une traduction a été soumise, elle est disponible au moyen d'un lien en bas de page. Si la traduction a été validée, la page s'affiche par défaut en français, et un lien en bas de page permet d'atteindre la version en anglais.
Le code sur l'infobrol est automatiquement coloré selon la syntaxe, et les différents mots clés sont transformés en liens pour accéder rapidement aux informations.
Vous avez la possibilité de partager vos expériences en proposant vos propres extraits de code en utilisant le bouton "ajouter un commentaire" en bas de page. Si vous visitez simplement l'infobrol, vous avez déjà accès à cette fonction, mais si vous étes membre du brol, vous pouvez en plus utiliser des boutons supplémentaires de mise en forme, dont la coloration automatique de vos extraits de codes.
Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.
6 mots clés dont 0 définis manuellement (plus d'information...).
Avertissement
Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.
Recherche (afficher)
Utilisateur (masquer)
Navigation (masquer)
Apparence (afficher)
Stats (afficher)
Citation (masquer)