Package de.xima.fc.common.text
Class UniqueNameGenerationHelper.NameGenerationOptions
- java.lang.Object
-
- de.xima.fc.common.text.UniqueNameGenerationHelper.NameGenerationOptions
-
- Enclosing class:
- UniqueNameGenerationHelper
public static final class UniqueNameGenerationHelper.NameGenerationOptions extends Object
Options for generating unique names. Allows to customize the name generation process, e.g. by setting a maximum length for the name, a validity check for the generated names, or a sanitizer for the names.- Since:
- 8.3.0
- Author:
- XIMA MEDIA GmbH
- See Also:
UniqueNameGenerationHelper.generateNewName(String, NameGenerationOptions)
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description UniqueNameGenerationHelper.NameGenerationOptions
copyWithIndex(String separator)
Sets an interpolator so that the generated names are likeMyEntity 4
.UniqueNameGenerationHelper.NameGenerationOptions
copyWithSpace(String copyName)
Sets an interpolator so that the generated names are likeMyEntity (Copy 4)
.UniqueNameGenerationHelper.NameGenerationOptions
copyWithSuffixAndIndex(String copySuffix, String separator)
Sets an interpolator so that the generated names are likeMyEntity_Copy_4
.static UniqueNameGenerationHelper.NameGenerationOptions
create()
Creates a new instance of the name generation options, with the default values.BiFunction<? super String,? super Integer,? extends String>
interpolator()
Gets the interpolator for generating candidate names.UniqueNameGenerationHelper.NameGenerationOptions
interpolator(BiFunction<? super String,? super Integer,? extends String> interpolator)
Sets the interpolator for generating candidate names.int
maxAttempts()
Gets the maximum number of attempts to make for generating a new name before giving up.UniqueNameGenerationHelper.NameGenerationOptions
maxAttempts(int maxAttempts)
Sets the maximum number of attempts to make for generating a new name before giving up.int
maxLength()
Gets the maximum length that is allowed for the name.UniqueNameGenerationHelper.NameGenerationOptions
maxLength(int maxLength)
Sets the maximum length that is allowed for the name.UniqueNameGenerationHelper.NameGenerationOptions
replace(String disallowedCharactersRegex, char replacementCharacter)
Sets a sanitizer so that, when generating the new name, replaces all matches for the given regex with the given replacement.Function<? super String,? extends String>
sanitizer()
Gets the sanitizer that takes a generated name and sanitizes it.UniqueNameGenerationHelper.NameGenerationOptions
sanitizer(Function<? super String,? extends String> sanitizer)
Sets the sanitizer that takes a generated name and sanitizes it.int
startIndex()
Gets the initial index used for generating new names.UniqueNameGenerationHelper.NameGenerationOptions
startIndex(int startIndex)
Sets the initial index used for generating new names.Predicate<? super String>
validityCheck()
Gets the validity check that tests whether a given generated name is allowed.UniqueNameGenerationHelper.NameGenerationOptions
validityCheck(Predicate<? super String> validityCheck)
Sets the validity check that tests whether a given generated name is allowed.
-
-
-
Method Detail
-
copyWithSuffixAndIndex
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions copyWithSuffixAndIndex(String copySuffix, String separator)
Sets an interpolator so that the generated names are likeMyEntity_Copy_4
. In general, given a suffix and separator, the name is generated from the base name and index as<BASE_NAME><SEPARATOR><SUFFIX><SEPARATOR><INDEX>
Replaces any interpolator previously set viainterpolator()
.- Parameters:
copySuffix
- Suffix added to the original name, e.g.Copy
.separator
- Separator between the base name, suffix and index.- Returns:
- These options for chaining method calls.
-
copyWithIndex
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions copyWithIndex(String separator)
Sets an interpolator so that the generated names are likeMyEntity 4
. In general, given a separator, the name is generated from the base name and index as<BASE_NAME><SEPARATOR><INDEX>
. Replaces any interpolator previously set viainterpolator()
.- Parameters:
separator
- Separator between the base name and index.- Returns:
- These options for chaining method calls.
-
copyWithSpace
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions copyWithSpace(String copyName)
Sets an interpolator so that the generated names are likeMyEntity (Copy 4)
. Replaces any interpolator previously set viainterpolator()
.- Parameters:
copyName
- Localized name for 'Copy'.- Returns:
- These options for chaining method calls.
-
interpolator
public BiFunction<? super String,? super Integer,? extends String> interpolator()
Gets the interpolator for generating candidate names. The interpolator is a function that receives the name and the current index and creates the derived name with the index, e.g.my_file (copy 2)
.Defaults to
(name, index) -> name + "_" + index
.- Returns:
- The interpolator to use.
-
interpolator
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions interpolator(BiFunction<? super String,? super Integer,? extends String> interpolator)
Sets the interpolator for generating candidate names. The interpolator is a function that receives the name and the current index and creates the derived name with the index, e.g.my_file (copy 2)
.Defaults to
(name, index) -> name + "_" + index
.- Parameters:
interpolator
- The interpolator to use.- Returns:
- These options for chaining method calls.
-
maxAttempts
public int maxAttempts()
Gets the maximum number of attempts to make for generating a new name before giving up. At most this number of candidate names will be tried.Defaults to 256.
- Returns:
- Maximum number of attempts.
-
maxAttempts
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions maxAttempts(int maxAttempts)
Sets the maximum number of attempts to make for generating a new name before giving up. At most this number of candidate names will be tried.Defaults to 256.
- Parameters:
maxAttempts
- Maximum number of attempts.- Returns:
- These options for chaining method calls.
-
maxLength
public int maxLength()
Gets the maximum length that is allowed for the name. The generated name is truncated to fit this length.Defaults to no maximum length.
- Returns:
- Maximum length that is allowed for the name.
-
maxLength
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions maxLength(int maxLength)
Sets the maximum length that is allowed for the name. The generated name is truncated to fit this length.Defaults to no maximum length.
- Parameters:
maxLength
- Maximum length that is allowed for the name.- Returns:
- These options for chaining method calls.
-
replace
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions replace(String disallowedCharactersRegex, char replacementCharacter)
Sets a sanitizer so that, when generating the new name, replaces all matches for the given regex with the given replacement. Replaces any sanitizer previously set viasanitizer()
. For example, you can use this options to replace whitespaces with underscores.- Parameters:
disallowedCharactersRegex
- Regex for substrings or characters that are not allowed in the name and should be replaced.replacementCharacter
- Character to replace the disallowed characters.- Returns:
- These options for chaining method calls.
-
sanitizer
public Function<? super String,? extends String> sanitizer()
Gets the sanitizer that takes a generated name and sanitizes it. For example, the sanitizer could replace characters that are not allowed for a name with a replacement character, e.g. whitespaces with underscores.Defaults to the
identity
operator, i.e. no sanitization.- Returns:
- The sanitizer to use.
-
sanitizer
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions sanitizer(Function<? super String,? extends String> sanitizer)
Sets the sanitizer that takes a generated name and sanitizes it. For example, the sanitizer could replace characters that are not allowed for a name with a replacement character, e.g. whitespaces with underscores.Defaults to the
identity
operator, i.e. no sanitization.- Parameters:
sanitizer
- The sanitizer to use.- Returns:
- This for chaining.
-
startIndex
public int startIndex()
Gets the initial index used for generating new names. Defaults to1
, so the first generated name ends onCopy_1
.- Returns:
- The initial index used for generating new names.
-
startIndex
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions startIndex(int startIndex)
Sets the initial index used for generating new names. Defaults to1
, so the first generated name ends onCopy_1
.- Parameters:
startIndex
- The initial index used for generating new names.- Returns:
- This for chaining.
-
validityCheck
public Predicate<? super String> validityCheck()
Gets the validity check that tests whether a given generated name is allowed. For example, the check could test whether an entity with that name exists already. It must returntrue
if the given new name is valid, andfalse
otherwise.- Returns:
- The predicate that checks whether a given generated name is allowed.
-
validityCheck
@CanIgnoreReturnValue public UniqueNameGenerationHelper.NameGenerationOptions validityCheck(Predicate<? super String> validityCheck)
Sets the validity check that tests whether a given generated name is allowed. For example, the check could test whether an entity with that name exists already. It must returntrue
if the given new name is valid, andfalse
otherwise.- Parameters:
validityCheck
- A predicate that checks whether a given generated name is allowed.- Returns:
- These options for chaining method calls.
-
create
public static UniqueNameGenerationHelper.NameGenerationOptions create()
Creates a new instance of the name generation options, with the default values.- Returns:
- New name generation options with default values.
-
-