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
Modifier and TypeClassDescriptionstatic class
Foreseen for data annotations.static class
Prints contained sub-formats always horizontally adjacent.static class
Prints its sub-formats vertically aligned one beneath the other.static class
Prints all sub-formats directly adjacent, even if they are multi-line.static class
Prints 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 class
Variable identified by an index number, resolved by un-named arguments by counting their positions.static class
Arranges all sub-formats with leading asterisks, like a javadoc comment.static class
The base class for all formats which combine (= "contain") sub-formats.static class
Realizes a map of bindings for a collection ofFormat.BoundVariable
s andFormat.FreeVariable
s.static class
Needed eg for case distinctions, when one alternative shall produce nothing.static class
Central Visitor for formatted printing.static class
Variable identified by a alphanumeric name which can be bound explicitly.static class
Prints 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 class
Printed literally to the output.static class
Printed literally to the output, but not included in width calculation.static interface
Base class for Visitor and Rewriter.protected static class
Prints a format discarding all formatting.static class
This 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 class
General purpose Rewriterstatic class
Causes 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 class
bt [__] FIXME DOK FEHLT, NEVER TESTED !?!? NEVER USED !?!?static class
Realizes one(1) line of a table, and prints sub-formats starting in the column indicated by the value of theirFormat.Compound.indent(int)
.static class
Common base class forFormat.FreeVariable
andFormat.BoundVariable
static class
General purpose Visitor -
Field Summary
Modifier and TypeFieldDescriptionstatic final Format
A public constant reference to a literal format containing a comma.static final int
Default page width used byprintFormat(PrintWriter)
static final Format
A public constant reference to a literal format containing a dot.static final Format
A constant reference to an empty format.final boolean
Reflects that this format does not contain variables.final int
The indentation value explicitly assigned by the user.static final int
Default value for indentation, meaning no explicit specification.final boolean
Reflects whether format contains any (non-singleton)Format.Beneath
formats.protected static final int
Length of common class name prefix, which can be suppressed in debug output.final int
Contains 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 int
Contains the minimal width if not directly at start of line, ie respecting blanks and not including indentation.static final Pattern
Matches the characters which are currently not allowed inFormat.Literal
s.static final Format
A constant reference to a space of one(1) column/line. -
Constructor Summary
ModifierConstructorDescriptionprotected
Format
(int i, int a, boolean mult, boolean ground) Private constructor, setting all fields of the Format definition, but setting indent to default.protected
Format
(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> Format
Creates anFormat.Annotated
format.static Format
Same asappend(Iterable)
static Format
CallsmakeAppend(java.util.List<eu.bandm.tools.format.Format>)
while ignoring all empty formats and null pointers contained in the argument list.static Format
Returns the format which results from replacing allFormat.BoundVariable
s by the argument on the position given by their number.static Format
apply
(Format body, Format.Context c, boolean partial) Returns the format which results from replacing allFormat.Variable
s by their binding defined in Context c, if any.static Format
apply
(Format body, Formattable... args) Returns the format which results from replacing allFormat.BoundVariable
s by the argument on the position given by their number.static Format
Returns the format which results from replacing all FreeVariable with the given name by the given value.final Format
applyPartial
(Formattable... args) Most frequently used service provider for resolving variables in formats.final Format
Most frequently used service provider for resolving variables in formats.final Format
applyTo
(Formattable... args) Most frequently used service provider for resolving variables in formats.static Format
Same asbeneath(Iterable)
static Format
CallsmakeBeneath(java.util.List<eu.bandm.tools.format.Format>)
while ignoring all empty formats and null pointers contained in the argument list.static Format
Same asbeside(Iterable)
static Format
CallsmakeBeside(java.util.List<eu.bandm.tools.format.Format>)
while ignoring all empty formats and null pointers contained in the argument list.static Format
Same asblock(Iterable)
static Format
CallsmakeBlock(java.util.List<eu.bandm.tools.format.Format>)
while ignoring all empty formats and null pointers contained in the argument list.static Format
Creates anFormat.Comment
format.static Format
comment
(@Opt String prefix, String linePrefix, @Opt String suffix, Iterable<@Opt ? extends Format> f) Creates anFormat.Comment
format.static Format
Creates anFormat.Comment
format.static Format
Creates anFormat.Comment
format.abstract String
debugPrefix
(int n) Overridden by subclasses to deliver a printable representation for debugging purpose.protected abstract void
Over-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 Format
format()
Any instance of Format is its own formatting result.abstract Format
indent
(int i) Returns a clone ofthis
with the new value ofindent
.protected boolean
Whether an indentation value has been set explicitly.protected boolean
isSpace()
Whether this format is a space.static boolean
Returns true iff argument is valid as content of a "literal" format, i.e.static Format
Same asline(Iterable)
static Format
CallsmakeLine(java.util.List<eu.bandm.tools.format.Format>)
while ignoring all empty formats and null pointers contained in the argument list.static Format
Create a literal or return an already cached one.protected static Format
makeAppend
(List<Format> f) Returns an empty format if list is empty, or the only contained format, or anFormat.Append
format of the multiple contents.protected static Format
makeBeneath
(List<Format> f) Returns an empty format if list is empty, or the only contained format, or aFormat.Beneath
format of the multiple contents.protected static Format
makeBeside
(List<Format> f) Returns an empty format if list is empty, or the only contained format, or aFormat.Beside
format of the multiple contents.protected static Format
Returns an empty format if list is empty, or the only contained format, or aFormat.Block
format of the multiple contents.protected static Format
Returns an empty format if list is empty, or the only contained format, or aFormat.Line
format of the multiple contents.protected static Format
makeTabular
(List<Format> f) Returns an empty format if list is empty, or aFormat.Tabular
format containing the one or more formats from the list.static Format
Creates a mark-up format (not occupying any space), same asmarkupRight(String)
.static Format
markupLeft
(String s) Creates a mark-up format (not occupying any space).static Format
Creates a mark-up format (not occupying any space).protected static int
Calculate the maximum of theminWidth
fields of all Formats in "subs".static Format
void
Central public function for printing a format with a default page width.void
printFormat
(PrintWriter pw, int width) Central public function for printing a format with the explicitly given page width.void
Print a format without any formatting: Only small strctures are human-readable, for debugging.static Format
Creates aFormat.Prior
format.static Format
Creates aFormat.Prior
format.static FormatServer<Format>
server()
static String
Creates a debugging representation of a Format term, restricted to show only the top "depth" levels.static Format
space
(int i) Creates a space format.static Format
Creates a newFormat.Subst
substitution object.static Format
subst
(Format body, Format.Context c, boolean partial) Creates a newFormat.Subst
substitution object.static Format
Creates a newFormat.Subst
substitution object where the variable with the given name is bound to the given Format value.static Format
Same astabular(Iterable)
static Format
CallsmakeTabular(java.util.List<eu.bandm.tools.format.Format>)
while ignoring all empty formats and null pointers contained in the argument list.CalltoString
from theObject
superclass, 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 Format
variable
(int index) Creates a newFormat.BoundVariable
with the given number.static Format
Creates a newFormat.FreeVariable
with the given name.
-
Field Details
-
pattern_illegal_literal
Matches the characters which are currently not allowed inFormat.Literal
s. 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.Beneath
formats. 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:
format
in 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.Prior
format. -
prior
Creates aFormat.Prior
format. -
makeAppend
Returns an empty format if list is empty, or the only contained format, or anFormat.Append
format of the multiple contents. -
makeBlock
Returns an empty format if list is empty, or the only contained format, or aFormat.Block
format of the multiple contents. -
makeLine
Returns an empty format if list is empty, or the only contained format, or aFormat.Line
format of the multiple contents. -
makeBeside
Returns an empty format if list is empty, or the only contained format, or aFormat.Beside
format of the multiple contents. -
makeBeneath
Returns an empty format if list is empty, or the only contained format, or aFormat.Beneath
format of the multiple contents. -
makeTabular
Returns an empty format if list is empty, or aFormat.Tabular
format 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.Comment
format. 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.Comment
format. 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.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. -
comment
Creates anFormat.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. -
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 ofthis
with the new value ofindent
. (The built-in "clone()" functionality cannot be used, since field "indent" is final). -
annotated
Creates anFormat.Annotated
format. -
variable
Creates a newFormat.FreeVariable
with the given name. -
variable
Creates a newFormat.BoundVariable
with the given number. -
subst
Creates a newFormat.Subst
substitution object.- Parameters:
body
- in which the variables shall be resolvedc
-Format.Context
which binds names and numbers of variables to Format valuespartial
- whether some variables are still unbound
-
subst
Creates a newFormat.Subst
substitution object. Seesubst(Format,Context,boolean)
, where partial defaults to false. -
subst
Creates a newFormat.Subst
substitution 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.Variable
s 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.BoundVariable
s by the argument on the position given by their number. -
apply
Returns the format which results from replacing allFormat.BoundVariable
s by the argument on the position given by their number. -
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.BoundVariable
s, ie variables identified by numbers. -
applyTo
Most frequently used service provider for resolving variables in formats. It resolves onlyFormat.BoundVariable
s, ie variables identified by numbers. -
applyPartial
Most frequently used service provider for resolving variables in formats. It resolves onlyFormat.BoundVariable
s, 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
CalltoString
from theObject
superclass, for debugging only. -
maxOfMins
Calculate the maximum of theminWidth
fields of all Formats in "subs". -
server
-