
de.larssh.utils.text.SentenceFormatter Maven / Gradle / Ivy
// Generated by delombok at Thu Aug 29 22:56:56 CEST 2019
package de.larssh.utils.text;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
* Class to format and parse a list of words.
*
*
* Constants define instances of common formatters. The following is an example
* using the words {@code abc}, {@code def} and {@code ghi}.
*
* Examples
*
* Formatter
* Example
*
*
* {@link #LOWER_CAMEL_CASE}
* abcDefGhi
*
*
* {@link #LOWER_KEBAB_CASE}
* abc-def-ghi
*
*
* {@link #LOWER_SNAKE_CASE}
* abc_def_ghi
*
*
* {@link #LOWER_WHITE_SPACE}
* Abc def ghi
*
*
* {@link #UPPER_CAMEL_CASE}
* AbcDefGhi
*
*
* {@link #UPPER_KEBAB_CASE}
* ABC-DEF-GHI
*
*
* {@link #UPPER_SNAKE_CASE}
* ABC_DEF_GHI
*
*
* {@link #UPPER_WHITE_SPACE}
* Abc Def Ghi
*
*
*/
public class SentenceFormatter {
/**
* Word separator for kebab space
*/
private static final String SEPARATOR_KEBAB_CASE = "-";
/**
* Word separator for snake space
*/
private static final String SEPARATOR_SNAKE_CASE = "_";
/**
* Word separator for white space
*/
private static final String SEPARATOR_WHITE_SPACE = " ";
/**
* Lower Camel Case formatter
*
*
* Example: abcDefGhi
*
*
* Words are translated to title case, except the first words, which is
* translated to lower case.
*/
public static final SentenceFormatter LOWER_CAMEL_CASE = new SentenceFormatter(Strings::toNeutralLowerCase, "", Strings::toNeutralTitleCase);
/**
* Lower Kebab Case formatter
*
*
* Example: abc-def-ghi
*
*
* Words are translated to lower case and are separated by minus character.
*/
public static final SentenceFormatter LOWER_KEBAB_CASE = new SentenceFormatter(Strings::toNeutralLowerCase, SEPARATOR_KEBAB_CASE, Strings::toNeutralLowerCase);
/**
* Lower Snake Case formatter
*
*
* Example: abc_def_ghi
*
*
* Words are translated to lower case and are separated by underscore character.
*/
public static final SentenceFormatter LOWER_SNAKE_CASE = new SentenceFormatter(Strings::toNeutralLowerCase, SEPARATOR_SNAKE_CASE, Strings::toNeutralLowerCase);
/**
* Lower White Space formatter
*
*
* Example: Abc def ghi
*
*
* Words are translated to lower case, except the first word, which is
* translated to title case, and are separated by white space character.
*/
public static final SentenceFormatter LOWER_WHITE_SPACE = new SentenceFormatter(Strings::toNeutralTitleCase, SEPARATOR_WHITE_SPACE, Strings::toNeutralLowerCase);
/**
* Upper Camel Case formatter
*
*
* Example: AbcDefGhi
*
*
* Words are translated to title case.
*/
public static final SentenceFormatter UPPER_CAMEL_CASE = new SentenceFormatter(Strings::toNeutralTitleCase, "", Strings::toNeutralTitleCase);
/**
* Upper Kebab Case formatter
*
*
* Example: ABC-DEF-GHI
*
*
* Words are translated to upper case and are separated by minus character.
*/
public static final SentenceFormatter UPPER_KEBAB_CASE = new SentenceFormatter(Strings::toNeutralUpperCase, SEPARATOR_KEBAB_CASE, Strings::toNeutralUpperCase);
/**
* Upper Snake Case formatter
*
*
* Example: ABC_DEF_GHI
*
*
* Words are translated to upper case and are separated by underscore character.
*/
public static final SentenceFormatter UPPER_SNAKE_CASE = new SentenceFormatter(Strings::toNeutralUpperCase, SEPARATOR_SNAKE_CASE, Strings::toNeutralUpperCase);
/**
* Upper White Space formatter
*
*
* Example: Abc Def Ghi
*
*
* Words are translated to title case and are separated by white space
* character.
*/
public static final SentenceFormatter UPPER_WHITE_SPACE = new SentenceFormatter(Strings::toNeutralTitleCase, SEPARATOR_WHITE_SPACE, Strings::toNeutralTitleCase);
/**
* Function to convert the first word
*/
private final Function convertFirstWord;
/**
* Separator character between words
*/
private final String separator;
/**
* Function to convert all words except the first word
*/
private final Function convertSubsequentWords;
/**
* Formats the list of given words using the specified converter functions and
* separator.
*
* @param words list of words
* @return formatted sentence
*/
public String format(final String... words) {
return format(asList(words));
}
/**
* Formats the list of given words using the specified converter functions and
* separator.
*
* @param words list of words
* @return formatted sentence
*/
public String format(final List words) {
if (words.isEmpty()) {
return "";
}
final StringBuilder builder = new StringBuilder();
builder.append(getConvertFirstWord().apply(words.get(0)));
final int size = words.size();
for (int index = 1; index < size; index += 1) {
builder.append(getSeparator());
builder.append(getConvertSubsequentWords().apply(words.get(index)));
}
return builder.toString();
}
/**
* Splits the given sentence into words using the separator. If the separator
* string is empty, the sentence is splitted by title characters. A leading
* title character does not generate an empty leading word.
*
* @param sentence the sentence to be splitted
* @return list of splitted words
*/
public List parse(final String sentence) {
if (sentence.isEmpty()) {
return emptyList();
}
if (getSeparator().isEmpty()) {
return splitByTitleCharacters(sentence);
}
return splitBySeparator(sentence);
}
/**
* Splits the given sentence into words using the separator string.
*
* @param sentence the sentence to be splitted
* @return list of splitted words
*/
private List splitBySeparator(final String sentence) {
final int sentenceLength = sentence.length();
final int separatorLength = getSeparator().length();
final int length = sentenceLength - separatorLength;
int beginIndex = 0;
final List words = new ArrayList<>();
for (int index = 0; index < length; index += 1) {
if (sentence.regionMatches(index, getSeparator(), 0, separatorLength)) {
words.add(sentence.substring(beginIndex, index));
beginIndex = index + separatorLength;
}
}
words.add(sentence.substring(beginIndex, sentenceLength));
return words;
}
/**
* Splits the given sentence into words by title characters. A leading title
* character does not generate an empty leading word.
*
* @param sentence the sentence to be splitted
* @return list of splitted words
*/
private List splitByTitleCharacters(final String sentence) {
final int length = sentence.length();
int beginIndex = 0;
final List words = new ArrayList<>();
for (int index = 1; index < length; index += 1) {
if (Character.isUpperCase(sentence.charAt(index))) {
words.add(sentence.substring(beginIndex, index));
beginIndex = index;
}
}
words.add(sentence.substring(beginIndex, length));
return words;
}
/**
* Function to convert the first word
*
* @return convert function for the first word of a sentence
*/
@java.lang.SuppressWarnings("all")
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
@lombok.Generated
public Function getConvertFirstWord() {
return this.convertFirstWord;
}
/**
* Separator character between words
*
* @return separator
*/
@java.lang.SuppressWarnings("all")
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
@lombok.Generated
public String getSeparator() {
return this.separator;
}
/**
* Function to convert all words except the first word
*
* @return convert function for subsequent words
*/
@java.lang.SuppressWarnings("all")
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
@lombok.Generated
public Function getConvertSubsequentWords() {
return this.convertSubsequentWords;
}
@java.lang.SuppressWarnings("all")
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
@lombok.Generated
public SentenceFormatter(final Function convertFirstWord, final String separator, final Function convertSubsequentWords) {
this.convertFirstWord = convertFirstWord;
this.separator = separator;
this.convertSubsequentWords = convertSubsequentWords;
}
}