All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.code.title.TitleFromIdentifierDeterminer Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.code.title;

import com.softicar.platform.common.code.java.WordFragment;
import com.softicar.platform.common.string.Trim;
import com.softicar.platform.common.string.plural.PluralDeterminer;
import com.softicar.platform.common.string.title.capitalizer.TitleCapitalizer;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Determines a title from a given identifier.
 * 

* The given identifier will be split into individual words, which will then be * capitalized using {@link TitleCapitalizer}. *

* The identifier can be given in camel case (BillOfMaterial), snake case * (bill_of_material) or as words separated by space (bill of * material) or dashes (bill-of-material). *

* Numbers will be treated as individual words, e.g. Foo1337 will be * treated as two words, Foo and 1337. * * @author Oliver Richers */ public class TitleFromIdentifierDeterminer { private final String identifier; private final Set prefixes; public TitleFromIdentifierDeterminer(String identifier) { this.identifier = identifier; this.prefixes = new TreeSet<>( Comparator// .comparing(String::length, Comparator.reverseOrder()) .thenComparing(Function.identity())); } public TitleFromIdentifierDeterminer addPrefixToDrop(String prefix) { prefixes.add(prefix); return this; } /** * Returns the capitalized title determined from the identifier. * * @return the title (never null) */ public String getTitle() { return new TitleCapitalizer(getWords()).capitalize(); } /** * Returns the capitalized plural title determined from the identifier. *

* Please note that the plural of identifiers with prepositions like * of or in will not work as might be expected, e.g. * BillOfMaterial will become Bill of Materials. * * @return the plural title (never null) */ public String getPluralTitle() { return new TitleCapitalizer(getPluralWords()).capitalize(); } private List getPluralWords() { List words = getWords(); int lastIndex = words.size() - 1; words.set(lastIndex, new PluralDeterminer(words.get(lastIndex)).getPlural()); return words; } private List getWords() { return WordFragment// .parse(getIdentifierWithoutPrefixes()) .stream() .map(WordFragment::getText) .map(String::toLowerCase) .collect(Collectors.toList()); } private String getIdentifierWithoutPrefixes() { String text = identifier; for (String prefix: prefixes) { text = Trim.trimPrefix(text, prefix); } return text; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy