- java.lang.Object
-
- com.speedment.common.codegen.util.Formatting
-
public final class Formatting extends Object
Common formatting methods used when generating code.- Author:
- Emil Forslund
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringalignTabs(String rows)Replaces every tab character (\t) in the specified rows with a number spaces so that the character indexes align.static voidalignTabs(List<String> rows)Replaces every tab character (\t) in the specified rows with a number spaces so that the character indexes align.static Stringblock(String text)Indents the specified text, surrounds it with brackets and put the content on a separate line.static Stringblock(String row, String... rows)Indents the specified text, surrounds it with brackets and put the content on a separate line.static Stringblock(Stream<String> rows)Indents the specified text, surrounds it with brackets and put the content on a separate line.static StringclassToJavaFileName(String longName)static Stringdnl()Returns two new-line-characters as defined innl(String).static Optional<String>fileToClassName(String fileName)static <E,R>
Rifelse(Optional<E> condition, Function<E,R> trueMap, R falseValue)If the condition is present, the true-function will be called with its value and the result will be returned.static Stringindent(String text)Indents one level after each new-line-character as defined bynl().static Stringindent(String... text)Indents one level after each new-line-character as defined bynl().static Stringindent(String text, int steps)Indents a variable number of levels level after each new-line-character as defined bynl().static Stringindent(Stream<String> text)Indents one level after each new-line-character as defined bynl().static StringjavaNameFromExternal(String externalName)static Stringlcfirst(String input)Returns the specified text but with the first character lowercase.static StringnameFromExternal(String externalName)static Stringnl()Returns the new-line-character as defined innl(String).static voidnl(String nl)Sets which new-line-character to use.static Optional<String>packageName(String longName)Returns the 'package' part of a long name.static Stringrepeat(String str, int count)Repeats the specified substring count times.static StringreplaceIfIllegalJavaIdentifierCharacter(String word)static StringreplaceIfJavaUsedWord(String word)static Stringseparate(String separator, String... blocks)Returns a string consisting of the specified blocks concatenated and separated by the specified separator.static StringshortName(String longName)Returns the 'name' part of a long name.static StringstaticField(String externalName)Returns a static field name representation of the specified camel-cased string.static StringstripGenerics(String className)Removes the generics and array parts of a class name.static Stringtab()Returns the current tab character.static voidtab(String tab)Sets the tab character to use when generating code.static StringtoUnderscoreSeparated(String javaName)Turns the specified string into an underscore-separated string.static Stringucfirst(String input)Returns the specified text but with the first character uppercase.static StringunQuote(String s)Returns the string but with any leading and trailing quotation marks trimmed.static StringwithFirst(String input, Function<Character,String> callback)Does something with the first character in the specified String.
-
-
-
Method Detail
-
separate
public static String separate(String separator, String... blocks)
Returns a string consisting of the specified blocks concatenated and separated by the specified separator.- Parameters:
separator- The separator.blocks- All the blocks.- Returns:
- The concatenated string.
-
lcfirst
public static String lcfirst(String input)
Returns the specified text but with the first character lowercase.- Parameters:
input- The text.- Returns:
- The resulting text.
-
ucfirst
public static String ucfirst(String input)
Returns the specified text but with the first character uppercase.- Parameters:
input- The text.- Returns:
- The resulting text.
-
withFirst
public static String withFirst(String input, Function<Character,String> callback)
Does something with the first character in the specified String.- Parameters:
input- The String.callback- The something.- Returns:
- The new String.
-
repeat
public static String repeat(String str, int count)
Repeats the specified substring count times.- Parameters:
str- The string to repeat.count- The number of times to repeat it.- Returns:
- The new String.
-
block
public static String block(String text)
Indents the specified text, surrounds it with brackets and put the content on a separate line.- Parameters:
text- The text to surround.- Returns:
- The text with a '{\n' before and a '\n}' afterwards.
-
block
public static String block(String row, String... rows)
Indents the specified text, surrounds it with brackets and put the content on a separate line.- Parameters:
row- the first row of the text.rows- the rest of the rows.- Returns:
- the text with a '{\n' before and a '\n}' afterwards.
-
block
public static String block(Stream<String> rows)
Indents the specified text, surrounds it with brackets and put the content on a separate line.- Parameters:
rows- the rest of the rows.- Returns:
- the text with a '{\n' before and a '\n}' afterwards.
-
indent
public static String indent(String text)
Indents one level after each new-line-character as defined bynl().- Parameters:
text- the text to indent.- Returns:
- the indented text.
-
indent
public static String indent(String... text)
Indents one level after each new-line-character as defined bynl(). If multiple strings are specified, new-line characters will be added between them.- Parameters:
text- the text to indent.- Returns:
- the indented text.
-
indent
public static String indent(Stream<String> text)
Indents one level after each new-line-character as defined bynl(). f multiple strings are specified, new-line characters will be added between them.- Parameters:
text- the text to indent.- Returns:
- the indented text.
-
indent
public static String indent(String text, int steps)
Indents a variable number of levels level after each new-line-character as defined bynl().- Parameters:
text- the text to indentsteps- the number of steps to indent- Returns:
- the indented text
-
ifelse
public static <E,R> R ifelse(Optional<E> condition, Function<E,R> trueMap, R falseValue)
If the condition is present, the true-function will be called with its value and the result will be returned. Else the false-value will be returned.- Type Parameters:
E- The inner type of the condition.R- The result type.- Parameters:
condition- The condition to be evaluated for presence.trueMap- The function that will be called if present.falseValue- The value returned if the condition is not present.- Returns:
- The value.
-
nl
public static String nl()
Returns the new-line-character as defined innl(String).- Returns:
- The new-line character.
-
nl
public static void nl(String nl)
Sets which new-line-character to use. Should be set before any code generation happens for indentation to work as expected.- Parameters:
nl- The new character to use.
-
dnl
public static String dnl()
Returns two new-line-characters as defined innl(String).- Returns:
- The new-line character.
-
tab
public static String tab()
Returns the current tab character. This can be defined by usingtab(String).- Returns:
- The current tab character.
-
tab
public static void tab(String tab)
Sets the tab character to use when generating code. This should only be called before any indentation occurs to prevent errors.- Parameters:
tab- The new tab character.
-
shortName
public static String shortName(String longName)
Returns the 'name' part of a long name. This is everything after the last dot for non-parameterized types. For parameterized types the rule applies to the part proceeding the bracket enclosed parameters e.g.long name java.util.Map<String, java.util.Date>returnsMap<String, java.util.Date>.- Parameters:
longName- The long name.- Returns:
- The name part.
-
packageName
public static Optional<String> packageName(String longName)
Returns the 'package' part of a long name. This is everything before the last dot.- Parameters:
longName- The long name.- Returns:
- The package part.
-
stripGenerics
public static String stripGenerics(String className)
Removes the generics and array parts of a class name.- Parameters:
className- the full class name- Returns:
- class name with decorations stripped
-
alignTabs
public static String alignTabs(String rows)
Replaces every tab character (\t) in the specified rows with a number spaces so that the character indexes align. This is called recursively until all tab characters have been replaced. If there are no tabs in the text, the method has no effect.This implementation will begin by splitting the specified string into rows, then apply the alignment. Otherwise it is the same as
alignTabs(List).- Parameters:
rows- a single string with all the rows to align- Returns:
- the same string but with tabs replaced with aligning spaces
-
alignTabs
public static void alignTabs(List<String> rows)
Replaces every tab character (\t) in the specified rows with a number spaces so that the character indexes align. This is called recursively until all tab characters have been replaced. If there are no tabs in the text, the method has no effect.Example: If you call the method on the following code:
this.firstname\t = firstname;\t // Nullable this.lastname\t = lastname;\t // Nullable this.email\t = email;\t // Nullable this.age\t = age;You will get the following result:this.firstname = firstname; // Nullable this.lastname = lastname; // Nullable this.email = email; // Nullable this.age = age;- Parameters:
rows- list of rows to operate on
-
unQuote
public static String unQuote(String s)
Returns the string but with any leading and trailing quotation marks trimmed.- Parameters:
s- the string to unquote- Returns:
- the string without surrounding quotation marks
-
staticField
public static String staticField(String externalName)
Returns a static field name representation of the specified camel-cased string.- Parameters:
externalName- the string- Returns:
- the static field name representation
-
toUnderscoreSeparated
public static String toUnderscoreSeparated(String javaName)
Turns the specified string into an underscore-separated string.- Parameters:
javaName- the string to parse- Returns:
- as underscore separated
-
-