Class Format
- All Implemented Interfaces:
Formattable,Serializable,Cloneable
For a detailed description of the concepts see the user documentation.
Format objects have algebraic semantics, they are implemented immutable. They are created by invoking the static factory methods contained in this class. Once created, they cannot be modified. Therefore complicated structures can be shared and reused.
The attributes of a format object are calculated once at creation time
and never need update. They are implemented as public final fields.
If modifications are necessary, they are done by cloning, i.e. creating
new instances.
The subclass Compound
is superclass of all Formats which combine several formats (horizontally,
vertically, etc.)
It provides methods for cloning and re-calculating of the
internal attributes.
The static factory methods contained herein (like
append(List),
append(Format[]),
block(List),
block(Format[]),
line(List),
beside(List),
beneath(List),
tabular(List),
comment(String,String,String,List), etc.)
will all create only compounds with non-denormalized contents,
i.e. with more than one contained Format.
But also denormalized instances of compound subclasses
(with zero or one component) will be displayed correctly.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FormatA literal format containing a comma »,«.static final intDefault page width used byprintFormat(PrintWriter)static final FormatA literal format containing a dot ».«.static final FormatA constant reference to an empty format.final intThe indentation value explicitly assigned by the user.static final intDefault value for indentation, meaning no explicit specification.protected static final intLength of common class name prefix, which can be suppressed in debug output.static final FormatA space of one(1) column or line. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFormat(int i, int a, boolean mult, boolean ground) Protected constructor, setting all fields of the Format definition, but leaving the indentation unset.protectedFormat(int i, int a, boolean mult, boolean ground, int indent) Protected constructor, setting all fields of the Format definition. -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> FormatCreates an annotated format.static FormatReturns (in most cases) a new append format with the given contents.static FormatReturns (in most cases) a new append format with the given contents.static FormatReturns the format which results from replacing allVariables by their binding defined in contextc, if any.static FormatReturns the format which results from replacing all bound (=numeric) variables by the argument on the position given by their number.static Formatapply(Format body, Formattable... args) Returns the format which results from replacing all bound (=numeric) variables by the argument on the position given by their number.static FormatReturns the format which results from replacing all FreeVariable with the given name by the given value.final FormatapplyPartial(Formattable... args) Resolve all bound (=numbered) variables in this format against the list of arguments.final FormatResolve all bound (=numbered) variables in this format against the list of arguments.final FormatapplyTo(Formattable... args) Resolve all bound (=numbered) variables in this format against the list of arguments.static FormatReturns (in most cases) a new beneath format with the given contents.static FormatReturns (in most cases) a new beneath format with the given contents.static FormatReturns (in most cases) a new beside format with the given contents.static FormatReturns (in most cases) a new beside format with the given contents.static FormatReturns (in most cases) a new block format with the given contents.static FormatReturns (in most cases) a new block format with the given contents.static voidcheckValidIndent(int i) static FormatCreates an comment format.static FormatCreates a comment format.static FormatCreates an comment format.static FormatCreates an comment format.abstract StringdebugPrefix(int n) Deliver the concatenation of the text contents, limited by the given number of characters, approximately.protected abstract voidOver-ridden by all sub-classes of Format, for performing the print operations.Return the result of replacing all variables with the bindings in the given context.final Formatformat()Any instance of Format is its own formatting result.intReturns the indentation value explicitly assigned by the user.booleanground()Returns whether this format does not contain variables.abstract Formatindent(int i) Returns a clone ofthiswith the new value ofindent.protected booleanWhether this indentation value is a specific one.booleanReturns whether this format contains anyBeneathformat.protected booleanisSpace()Whether this format is a space.static FormatReturns (in most cases) a new line format with the given contents.static FormatReturns (in most cases) a new line format with the given contents.static FormatCreate a literal or return an already cached one.static FormatCreates a mark-up format.static FormatmarkupLeft(String s) Creates a mark-up format.static FormatCreates a mark-up format.protected static intCalculate the maximum of theminWidthfields of all Formats insubs.intmaxWidth()Return the maximal width, ie reflects the printing situation where no single contained optional line-break contained in (the sub-formats of) this format is realized.intminWidth()Return the minimal width if not directly at start of line, ie respecting blanks and not including indentation.static FormatReturns a format which represents the argument as a textual representation.voidService point: print this format.voidprintFormat(PrintWriter pw, int width) Service point: print this format with the given line width.voidService point: print this format without any formatting.static FormatCreates a priority-controlled format, which is rendered differently, according to its relative positions to ancestor formats of this class.static FormatCreates a priority-controlled format, which is rendered differently, according to its relative positions to ancestor formats of this class.Returns a text representation of the attributes of a format.static FormatServer<Format> server()showLn(int depth) Creates a debugging representation of a Format term, restricted to show only the top "depth" levels.static Formatspace(int i) Creates a space format.static FormatCreates a scope which imposes the given variable substitutions on a subtree of formats.static FormatCreates a scope which imposes the given variable substitutions on a subtree of formats.static FormatCreates a new scope in which the variable with the given name is bound to the given Format value.static FormatCallsmakeTabular(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.static FormatCallsmakeTabular(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.CalltoStringfrom theObjectsuperclass, for debugging only.toString()Convert a format without any formatting.toString(int width) Print the format in a formatted way to a string (include linefeeds), with the specified maximal column width.static Formatvariable(int index) Creates a newBoundVariablewith the given number.static FormatCreates a newFreeVariablewith the given name.
-
Field Details
-
comma
A literal format containing a comma »,«. -
dot
A literal format containing a dot ».«. -
space
A space of one(1) column or line. -
empty
A constant reference to an empty format. -
LENGTH_PREFIX
protected static final int LENGTH_PREFIXLength of common class name prefix, which can be suppressed in debug output. -
INDENT_NOT_SET
public static final int INDENT_NOT_SETDefault value for indentation, meaning no explicit specification.- See Also:
-
indent
public final int indentThe indentation value explicitly assigned by the user. This will determine the number of spaces the format will begin further to the right then the beginning of its immediate container, iff the format is the first in an output line. Allowed values are greater or equal zero. -
DEFAULT_WIDTH
public static final int DEFAULT_WIDTHDefault page width used byprintFormat(PrintWriter)- See Also:
-
-
Constructor Details
-
Format
protected Format(int i, int a, boolean mult, boolean ground, int indent) Protected constructor, setting all fields of the Format definition. This should not be called spontaneously by the user, because the caller is responsible for the consistency of all parameters.- Parameters:
i- the minimal width of the formata- the maximal width of the formatmult- whether the format will print to multiple lines anyhowground- whether the format does not contain any variableindent- the indentation value of this format- Throws:
IllegalArgumentException- if ident value not valid, seecheckValidIndent(int)
-
Format
protected Format(int i, int a, boolean mult, boolean ground) Protected constructor, setting all fields of the Format definition, but leaving the indentation unset.- Parameters:
i- the minimal width of the formata- the maximal width of the formatmult- whether the format will print to multiple lines anyhowground- whether the format does not contain any variable- See Also:
-
-
Method Details
-
format
Any instance of Format is its own formatting result.- Specified by:
formatin interfaceFormattable- Returns:
- This instance, which already is a format.
-
of
Returns a format which represents the argument as a textual representation. If the argument implementsFormattableorFormatClient, the methods defined with these types are used. As a fall-back, the JavaObject.toString()method is used; from the result, aLiteralis constructed.- Parameters:
thing- to convert into a format- Returns:
- a format which represents the argument textually.
-
server
-
literal
Create a literal or return an already cached one. This factory method usesliteralCacheand is thus synchronized. Empty content will be translated into an empty format; whitespace only into a space format. Tab and linefeed characters are rejected.- Parameters:
s- the text to be contained in the literal- Returns:
- a new literal format instance, or the empty instance if and only if the argument is empty.
- Throws:
IllegalArgumentException- if the text contains forbidden characters as defined byLiteral.ILLEGAL_CONTENTS.- See Also:
-
markup
Creates a mark-up format. A mark-up format is rendered under the assumption that it does not occupy any space. This method is the same asmarkupLeft(String).- Parameters:
s- the text to be printed as mark-up (in case of XML, including the parentheses.- Returns:
- a markup format
-
markupRight
Creates a mark-up format. A mark-up format is rendered under the assumption that it does not occupy any space. This markup is assumed to stand right to its contents, like an XML "close tag".- Parameters:
s- the text to be printed as mark-up (in case of XML, including the parentheses.- Returns:
- a markup format
-
markupLeft
Creates a mark-up format. A mark-up format is rendered under the assumption that it does not occupy any space. This markup is assumed to stand left to its contents, like an XML "open tag".- Parameters:
s- the text to be printed as mark-up (in case of XML, including the parentheses.)- Returns:
- a markup format
-
space
Creates a space format.- Parameters:
i- the number of space characters contained, must be larger than zero.- Returns:
- a space format, or an empty format if the argument is zero.
- Throws:
IllegalArgumentException- if the argument is smaller than one.
-
prior
Creates a priority-controlled format, which is rendered differently, according to its relative positions to ancestor formats of this class. For details seePrior.- Parameters:
level- the level of this format. A higher numerical value means a higher binding power, and low-binding compounds in high-bounding context must appear parenthesized.noparens- the format to print when no parentheses are necessaryparens- the format to print when parentheses are necessary- Returns:
- the constructed format
-
prior
Creates a priority-controlled format, which is rendered differently, according to its relative positions to ancestor formats of this class. For details seePrior.- Parameters:
level- the level of this format. A higher numerical value means a higher binding power, and low-binding compounds in high-bounding context must appear parenthesized.alternative- a code which separates incompatible operators with the same binding power.noparens- the format to print when no parentheses are necessaryparens- the format to print when parentheses are necessary- Returns:
- the constructed format
-
append
Returns (in most cases) a new append format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is an append format, empty, or the singleton argument.
-
block
Returns (in most cases) a new block format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a block format, empty, or the singleton argument.
-
line
Returns (in most cases) a new line format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a line format, empty, or the singleton argument.
-
beside
Returns (in most cases) a new beside format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a beside format, empty, or the singleton argument.
-
beneath
Returns (in most cases) a new beneath format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a beneath format, empty, or the singleton argument.
-
tabular
CallsmakeTabular(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.- Parameters:
f- list of subformats- Returns:
- a tabular format
-
comment
public static Format comment(@Opt @Opt String prefix, String linePrefix, @Opt @Opt String suffix, List<@Opt ? extends Format> f) Creates a comment format. A comment format is created in any case, but all empty formats and null pointers contained in the argument list are ignored.- Parameters:
prefix- the string to lead in the first linelinePrefix- the string to lead in all inner lines, and the first line ifprefixis null.suffix- the string to lead in an additional last line, if not null.f- the commented lines- Returns:
- a comment format
-
comment
Creates an comment format. A comment format is created in any case, but all empty formats and null pointers contained in the argument list are ignored. Prefix and suffix default to null = not present.- Parameters:
linePrefix- the string to lead in all linesf- the commented lines- Returns:
- a comment format
-
append
Returns (in most cases) a new append format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is an append format, empty, or the singleton argument.
-
block
Returns (in most cases) a new block format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a block format, empty, or the singleton argument.
-
line
Returns (in most cases) a new line format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a line format, empty, or the singleton argument.
-
beside
Returns (in most cases) a new beside format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a beside format, empty, or the singleton argument.
-
beneath
Returns (in most cases) a new beneath format with the given contents. First the list is purged from all null pointers and empty formats. If then the list is empty, an empty format is returned. If the list has only one component, then that is returned. Otherwise a new append format is constructed and returned.- Parameters:
f- the contents for the new format.- Returns:
- a format which is a beneath format, empty, or the singleton argument.
-
tabular
CallsmakeTabular(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.- Parameters:
f- list of subformats- Returns:
- a tabular format
-
comment
public static Format comment(@Opt @Opt String prefix, String linePrefix, @Opt @Opt String suffix, Format... f) Creates an comment format. A comment format is created in any case, but all empty formats and null pointers contained in the argument list are ignored.- Parameters:
prefix- the string to lead in the first linelinePrefix- the string to lead in all inner lines, and the first line ifprefixis null.suffix- the string to lead in an additional last line, if not null.f- the commented lines- Returns:
- a comment format
-
comment
Creates an comment format. A comment format is created in any case, but all empty formats and null pointers contained in the argument list are ignored. Prefix and suffix default to null = not present.- Parameters:
linePrefix- the string to lead in all linesf- the commented lines- Returns:
- a comment format
-
maxOfMins
Calculate the maximum of theminWidthfields of all Formats insubs.- Parameters:
subs- the formats to analyse- Returns:
- the maximum of all minimal width values
-
indent
Returns a clone ofthiswith the new value ofindent.- Parameters:
i- the new value for indent.- Returns:
- an new format with identical values but i, or this, if i is its indent value.
- Throws:
IllegalArgumentException- if ident value not valid, seecheckValidIndent(int)
-
annotated
Creates an annotated format.- Type Parameters:
A- the type of the annotated data.- Parameters:
format- the format to annotateannotation- the annotead data- Returns:
- an annotated format.
-
variable
Creates a newFreeVariablewith the given name.- Parameters:
name- of the new variable- Returns:
- a new variable
-
variable
Creates a newBoundVariablewith the given number.- Parameters:
index- of the new variable- Returns:
- a new variable
-
subst
Creates a scope which imposes the given variable substitutions on a subtree of formats. These substitutions will become effective when the variables are resolved by the descending execution ofeval(Context,boolean).- Parameters:
body- in which the variables shall be resolvedc-Contextwhich binds names and numbers of variables to Format valuespartial- whether some variables are still unbound- Returns:
- a new subst format
-
subst
Creates a scope which imposes the given variable substitutions on a subtree of formats. These substitutions will become effective when the variables are resolved by the descending execution ofeval(Context,boolean).- Parameters:
body- in which the variables shall be resolvedargs- the format values which are bound to the numbered variablesBoundVariableascending from zero.- Returns:
- a new subst format
-
subst
Creates a new scope in which the variable with the given name is bound to the given Format value. This substitutions will become effective when the variables are resolved by the descending execution ofeval(Context,boolean). The argument partial defaults to true.- Parameters:
body- in which the variables shall be resolvedname- of the variable to define for the new scopevalue- of the variable to define for the new scope- Returns:
- a new subst format
-
apply
Returns the format which results from replacing allVariables by their binding defined in contextc, if any.- Parameters:
body- format in which all variables shall be replaced by the defined valuec- defines the values for all or some of the variables to resolvepartial- decides when a Variable not defined in the context is found: if true the Variable stays unaltered; if false an IllegalArgumentException is thrown.- Returns:
- the format with possibly variables replaced by their value in context
-
apply
Returns the format which results from replacing all bound (=numeric) variables by the argument on the position given by their number. Evaluation must be total.- Parameters:
body- format in which all bound (=numeric) variables shall be replaced by the defined valueargs- formats which are assigned to the bound (=numeric) variables in ascending order- Returns:
- a format with variables replaced by values
- Throws:
IllegalArgumentException- if a BoundVariable is not defined or a FreeVariable occurs.
-
apply
Returns the format which results from replacing all bound (=numeric) variables by the argument on the position given by their number. Evaluation must be total.- Parameters:
body- format in which all bound (=numeric) variables shall be replaced by the defined valueargs- formats which are assigned to the bound (=numeric) variables in ascending order- Returns:
- a format with variables replaced by values
- Throws:
IllegalArgumentException- if a BoundVariable is not defined or a FreeVariable occurs.
-
apply
Returns the format which results from replacing all FreeVariable with the given name by the given value. Evaluation is partial: All unbound Variables stay untouched and no exception occurs.- Parameters:
body- format in which one free (=named) variable shall be replaced by the defined valuename- of the variable to replacevalue- with which to replace the variable- Returns:
- a format with variables replaced by values
-
applyTo
Resolve all bound (=numbered) variables in this format against the list of arguments. Evaluation must be total.- Parameters:
args- formats which are assigned to the bound (=numeric) variables in ascending order- Returns:
- a format with variables replaced by values
- Throws:
IllegalArgumentException- if a BoundVariable is not defined or a FreeVariable occurs.
-
applyTo
Resolve all bound (=numbered) variables in this format against the list of arguments. Evaluation must be total.- Parameters:
args- formats which are assigned to the bound (=numeric) variables in ascending order- Returns:
- a format with variables replaced by values
- Throws:
IllegalArgumentException- if a BoundVariable is not defined or a FreeVariable occurs.
-
applyPartial
Resolve all bound (=numbered) variables in this format against the list of arguments. Evaluation can be partial: free variable and bound variable with numbers beyond the argument length -1 stays as they are.1- Parameters:
args- formats which are assigned to the bound (=numeric) variables in ascending order- Returns:
- a format with variables replaced by values
- Throws:
IllegalArgumentException- if a BoundVariable is not defined or a FreeVariable occurs.
-
showLn
Creates a debugging representation of a Format term, restricted to show only the top "depth" levels.- Parameters:
depth- the limit to descend into the format tree- Returns:
- a text representation of the data structure
-
properties
Returns a text representation of the attributes of a format.[1/13/g/m/-1] | | | | | | | | | indent | | | always multiline ("m") or not ("-") | | ground ("g") or not ("-") min and max width -
debugPrefix
Deliver the concatenation of the text contents, limited by the given number of characters, approximately. Useful for debugging purpose.- Parameters:
n- the maximal number of characters which can be generated per tree node.- Returns:
- the concatenated, unformatted text contents.
-
indentIsSet
protected boolean indentIsSet()Whether this indentation value is a specific one.- Returns:
- whether this indentation value is a specific one.
-
checkValidIndent
public static void checkValidIndent(int i) -
isSpace
protected boolean isSpace()Whether this format is a space.- Returns:
- whether this format is a space.
-
getIndent
public int getIndent()Returns the indentation value explicitly assigned by the user. This will determine the number of spaces the format will begin further to the right then the beginning of its immediate container, iff the format is the first in an output line.- Returns:
- the indentation value explicitly assigned by the user.
-
minWidth
public int minWidth()Return the minimal width if not directly at start of line, ie respecting blanks and not including indentation. (This is because after starting a new line, no decision on line break is necessary anyhow.)- Returns:
- the minimal width if not directly at start of line.
-
maxWidth
public int maxWidth()Return the maximal width, ie reflects the printing situation where no single contained optional line-break contained in (the sub-formats of) this format is realized.- Returns:
- the maximal width.
-
isAlwaysMultiline
public boolean isAlwaysMultiline()Returns whether this format contains anyBeneathformat. Needed for line and block formats, which propagate line breaks contained in their content formats to their own layout.- Returns:
- whether this format contains any non-denormalized
Beneathformat.
-
ground
public boolean ground()Returns whether this format does not contain variables.- Returns:
- whether this format does not contain variables.
-
eval
Return the result of replacing all variables with the bindings in the given context.- Parameters:
c- defines the replacement for variables by name and number.partial- decides when a Variable not defined in the context is found: if true the Variable stays aunaltered; if false an IllegalArgumentException is thrown.- Returns:
- a format with all or some variables replaced
- Throws:
IllegalArgumentException- ifpartialis false and one or multiple variables cannot be resolved.
-
doprint
Over-ridden by all sub-classes of Format, for performing the print operations.- Parameters:
v- realizes the dynamic context for printing
-
printFormat
Service point: print this format.- Parameters:
pw- the target for printing
-
printFormat
Service point: print this format with the given line width.The given limit is not absolute and possibly transgressed by a few characters.
- Parameters:
pw- the target for printingwidth- the line width to respect by inserting line feeds
-
printNonFormat
Service point: print this format without any formatting.Only small structures are human-readable. It useful for debugging or automated processing, e.g. as map keys. This method uses a
NonFormatPrinter.- Parameters:
pw- the target for printing
-
toString
Convert a format without any formatting.Only small structures are human-readable. It useful for debugging or automated processing, e.g. as map keys. This method uses a
NonFormatPrinter. -
toString
Print the format in a formatted way to a string (include linefeeds), with the specified maximal column width.- Parameters:
width- the line width to respect by inserting line feeds- Returns:
- a text representaton including line feeds and other formatting.
-
toObjString
CalltoStringfrom theObjectsuperclass, for debugging only.- Returns:
- the text representatio as defined by
Object.
-