Class Format
- All Implemented Interfaces:
Formattable,Serializable,Cloneable
- Direct Known Subclasses:
Format.Annotated,Format.Compound,Format.Empty,Format.Literal,Format.Markup,Format.Prior,Format.Space,Format.Subst,Format.Variable
(This class is at the same time (3) a "lexical containing class" for its sub-classes, which are declared "static". But this is intendend as a mere "lexical" comprehension. Of course this is bad design, esp. because each sub-class inherits itself as an inner class. But the initial author of this package prefers larger, but fewer source files !-)
For a detailed description of concepts see
the user documentation.
Format objects have algebraic semantics, they are implemented "imutable". They are created by calls to the static factory methods from this class, and cannot be modified, once created. Therefore complicated structures can be shared and re-used.
Their internal attribute caches are once calculated at creation time and need
never be updated.
If modifications are necessary, they are done by cloning, i.e. creating
new instances.
The subclass Format.Compound
is superclass of all Formats which combine several formats (horizontally,
vertically, etc.)
It provides methods for optimal cloning and clean re-calculating of the
internal attributes.
The static factory methods contained herein (like
append(Iterable),
append(Format[]),
block(Iterable),
block(Format[]),
line(Iterable),
beside(Iterable),
beneath(Iterable),
tabular(Iterable),
comment(String,String,String,Iterable), 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 will be displayed correctly.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classPlaceholder for data annotations.static classPrints contained sub-formats always horizontally adjacent.static classPrints its sub-formats vertically aligned one beneath the other.static classPrints all sub-formats directly adjacent, even if they are multi-line.static classPrints all sub-formats in the same line as long as possible, and starts a new line for every sub-format which would (probably) exceeded the page width.static classVariable identified by an index number, resolved by un-named arguments by counting their positions.static classArranges all sub-formats with leading asterisks, like a javadoc comment.static classThe base class for all formats which combine (= "contain") sub-formats.static classRealizes a map of bindings for a collection ofFormat.BoundVariables andFormat.FreeVariables.static classNeeded eg for case distinctions, when one alternative shall produce nothing.static classCentral Visitor for formatted printing.static classVariable identified by a alphanumeric name which can be bound explicitly.static classPrints all sub-formats in the same horizional line if this is possible within the page width, otherwise it prints all sub-formats in one vertical line.static classPrinted literally to the output.static classPrinted literally to the output, but not included in width calculation.static interfaceBase class for Visitor and Rewriter.protected static classPrints a format discarding all formatting.static classThis format prints the "parens" format, if it is (directly or indirectly) contained in a Prior Format with a higher priority 'level" (numerically larger priority code).static classGeneral purpose Rewriterstatic classCauses horizontal space (of variable width) when in the middle of a line, or one single blank line (independent of its width) when in vertical stack.static classbt [__] FIXME DOK FEHLT, NEVER TESTED !?!? NEVER USED !?!?static classRealizes one(1) line of a table, and prints sub-formats starting in the column indicated by the value of theirFormat.Compound.indent(int).static classCommon base class forFormat.FreeVariableandFormat.BoundVariablestatic classGeneral purpose Visitor -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FormatA public constant reference to a literal format containing a comma.static final intDefault page width used byprintFormat(PrintWriter)static final FormatA public constant reference to a literal format containing a dot.static final FormatA constant reference to an empty format.final booleanReflects that this format does not contain variables.final intThe indentation value explicitly assigned by the user.static final intDefault value for indentation, meaning no explicit specification.final booleanReflects whether format contains any (non-singleton)Format.Beneathformats.protected static final intLength of common class name prefix, which can be suppressed in debug output.final intContains 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.final intContains the minimal width if not directly at start of line, ie respecting blanks and not including indentation.static final PatternMatches the characters which are currently not allowed inFormat.Literals.static final FormatA constant reference to a space of one(1) column/line. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFormat(int i, int a, boolean mult, boolean ground) Private constructor, setting all fields of the Format definition, but setting indent to default.protectedFormat(int i, int a, boolean mult, boolean ground, int indent) Private constructor, setting all fields of the Format definition. -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> FormatCreates anFormat.Annotatedformat.static FormatSame asappend(Iterable)static FormatCallsmakeAppend(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.static FormatReturns the format which results from replacing allFormat.BoundVariables by the argument on the position given by their number.static Formatapply(Format body, Format.Context c, boolean partial) Returns the format which results from replacing allFormat.Variables by their binding defined in Context c, if any.static Formatapply(Format body, Formattable... args) Returns the format which results from replacing allFormat.BoundVariables 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) Most frequently used service provider for resolving variables in formats.final FormatMost frequently used service provider for resolving variables in formats.final FormatapplyTo(Formattable... args) Most frequently used service provider for resolving variables in formats.static FormatSame asbeneath(Iterable)static FormatCallsmakeBeneath(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.static FormatSame asbeside(Iterable)static FormatCallsmakeBeside(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.static FormatSame asblock(Iterable)static FormatCallsmakeBlock(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.static FormatCreates anFormat.Commentformat.static Formatcomment(@Opt String prefix, String linePrefix, @Opt String suffix, Iterable<@Opt ? extends Format> f) Creates anFormat.Commentformat.static FormatCreates anFormat.Commentformat.static FormatCreates anFormat.Commentformat.abstract StringdebugPrefix(int n) Overridden by subclasses to deliver a printable representation for debugging purpose.protected abstract voidOver-ridden by all sub-classes of Format, for performing the adquate print operations.eval(Format.Context c, boolean partial) 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.abstract Formatindent(int i) Returns a clone ofthiswith the new value ofindent.protected booleanWhether an indentation value has been set explicitly.protected booleanisSpace()Whether this format is a space.static booleanReturns true iff argument is valid as content of a "literal" format, i.e.static FormatSame asline(Iterable)static FormatCallsmakeLine(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list.static FormatCreate a literal or return an already cached one.protected static FormatmakeAppend(List<Format> f) Returns an empty format if list is empty, or the only contained format, or anFormat.Appendformat of the multiple contents.protected static FormatmakeBeneath(List<Format> f) Returns an empty format if list is empty, or the only contained format, or aFormat.Beneathformat of the multiple contents.protected static FormatmakeBeside(List<Format> f) Returns an empty format if list is empty, or the only contained format, or aFormat.Besideformat of the multiple contents.protected static FormatReturns an empty format if list is empty, or the only contained format, or aFormat.Blockformat of the multiple contents.protected static FormatReturns an empty format if list is empty, or the only contained format, or aFormat.Lineformat of the multiple contents.protected static FormatmakeTabular(List<Format> f) Returns an empty format if list is empty, or aFormat.Tabularformat containing the one or more formats from the list.static FormatCreates a mark-up format (not occupying any space), same asmarkupRight(String).static FormatmarkupLeft(String s) Creates a mark-up format (not occupying any space).static FormatCreates a mark-up format (not occupying any space).protected static intCalculate the maximum of theminWidthfields of all Formats in "subs".static FormatvoidCentral public function for printing a format with a default page width.voidprintFormat(PrintWriter pw, int width) Central public function for printing a format with the explicitly given page width.voidPrint a format without any formatting: Only small strctures are human-readable, for debugging.static FormatCreates aFormat.Priorformat.static FormatCreates aFormat.Priorformat.static FormatServer<Format> server()static StringCreates 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 newFormat.Substsubstitution object.static Formatsubst(Format body, Format.Context c, boolean partial) Creates a newFormat.Substsubstitution object.static FormatCreates a newFormat.Substsubstitution object where the variable with the given name is bound to the given Format value.static FormatSame astabular(Iterable)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: Only small strctures are human-readable, for debugging.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 newFormat.BoundVariablewith the given number.static FormatCreates a newFormat.FreeVariablewith the given name.
-
Field Details
-
pattern_illegal_literal
Matches the characters which are currently not allowed inFormat.Literals. These are "carriage return", "linefeed", and "horizontal tab". -
comma
A public constant reference to a literal format containing a comma. -
dot
A public constant reference to a literal format containing a dot. -
space
A constant reference to a space of one(1) column/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. -
minWidth
public final int minWidthContains 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.) -
maxWidth
public final int maxWidthContains 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. -
isAlwaysMultiline
public final boolean isAlwaysMultilineReflects whether format contains any (non-singleton)Format.Beneathformats. Needed for "line" and "block", which propagate line breaks contained in their content formats to their own layout. -
ground
public final boolean groundReflects that this format does not contain variables. -
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) Private constructor, setting all fields of the Format definition. -
Format
protected Format(int i, int a, boolean mult, boolean ground) Private constructor, setting all fields of the Format definition, but setting indent to default.
-
-
Method Details
-
format
Any instance of Format is its own formatting result.- Specified by:
formatin interfaceFormattable- Returns:
- a format that represents this object in a pretty-printable way
-
of
-
isValidContentForLiteral
Returns true iff argument is valid as content of a "literal" format, i.e. does neither contain tab nor newline characters. -
literal
Create a literal or return an already cached one.- Throws:
IllegalArgumentException- in case the string argument contains tabs or newlinepattern_illegal_literal.
-
markup
Creates a mark-up format (not occupying any space), same asmarkupRight(String). -
markupRight
Creates a mark-up format (not occupying any space). -
markupLeft
Creates a mark-up format (not occupying any space). -
space
Creates a space format. -
prior
Creates aFormat.Priorformat. -
prior
Creates aFormat.Priorformat. -
makeAppend
Returns an empty format if list is empty, or the only contained format, or anFormat.Appendformat of the multiple contents. -
makeBlock
Returns an empty format if list is empty, or the only contained format, or aFormat.Blockformat of the multiple contents. -
makeLine
Returns an empty format if list is empty, or the only contained format, or aFormat.Lineformat of the multiple contents. -
makeBeside
Returns an empty format if list is empty, or the only contained format, or aFormat.Besideformat of the multiple contents. -
makeBeneath
Returns an empty format if list is empty, or the only contained format, or aFormat.Beneathformat of the multiple contents. -
makeTabular
Returns an empty format if list is empty, or aFormat.Tabularformat containing the one or more formats from the list. -
append
CallsmakeAppend(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list. -
block
CallsmakeBlock(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list. -
line
CallsmakeLine(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list. -
beside
CallsmakeBeside(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list. -
beneath
CallsmakeBeneath(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list. -
tabular
CallsmakeTabular(java.util.List<eu.bandm.tools.format.Format>)while ignoring all empty formats and null pointers contained in the argument list. -
comment
public static Format comment(@Opt @Opt String prefix, String linePrefix, @Opt @Opt String suffix, Iterable<@Opt ? extends Format> f) Creates anFormat.Commentformat. A comment format is created in any case, but all empty formats and null pointers contained in the argument list are ignored. -
comment
public static Format comment(@Opt @Opt String prefix, String linePrefix, @Opt @Opt String suffix, Format... f) Creates anFormat.Commentformat. A comment format is created in any case, but all empty formats and null pointers contained in the argument list are ignored. -
comment
Creates anFormat.Commentformat. 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. -
comment
Creates anFormat.Commentformat. 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. -
append
Same asappend(Iterable) -
block
Same asblock(Iterable) -
line
Same asline(Iterable) -
beside
Same asbeside(Iterable) -
beneath
Same asbeneath(Iterable) -
tabular
Same astabular(Iterable) -
indent
Returns a clone ofthiswith the new value ofindent. (The built-in "clone()" functionality cannot be used, since field "indent" is final). -
annotated
Creates anFormat.Annotatedformat. -
variable
Creates a newFormat.FreeVariablewith the given name. -
variable
Creates a newFormat.BoundVariablewith the given number. -
subst
Creates a newFormat.Substsubstitution object.- Parameters:
body- in which the variables shall be resolvedc-Format.Contextwhich binds names and numbers of variables to Format valuespartial- whether some variables are still unbound
-
subst
Creates a newFormat.Substsubstitution object. Seesubst(Format,Context,boolean), where partial defaults to false. -
subst
Creates a newFormat.Substsubstitution object where the variable with the given name is bound to the given Format value. The argument partial defaults to true. -
apply
Returns the format which results from replacing allFormat.Variables by their binding defined in Context c, if any.- Parameters:
partial- decides when a Variable not defined in the context is found: if true the Variable stays aunaltered; if false an IllegalArgumentException is thrown.
-
apply
Returns the format which results from replacing allFormat.BoundVariables by the argument on the position given by their number.- Throws:
IllegalArgumentException- if a BoundVariable is not defined or a FreeVariable occurs.
-
apply
Returns the format which results from replacing allFormat.BoundVariables by the argument on the position given by their number.- 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. -
applyTo
Most frequently used service provider for resolving variables in formats. It resolves onlyFormat.BoundVariables, ie variables identified by numbers.- Throws:
IllegalArgumentException- if a FreeVariable occurs, or a BoundVariable with number beyond the argument count -1.
-
applyTo
Most frequently used service provider for resolving variables in formats. It resolves onlyFormat.BoundVariables, ie variables identified by numbers.- Throws:
IllegalArgumentException- if a FreeVariable occurs, or a BoundVariable with number beyond the argument count -1.
-
applyPartial
Most frequently used service provider for resolving variables in formats. It resolves onlyFormat.BoundVariables, ie variables identified by numbers. FreeVariable and BoundVariable with number sbeyond the argument count -1 stays as they are.1 -
showLn
Creates a debugging representation of a Format term, restricted to show only the top "depth" levels. -
debugPrefix
Overridden by subclasses to deliver a printable representation for debugging purpose.- Parameters:
n- the maximal number of characters
-
indentIsSet
protected boolean indentIsSet()Whether an indentation value has been set explicitly. -
isSpace
protected boolean isSpace()Whether this format is a space. -
eval
Return the result of replacing all variables with the bindings in the given Context. Will be overridden only by Variables, which need look-up in the context, and by Compound for descending into the sub-formats. These methods do not use the Visitor/Matcher framework, but direct method invocation and basic method dispatch for descending to child nodes.- Parameters:
partial- decides when a Variable not defined in the context is found: if true the Variable stays aunaltered; if false an IllegalArgumentException is thrown.
-
doprint
Over-ridden by all sub-classes of Format, for performing the adquate print operations. To be printed is the raison d'ĂȘtre of Format objects. These methods do not use the Visitor/Matcher framework, but direct method invocation and basic method dispatch for descending to child nodes. -
printFormat
Central public function for printing a format with a default page width. -
printFormat
Central public function for printing a format with the explicitly given page width. -
printNonFormat
Print a format without any formatting: Only small strctures are human-readable, for debugging. UsesFormat.NonFormatPrinter. -
toString
Convert a format without any formatting: Only small strctures are human-readable, for debugging. UsesFormat.NonFormatPrinter. -
toString
Print the format in a formatted way to a string (include linefeeds!), with the specified maximal column width. -
toObjString
CalltoStringfrom theObjectsuperclass, for debugging only. -
maxOfMins
Calculate the maximum of theminWidthfields of all Formats in "subs". -
server
-