Package it.polimi.ingsw.Misc
Class Utils
java.lang.Object
it.polimi.ingsw.Misc.Utils
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
C extends Integer>
booleanGiven two maps, evaluate whether the second map contains values that can fit the first mapstatic <T,
E extends T>
intcountSimilarClassOccurrences
(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 arraystatic <T> T
Given aList
, returns a random element from itstatic <T> void
Given aList
, shuffles it in place with a random seedstatic <T> List<T>
sortByFrequency
(List<T> array) Given aList
, sorts its elements in order of frequency from highest to lowest
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
shuffle
Given aList
, 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
Given aList
, 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 forwardsgroup
- the list to fetch frommovement
- 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 mapsC
- 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
Given a list, find occurrences of elements of the same class within it.- Type Parameters:
T
- the elements of the listE
- an inheritor of the class of elements contained in the list- Parameters:
selected
- the class object to search for in the listlist
- the list to search in- Returns:
- the count of how many elements of the list match the input class
-
sortByFrequency
Given aList
, 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
-