Class DescribableUtils

java.lang.Object
ubic.gemma.model.common.DescribableUtils

public class DescribableUtils extends Object
Utilities to manipulate Describable and collections thereof.
Author:
poirigui
  • Field Details

    • NAME_COMPARATOR

      public static final Comparator<String> NAME_COMPARATOR
      Comparator that sorts Describable objects by name, case-insensitively, with nulls last.
  • Constructor Details

    • DescribableUtils

      public DescribableUtils()
  • Method Details

    • equalsByName

      public static boolean equalsByName(Describable a, Describable b)
      Check if two Describable objects are considered equals by their names.
    • getNames

      public static Set<String> getNames(Collection<? extends Describable> describables)
      Create a set of names from a collection of Describable objects.

      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) and removeByName(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 supplied addFunc and removeFunc.
      toAdd - describables to add to the collection. The function returns the element that was actually added
      addFunc - 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 a null name.
      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 neither replaceExisting nor ignoreExisting is true. Note that null names are not considered for uniqueness.
    • addByName

      @Nullable public static <T extends Describable, S extends T> S addByName(Collection<T> describables, S element)
      Add a Describable to 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 all Describable objects 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) Describable from the collection by name.

      This is as close as it gets to Collection.remove(Object).

    • removeByName

      public static boolean removeByName(Collection<? extends Describable> describables, String name)
      Remove one (or more if any duplicates) Describable from the collection by name.

      The name comparison is case-insensitive.