Class Util

java.lang.Object
dev.roanh.gmark.util.Util

public final class Util extends Object
Class providing various small utilities as well as thread bound random operations.
  • Field Details

    • random

      private static final ThreadLocal<Random> random
      Random instances for each thread.
  • Constructor Details

    • Util

      private Util()
      Prevent instantiation.
  • Method Details

    • getRandom

      public static Random getRandom()
      Gets a random instance bound to the current thread.
      Returns:
      A thread local random instance.
    • setRandomSeed

      public static void setRandomSeed(long seed)
      Sets the seed of the random instance for the current thread.
      Parameters:
      seed - The new seed.
      See Also:
    • selectRandom

      public static <T> T selectRandom(List<T> data)
      Selects an element at random from the given list.
      Type Parameters:
      T - The element data type.
      Parameters:
      data - The list to pick an element from.
      Returns:
      The selected element or null when the provided list was empty.
      See Also:
    • selectRandom

      public static <T> T selectRandom(Collection<T> data)
      Randomly selects an element from the given collection.
      Type Parameters:
      T - The element data type.
      Parameters:
      data - The collection to pick an element from.
      Returns:
      The selected element or null when the provided collection was empty.
      See Also:
    • uniformRandom

      public static int uniformRandom(int min, int max)
      Generates a random integer between the given minimum and maximum value (both inclusive).
      Parameters:
      min - The minimum value.
      max - The maximum value.
      Returns:
      The randomly generated value.
    • selectivityMapSupplier

      public static <T> Supplier<Map<SelectivityClass,T>> selectivityMapSupplier()
      Returns a Supplier that constructs a new enum map from the SelectivityClass enum to the given data type.
      Type Parameters:
      T - The data type to map to.
      Returns:
      A supplier that returns a map that maps from selectivity classes to the given data type.
      See Also:
    • applyOrNull

      public static <T, R> R applyOrNull(T data, Function<T,R> function)
      Applies the given function to the given data, unless the data is null then null is returned instead.
      Type Parameters:
      T - The input data type.
      R - The output data type.
      Parameters:
      data - The data to give to the function.
      function - The function to run on the data.
      Returns:
      The result of applying the given function to the given data or null when the given data is null.
    • isEmpty

      public static boolean isEmpty(Path folder) throws IOException
      Checks if the given folder is empty or not.
      Parameters:
      folder - The folder to check.
      Returns:
      True if the given folder does not contain any files or folders.
      Throws:
      IOException - When an IOException occurs.
    • triplesToSQL

      public static void triplesToSQL(Path triples, Path sql, boolean overwrite) throws IOException, FileNotFoundException, FileAlreadyExistsException
      Takes a file containing a graph in RDF triple format and outputs a SQL INSERT statement to populate a database with the graph.
      Parameters:
      triples - The input file with the RDF triple graph.
      sql - The file to write the generated SQL statement to.
      overwrite - True if the given output file should be overwritten if it already exists.
      Throws:
      IOException - When an IOException occurs.
      FileNotFoundException - When the given graph file was not found.
      FileAlreadyExistsException - When the given output file already exists and overwriting is not requested.
    • edgeLabelsToNodes

      public static <V, E> UniqueGraph<Object,Void> edgeLabelsToNodes(UniqueGraph<V,E> in)
      Converts the given edge labelled input graph to a graph where all edge labels have been turned into labelled vertices. Essentially, for each edge (a) --b--> (c) the edge will be turned into two edges with a new node with the former edge label in the middle, giving (a) --> (b) --> (c) . Thus this transform doubles the number of edges in the graph and adds as many new nodes as there used to be edges in the old graph. The returned graph has Object as the vertex data type. There are two options for the actual class of these vertex data objects. Either they are a vertex data object from the old graph and thus of generic type V. Or they are a DataProxy instance wrapping an old edge label of generic type E.
      Type Parameters:
      V - The vertex data type.
      E - The edge label data type.
      Parameters:
      in - The input graph to transform.
      Returns:
      The transformed graph without edge labels.
      See Also:
    • generateLabels

      public static List<Predicate> generateLabels(int n)
      Generates a list of the given size n with sequentially numbered predicate objects. This means each predicate will have a textual name that matches their numerical ID.
      Parameters:
      n - The number of labels to generate.
      Returns:
      The generated set of labels.
    • runIfNotNull

      public static <T> void runIfNotNull(T data, Consumer<T> fun)
      Runs the give consumer on the given data only if the given data is not equal to null.
      Type Parameters:
      T - The data type.
      Parameters:
      data - The data to run the consumer on.
      fun - The consumer to pass the given data to.
    • asArrayList

      @SafeVarargs public static <T> ArrayList<T> asArrayList(T... items)
      Creates a new array list populated with the given items.
      Type Parameters:
      T - The type of the data.
      Parameters:
      items - The items to put in the list.
      Returns:
      The newly created and populated array list.
      See Also:
    • computeTreeDecompositionWidth2

      public static <T extends IDable> Tree<List<T>> computeTreeDecompositionWidth2(SimpleGraph<T,List<Tree<List<T>>>> graph) throws IllegalArgumentException
      Computes the tree decomposition of the given input graph assuming that the input graph has the following properties:
      1. The input graph has a tree width of at most 2.
      2. The input graph is connected.
      3. The input graph contains no self loops.
      4. The input graph contains no parallel edges.
      Type Parameters:
      T - The data type of the graph.
      Parameters:
      graph - The graph to compute the tree decomposition of. This graph instance will be modified so a copy should be passed if this is a problem.
      Returns:
      The computed tree decomposition, not necessarily a nice tree decomposition.
      Throws:
      IllegalArgumentException - When the input graph is not connected or has a treewidth that is larger than 2.
    • findMaximalMatching

      public static <T extends IDable, M> List<SimpleGraph.SimpleEdge<T,M>> findMaximalMatching(SimpleGraph<T,M> graph)
      Finds a maximal (not maximum) matching in the given graph.
      Type Parameters:
      T - The graph data type.
      M - The metadata data type.
      Parameters:
      graph - The graph to find a maximal matching for.
      Returns:
      The edges of the found maximal matching.
    • cartesianProduct

      public static <T> List<List<T>> cartesianProduct(List<List<T>> sets) throws ArithmeticException
      Computes the Cartesian product of the given input sets. For example, for the following input sets:
       {
         {A, B},
         {C, D},
         {E}
       }
       
      The output is as follows:
       {
         {A, C, E},
         {A, D, E},
         {B, C, E},
         {B, D, E}
       }
       
      Type Parameters:
      T - The type of the data in the lists.
      Parameters:
      sets - The sets to compute the Cartesian product of.
      Returns:
      The computed Cartesian product of the input sets.
      Throws:
      ArithmeticException - When the size of the output list with sets would be more than Integer.MAX_VALUE.
    • computeAllSubsets

      public static <T> void computeAllSubsets(List<T> items, Consumer<List<T>> consumer)
      Computes all unordered subsets of the given list of items. Computed sets are passed to the given consumer and should be treated as ephemeral. They only exist for the during of the consumer call and should be copied if persistence is required. Note that unordered subsets are computed, this means that the output will not contain identical sets with a different order of items. In addition, while technically a valid subset, the empty set is never returned from this subroutine. Note that the input list itself will be one of the returned subsets. As an implementation note, it is allowed to add elements to the passed list of items in the consumer, though these items will not be considered for any subsets.
      Type Parameters:
      T - The list data type.
      Parameters:
      items - The items to compute subsets of.
      consumer - The consumer computed subsets are passed to.
    • computeAllSubsets

      private static <T> void computeAllSubsets(List<T> items, int offset, int max, List<T> set, Consumer<List<T>> consumer)
      Computes all unordered subsets of the given list of items. Computed sets are passed to the given consumer and should be treated as ephemeral. They only exist for the during of the consumer call and should be copied if persistence is required. Note that unordered subsets are computed, this means that the output will not contain identical sets with a different order of items. In addition, while technically a valid subset, the empty set is never returned from this subroutine. Note that the input list itself will be one of the returned subsets. As an implementation note, it is allowed to add elements to the passed list of items in the consumer, though these items will not be considered for any subsets.
      Type Parameters:
      T - The list data type.
      Parameters:
      items - The items to compute subsets of.
      offset - The current item to consider of inclusion in the output.
      max - The maximum index in the array to consider for any subset.
      set - The current output working set.
      consumer - The consumer computed subsets are passed to.