Class DescribableUtils
Describable and collections thereof.- Author:
- poirigui
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Comparator<String> Comparator that sortsDescribableobjects by name, case-insensitively, with nulls last. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Describable,S extends T>
Collection<S> addAllByName(Collection<T> describables, Collection<S> toAdd) Add all the describables to a given collection.static <T extends Describable,S extends T>
Collection<S> addAllByName(Collection<T> describables, Collection<S> toAdd, boolean replaceExisting, boolean ignoreExisting) Add all the describables to a given collection.static <T extends Describable,S extends T>
Collection<S> addAllByName(Collection<T> describables, Collection<S> toAdd, BiFunction<Collection<T>, S, S> addFunc, BiFunction<Collection<T>, T, Boolean> removeFunc, boolean replaceExisting, boolean ignoreExisting) Add all the describables to a given collection.static <T extends Describable,S extends T>
SaddByName(Collection<T> describables, S element) Add aDescribableto a collection.static booleanCheck if twoDescribableobjects are considered equals by their names.getNames(Collection<? extends Describable> describables) Create a set of names from a collection ofDescribableobjects.static StringgetNextAvailableName(Collection<? extends Describable> describables, String name) Generate the next available name by appending a number to the given string.static <T extends Describable,S extends T>
booleanremoveAllByName(Collection<T> describables, Collection<S> toRemove) Remove allDescribableobjects from the collection that have the same name as any of those in the toRemove collection.static booleanremoveByName(Collection<? extends Describable> describables, String name) Remove one (or more if any duplicates)Describablefrom the collection by name.static <T extends Describable,S extends T>
booleanremoveByName(Collection<T> describables, S element) Remove one (or more if any duplicates)Describablefrom the collection by name.
-
Field Details
-
NAME_COMPARATOR
Comparator that sortsDescribableobjects by name, case-insensitively, with nulls last.
-
-
Constructor Details
-
DescribableUtils
public DescribableUtils()
-
-
Method Details
-
equalsByName
Check if twoDescribableobjects are considered equals by their names. -
getNames
Create a set of names from a collection ofDescribableobjects.The resulting set is case-insensitive.
-
getNextAvailableName
public static String getNextAvailableName(Collection<? extends Describable> describables, String name) Generate the next available name by appending a number to the given string.The format is "name", "name (2)", "name (3)", etc.
The name comparison is case-insensitive.
-
addAllByName
public static <T extends Describable,S extends T> Collection<S> addAllByName(Collection<T> describables, Collection<S> toAdd) Add all the describables to a given collection.Elements with the same name that are already present in the collection will be ignored.
This is as close as it gets to
Collection.addAll(Collection). -
addAllByName
public static <T extends Describable,S extends T> Collection<S> addAllByName(Collection<T> describables, Collection<S> toAdd, boolean replaceExisting, boolean ignoreExisting) Add all the describables to a given collection.This uses
addByName(Collection, Describable)andremoveByName(Collection, Describable)to add and remove elements from the collection.- See Also:
-
addAllByName
public static <T extends Describable,S extends T> Collection<S> addAllByName(Collection<T> describables, Collection<S> toAdd, BiFunction<Collection<T>, S, S> addFunc, BiFunction<Collection<T>, T, Boolean> removeFunc, boolean replaceExisting, boolean ignoreExisting) Add all the describables to a given collection.- Type Parameters:
T- type of describable.- Parameters:
describables- collection to which describables will be added. Note that the collection is only altered through the suppliedaddFuncandremoveFunc.toAdd- describables to add to the collection. The function returns the element that was actually addedaddFunc- function to add an element to the collection.removeFunc- function to remove an element from the collection by name. This is never called with a describable that has anullname.replaceExisting- if true, existing describables with the same name will be replaced.ignoreExisting- if true, existing describables with the same name will be ignored.- Returns:
- a collection of elements that were actually added to the collection.
- Throws:
NonUniqueDescribableByNameException- if the added describable have duplicated names or if a describable with the same name exists and neitherreplaceExistingnorignoreExistingis true. Note thatnullnames are not considered for uniqueness.
-
addByName
@Nullable public static <T extends Describable,S extends T> S addByName(Collection<T> describables, S element) Add aDescribableto a collection.Elements with the same name that are already present in the collection will be ignored.
This is as close as it gets to
Collection.add(Object).- Returns:
- the added element, or null if it was ignored.
-
removeAllByName
public static <T extends Describable,S extends T> boolean removeAllByName(Collection<T> describables, Collection<S> toRemove) Remove allDescribableobjects from the collection that have the same name as any of those in the toRemove collection.This has an O(n+m) complexity, unlike repeatedly calling
removeByName(Collection, Describable)which is O(n*m). -
removeByName
public static <T extends Describable,S extends T> boolean removeByName(Collection<T> describables, S element) Remove one (or more if any duplicates)Describablefrom the collection by name.This is as close as it gets to
Collection.remove(Object). -
removeByName
Remove one (or more if any duplicates)Describablefrom the collection by name.The name comparison is case-insensitive.
-