Class HFormatter
- java.lang.Object
-
- net.lax1dude.eaglercraft.v1_8.HFormatter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class HFormatter extends java.lang.Object implements java.io.Closeable, java.io.FlushableThe
Formatterclass is a String-formatting utility that is designed to work like theprintffunction of the C programming language. Its key methods are theformatmethods which create a formattedStringby replacing a set of placeholders (format tokens) with formatted values. The style used to format each value is determined by the format token used. For example, the call
format("My decimal value is %d and my String is %s.", 3, "Hello");
returns theString
My decimal value is 3 and my String is Hello.The format token consists of a percent sign, optionally followed by flags and precision arguments, and then a single character that indicates the type of value being formatted. If the type is a time/date, then the type character
tis followed by an additional character that indicates how the date is to be formatted. The two characters<$immediately following the % sign indicate that the previous value should be used again instead of moving on to the next value argument. A numbernand a dollar sign immediately following the % sign make n the next argument to be used.The available choices are the following:
Text value types sString format("%s, %s", "hello", "Hello");hello, HelloS,sString to capitals format("%S, %S", "hello", "Hello");HELLO, HELLOcCharacter format("%c, %c", 'd', 0x65);d, eCCharacter to capitals format("%C, %C", 'd', 0x65);D, EText option flags
The value between the option and the type character indicates the minimum width in characters of the formatted value-Left justify (width value is required) format("%-3C, %3C", 'd', 0x65);D , EInteger types dint, formatted as decimal format("%d, %d"1$, 35, 0x10);35, 16oint, formatted as octal format("%o, %o", 8, 010);10, 10X,xint, formatted as hexidecimal format("%x, %X", 10, 10);a, AInteger option flags
The value between the option and the type character indicates the minimum width in characters of the formatted value+lead with the number's sign format("%+d, %+4d", 5, 5);+5, +5-Left justify (width value is required) format("%-6dx", 5);5 x#Print the leading characters that indicate hexidecimal or octal (for use only with hex and octal types) format("%#o", 010);010A space indicates that non-negative numbers should have a leading space. format("x% d% 5d", 4, 4);x 4 40Pad the number with leading zeros (width value is required) format("%07d, %03d", 4, 5555);0000004, 5555(Put parentheses around negative numbers (decimal only) format("%(d, %(d, %(6d", 12, -12, -12);12, (12), (12)Float types
A value immediately following the % symbol gives the minimum width in characters of the formatted value; if it is followed by a period and another integer, then the second value gives the precision (6 by default).ffloat (or double) formatted as a decimal, where the precision indicates the number of digits after the decimal. format("%f %<.1f %<1.5f %<10f %<6.0f", 123.456f);123.456001 123.5 123.45600 123.456001 123E,efloat (or double) formatted in decimal exponential notation, where the precision indicates the number of significant digits. format("%E %<.1e %<1.5E %<10E %<6.0E", 123.456f);1.234560E+02 1.2e+02 1.23456E+02 1.234560E+02 1E+02G,gfloat (or double) formatted in decimal exponential notation , where the precision indicates the maximum number of significant digits. format("%G %<.1g %<1.5G %<10G %<6.0G", 123.456f);123.456 1e+02 123.46 123.456 1E+02A,afloat (or double) formatted as a hexidecimal in exponential notation, where the precision indicates the number of significant digits. format("%A %<.1a %<1.5A %<10A %<6.0A", 123.456f);0X1.EDD2F2P6 0x1.fp6 0X1.EDD2FP6 0X1.EDD2F2P6 0X1.FP6Float-type option flags
See the Integer-type options. The options for float-types are the same as for integer types with one addition:,Use a comma in place of a decimal if the locale requires it. format(new Locale("fr"), "%,7.2f", 6.03f);6,03Date types t,TDate format(new Locale("fr"), "%tB %TB", Calendar.getInstance(), Calendar.getInstance());avril AVRILDate format precisions
The format precision character follows thet.A,aThe day of the week format("%ta %tA", cal, cal);Tue Tuesdayb,B,hThe name of the month format("%tb %<tB %<th", cal, cal, cal);Apr April AprCThe century format("%tC\n", cal);20d,eThe day of the month (with or without leading zeros) format("%td %te", cal, cal);01 1FThe complete date formatted as YYYY-MM-DD format("%tF", cal);2008-04-01DThe complete date formatted as MM/DD/YY (not corrected for locale) format(new Locale("en_US"), "%tD", cal);<br/> format(new Locale("en_UK"), " %tD", cal);04/01/08 04/01/08jThe number of the day (from the beginning of the year). format("%tj\n", cal);092mThe number of the month format("%tm\n", cal);04y,YThe year format("%ty %tY", cal, cal);08 2008H,I,k,lThe hour of the day, in 12 or 24 hour format, with or without a leading zero format("%tH %tI %tk %tl", cal, cal, cal, cal);16 04 16 4pa.m. or p.m. format("%tp %Tp", cal, cal);pm PMM,S,L,NThe minutes, seconds, milliseconds, and nanoseconds format("%tM %tS %tL %tN", cal, cal, cal, cal);08 17 359 359000000Z,zThe time zone: its abbreviation or offset from GMT format("%tZ %tz", cal, cal);CEST +0100R,r,TThe complete time format("%tR %tr %tT", cal, cal, cal);16:15 04:15:32 PM 16:15:32s,QThe number of seconds or milliseconds from "the epoch" (1 January 1970 00:00:00 UTC) format("%ts %tQ", cal, cal);1207059412 1207059412656cThe complete time and date format("%tc", cal);Tue Apr 01 16:19:17 CEST 2008Other data types B,bBoolean format("%b, %B", true, false);true, FALSEH,hHashcode format("%h, %H", obj, obj);190d11, 190D11nline separator format("first%nsecond", "???");first<br/> secondEscape sequences %Escape the % character format("%d%%, %d", 50, 60);50%, 60An instance of Formatter can be created to write the formatted output to standard types of output streams. Its functionality can also be accessed through the format methods of an output stream or of
String:
System.out.println(HString.format("%ty\n", cal));
System.out.format("%ty\n", cal);The class is not multi-threaded safe. The user is responsible for maintaining a thread-safe design if a
Formatteris accessed by multiple threads.- Since:
- 1.5
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHFormatter.BigDecimalLayoutFormThe enumeration giving the available styles for formatting very large decimal numbers.static classHFormatter.DuplicateFormatFlagsExceptionstatic classHFormatter.IllegalFormatWidthExceptionstatic classHFormatter.MissingFormatArgumentException
-
Constructor Summary
Constructors Constructor Description HFormatter()Constructs aFormatter.HFormatter(java.io.File file)Constructs aFormatterwhose output is written to the specifiedFile.HFormatter(java.io.File file, java.lang.String csn)Constructs aFormatterwith the given charset, and whose output is written to the specifiedFile.HFormatter(java.io.File file, java.lang.String csn, java.util.Locale l)Constructs aFormatterwith the givenLocaleand charset, and whose output is written to the specifiedFile.HFormatter(java.io.OutputStream os)Constructs aFormatterwhose output is written to the specifiedOutputStream.HFormatter(java.io.OutputStream os, java.lang.String csn)Constructs aFormatterwith the given charset, and whose output is written to the specifiedOutputStream.HFormatter(java.io.OutputStream os, java.lang.String csn, java.util.Locale l)Constructs aFormatterwith the givenLocaleand charset, and whose output is written to the specifiedOutputStream.HFormatter(java.io.PrintStream ps)Constructs aFormatterwhose output is written to the specifiedPrintStream.HFormatter(java.lang.Appendable a)Constructs aFormatterwhose output will be written to the specifiedAppendable.HFormatter(java.lang.Appendable a, java.util.Locale l)Constructs aFormatterwith the specifiedLocaleand whose output will be written to the specifiedAppendable.HFormatter(java.lang.String fileName)Constructs aFormatterwhose output is written to the specified file.HFormatter(java.lang.String fileName, java.lang.String csn)Constructs aFormatterwhose output is written to the specified file.HFormatter(java.lang.String fileName, java.lang.String csn, java.util.Locale l)Constructs aFormatterwith the givenLocaleand charset, and whose output is written to the specified file.HFormatter(java.util.Locale l)Constructs aFormatterwith the specifiedLocale.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes theFormatter.voidflush()Flushes theFormatter.HFormatterformat(java.lang.String format, java.lang.Object... args)Writes a formatted string to the output destination of theFormatter.HFormatterformat(java.util.Locale l, java.lang.String format, java.lang.Object... args)Writes a formatted string to the output destination of theFormatter.java.io.IOExceptionioException()Returns the lastIOExceptionthrown by theFormatter's output destination.java.util.Localelocale()Returns theLocaleof theFormatter.java.lang.Appendableout()Returns the output destination of theFormatter.java.lang.StringtoString()Returns the content by calling thetoString()method of the output destination.
-
-
-
Constructor Detail
-
HFormatter
public HFormatter()
Constructs aFormatter. The output is written to aStringBuilderwhich can be acquired by invokingout()and whose content can be obtained by callingtoString(). TheLocalefor theFormatteris the defaultLocale.
-
HFormatter
public HFormatter(java.lang.Appendable a)
Constructs aFormatterwhose output will be written to the specifiedAppendable. The locale for theFormatteris the defaultLocale.- Parameters:
a- the output destination of theFormatter. Ifaisnull, then aStringBuilderwill be used.
-
HFormatter
public HFormatter(java.util.Locale l)
Constructs aFormatterwith the specifiedLocale. The output is written to aStringBuilderwhich can be acquired by invokingout()and whose content can be obtained by callingtoString().- Parameters:
l- theLocaleof theFormatter. Iflisnull, then no localization will be used.
-
HFormatter
public HFormatter(java.lang.Appendable a, java.util.Locale l)Constructs aFormatterwith the specifiedLocaleand whose output will be written to the specifiedAppendable.- Parameters:
a- the output destination of theFormatter. Ifaisnull, then aStringBuilderwill be used.l- theLocaleof theFormatter. Iflisnull, then no localization will be used.
-
HFormatter
public HFormatter(java.lang.String fileName) throws java.io.FileNotFoundExceptionConstructs aFormatterwhose output is written to the specified file. The charset of theFormatteris the default charset. TheLocalefor theFormatteris the defaultLocale.- Parameters:
fileName- the filename of the file that is used as the output destination for theFormatter. The file will be truncated to zero size if the file exists, or else a new file will be created. The output of theFormatteris buffered.- Throws:
java.io.FileNotFoundException- if the filename does not denote a normal and writable file, or if a new file cannot be created, or if any error arises when opening or creating the file.java.lang.SecurityException- if there is aSecurityManagerin place which denies permission to write to the file incheckWrite(file.getPath()).
-
HFormatter
public HFormatter(java.lang.String fileName, java.lang.String csn) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingExceptionConstructs aFormatterwhose output is written to the specified file. TheLocalefor theFormatteris the defaultLocale.- Parameters:
fileName- the filename of the file that is used as the output destination for theFormatter. The file will be truncated to zero size if the file exists, or else a new file will be created. The output of theFormatteris buffered.csn- the name of the charset for theFormatter.- Throws:
java.io.FileNotFoundException- if the filename does not denote a normal and writable file, or if a new file cannot be created, or if any error arises when opening or creating the file.java.lang.SecurityException- if there is aSecurityManagerin place which denies permission to write to the file incheckWrite(file.getPath()).java.io.UnsupportedEncodingException- if the charset with the specified name is not supported.
-
HFormatter
public HFormatter(java.lang.String fileName, java.lang.String csn, java.util.Locale l) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingExceptionConstructs aFormatterwith the givenLocaleand charset, and whose output is written to the specified file.- Parameters:
fileName- the filename of the file that is used as the output destination for theFormatter. The file will be truncated to zero size if the file exists, or else a new file will be created. The output of theFormatteris buffered.csn- the name of the charset for theFormatter.l- theLocaleof theFormatter. Iflisnull, then no localization will be used.- Throws:
java.io.FileNotFoundException- if the filename does not denote a normal and writable file, or if a new file cannot be created, or if any error arises when opening or creating the file.java.lang.SecurityException- if there is aSecurityManagerin place which denies permission to write to the file incheckWrite(file.getPath()).java.io.UnsupportedEncodingException- if the charset with the specified name is not supported.
-
HFormatter
public HFormatter(java.io.File file) throws java.io.FileNotFoundExceptionConstructs aFormatterwhose output is written to the specifiedFile. The charset of theFormatteris the default charset. TheLocalefor theFormatteris the defaultLocale.- Parameters:
file- theFilethat is used as the output destination for theFormatter. TheFilewill be truncated to zero size if theFileexists, or else a newFilewill be created. The output of theFormatteris buffered.- Throws:
java.io.FileNotFoundException- if theFileis not a normal and writableFile, or if a newFilecannot be created, or if any error rises when opening or creating theFile.java.lang.SecurityException- if there is aSecurityManagerin place which denies permission to write to theFileincheckWrite(file.getPath()).
-
HFormatter
public HFormatter(java.io.File file, java.lang.String csn) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingExceptionConstructs aFormatterwith the given charset, and whose output is written to the specifiedFile. TheLocalefor theFormatteris the defaultLocale.- Parameters:
file- theFilethat is used as the output destination for theFormatter. TheFilewill be truncated to zero size if theFileexists, or else a newFilewill be created. The output of theFormatteris buffered.csn- the name of the charset for theFormatter.- Throws:
java.io.FileNotFoundException- if theFileis not a normal and writableFile, or if a newFilecannot be created, or if any error rises when opening or creating theFile.java.lang.SecurityException- if there is aSecurityManagerin place which denies permission to write to theFileincheckWrite(file.getPath()).java.io.UnsupportedEncodingException- if the charset with the specified name is not supported.
-
HFormatter
public HFormatter(java.io.File file, java.lang.String csn, java.util.Locale l) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingExceptionConstructs aFormatterwith the givenLocaleand charset, and whose output is written to the specifiedFile.- Parameters:
file- theFilethat is used as the output destination for theFormatter. TheFilewill be truncated to zero size if theFileexists, or else a newFilewill be created. The output of theFormatteris buffered.csn- the name of the charset for theFormatter.l- theLocaleof theFormatter. Iflisnull, then no localization will be used.- Throws:
java.io.FileNotFoundException- if theFileis not a normal and writableFile, or if a newFilecannot be created, or if any error rises when opening or creating theFile.java.lang.SecurityException- if there is aSecurityManagerin place which denies permission to write to theFileincheckWrite(file.getPath()).java.io.UnsupportedEncodingException- if the charset with the specified name is not supported.
-
HFormatter
public HFormatter(java.io.OutputStream os)
Constructs aFormatterwhose output is written to the specifiedOutputStream. The charset of theFormatteris the default charset. TheLocalefor theFormatteris the defaultLocale.- Parameters:
os- the stream to be used as the destination of theFormatter.
-
HFormatter
public HFormatter(java.io.OutputStream os, java.lang.String csn) throws java.io.UnsupportedEncodingExceptionConstructs aFormatterwith the given charset, and whose output is written to the specifiedOutputStream. TheLocalefor theFormatteris the defaultLocale.- Parameters:
os- the stream to be used as the destination of theFormatter.csn- the name of the charset for theFormatter.- Throws:
java.io.UnsupportedEncodingException- if the charset with the specified name is not supported.
-
HFormatter
public HFormatter(java.io.OutputStream os, java.lang.String csn, java.util.Locale l) throws java.io.UnsupportedEncodingExceptionConstructs aFormatterwith the givenLocaleand charset, and whose output is written to the specifiedOutputStream.- Parameters:
os- the stream to be used as the destination of theFormatter.csn- the name of the charset for theFormatter.l- theLocaleof theFormatter. Iflisnull, then no localization will be used.- Throws:
java.io.UnsupportedEncodingException- if the charset with the specified name is not supported.
-
HFormatter
public HFormatter(java.io.PrintStream ps)
Constructs aFormatterwhose output is written to the specifiedPrintStream. The charset of theFormatteris the default charset. TheLocalefor theFormatteris the defaultLocale.- Parameters:
ps- thePrintStreamused as destination of theFormatter. Ifpsisnull, then aNullPointerExceptionwill be raised.
-
-
Method Detail
-
locale
public java.util.Locale locale()
Returns theLocaleof theFormatter.- Returns:
- the
Localefor theFormatterornullfor noLocale. - Throws:
java.util.FormatterClosedException- if theFormatterhas been closed.
-
out
public java.lang.Appendable out()
Returns the output destination of theFormatter.- Returns:
- the output destination of the
Formatter. - Throws:
java.util.FormatterClosedException- if theFormatterhas been closed.
-
toString
public java.lang.String toString()
Returns the content by calling thetoString()method of the output destination.- Overrides:
toStringin classjava.lang.Object- Returns:
- the content by calling the
toString()method of the output destination. - Throws:
java.util.FormatterClosedException- if theFormatterhas been closed.
-
flush
public void flush()
Flushes theFormatter. If the output destination isFlushable, then the methodflush()will be called on that destination.- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.util.FormatterClosedException- if theFormatterhas been closed.
-
close
public void close()
Closes theFormatter. If the output destination isCloseable, then the methodclose()will be called on that destination. If theFormatterhas been closed, then calling the this method will have no effect. Any method but theioException()that is called after theFormatterhas been closed will raise aFormatterClosedException.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
ioException
public java.io.IOException ioException()
Returns the lastIOExceptionthrown by theFormatter's output destination. If theappend()method of the destination does not throwIOExceptions, theioException()method will always returnnull.- Returns:
- the last
IOExceptionthrown by theFormatter's output destination.
-
format
public HFormatter format(java.lang.String format, java.lang.Object... args)
Writes a formatted string to the output destination of theFormatter.- Parameters:
format- a format string.args- the arguments list used in theformat()method. If there are more arguments than those specified by the format string, then the additional arguments are ignored.- Returns:
- this
Formatter. - Throws:
java.util.IllegalFormatException- if the format string is illegal or incompatible with the arguments, or if fewer arguments are sent than those required by the format string, or any other illegal situation.java.util.FormatterClosedException- if theFormatterhas been closed.
-
format
public HFormatter format(java.util.Locale l, java.lang.String format, java.lang.Object... args)
Writes a formatted string to the output destination of theFormatter.- Parameters:
l- theLocaleused in the method. Iflocaleisnull, then no localization will be applied. This parameter does not influence theLocalespecified during construction.format- a format string.args- the arguments list used in theformat()method. If there are more arguments than those specified by the format string, then the additional arguments are ignored.- Returns:
- this
Formatter. - Throws:
java.util.IllegalFormatException- if the format string is illegal or incompatible with the arguments, or if fewer arguments are sent than those required by the format string, or any other illegal situation.java.util.FormatterClosedException- if theFormatterhas been closed.
-
-