Class Utils

java.lang.Object
it.polimi.ingsw.Misc.Utils

public class Utils extends Object
This class contains various static functions useful for more than one purpose inside the code. Check the docs of each function for more info.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, C extends Integer>
    boolean
    canMapFit(Map<T,C> originalSet, Map<T,C> possibleSubset)
    Given two maps, evaluate whether the second map contains values that can fit the first map
    static <T, E extends T>
    int
    countSimilarClassOccurrences(Class<E> selected, List<T> list)
    Given a list, find occurrences of elements of the same class within it.
    static <T> T
    modularSelection(T startingElement, List<T> group, int movement)
    Selects an element in the list as if it was a circular array
    static <T> T
    random(List<T> list)
    Given a List, returns a random element from it
    static <T> void
    shuffle(List<T> coll)
    Given a List, shuffles it in place with a random seed
    static <T> List<T>
    Given a List, sorts its elements in order of frequency from highest to lowest

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • shuffle

      public static <T> void shuffle(List<T> coll)
      Given a List, shuffles it in place with a random seed
      Type Parameters:
      T - the type of the elements of the list
      Parameters:
      coll - the list to shuffle
    • random

      public static <T> T random(List<T> list)
      Given a List, returns a random element from it
      Type Parameters:
      T - the type of the list's elements
      Parameters:
      list - the list to fetch from
      Returns:
      a random element from the list
    • modularSelection

      public static <T> T modularSelection(T startingElement, List<T> group, int movement) throws InvalidElementException
      Selects an element in the list as if it was a circular array
      Type Parameters:
      T - the type of elements of the group
      Parameters:
      startingElement - the element from which to start counting forwards
      group - the list to fetch from
      movement - the amount of steps forward from the starting element to take in order to reach the fetched element
      Returns:
      an element from the group
      Throws:
      InvalidElementException - if startingElement is not present in the list
    • canMapFit

      public static <T, C extends Integer> boolean canMapFit(Map<T,C> originalSet, Map<T,C> possibleSubset)
      Given two maps, evaluate whether the second map contains values that can fit the first map
      Type Parameters:
      T - the key of both maps
      C - an element extending the Integer class as the value of both maps
      Parameters:
      originalSet - the first map is the "original"
      possibleSubset - the second map is a "possible subset" of the original
      Returns:
      true if the second map is a possible subset of the original, meaning the values for each key in the possible subset are lower than the values for the same key in the original map. otherwise returns false
    • countSimilarClassOccurrences

      public static <T, E extends T> int countSimilarClassOccurrences(Class<E> selected, List<T> list)
      Given a list, find occurrences of elements of the same class within it.
      Type Parameters:
      T - the elements of the list
      E - an inheritor of the class of elements contained in the list
      Parameters:
      selected - the class object to search for in the list
      list - the list to search in
      Returns:
      the count of how many elements of the list match the input class
    • sortByFrequency

      public static <T> List<T> sortByFrequency(List<T> array)
      Given a List, sorts its elements in order of frequency from highest to lowest
      Type Parameters:
      T - the type of the elements in the list
      Parameters:
      array - the list to sort
      Returns:
      a new List, containing the elements of the previous one sorted by frequency