Coverage Summary for Class: Utils (it.polimi.ingsw.Misc)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
Utils |
100%
(1/1)
|
85,7%
(6/7)
|
91,7%
(11/12)
|
93,3%
(28/30)
|
1 package it.polimi.ingsw.Misc;
2
3 import it.polimi.ingsw.Exceptions.Input.InvalidElementException;
4
5 import java.util.*;
6
7 /**
8 * This class contains various static functions useful for more than one purpose inside the code.
9 * Check the docs of each function for more info.
10 */
11 public class Utils {
12 /**
13 * Given a {@link List}, shuffles it in place with a random seed
14 *
15 * @param coll the list to shuffle
16 * @param <T> the type of the elements of the list
17 */
18 public static <T> void shuffle(List<T> coll) {
19 Collections.shuffle(coll, new Random(System.currentTimeMillis()));
20 }
21
22 /**
23 * Given a {@link List}, returns a random element from it
24 *
25 * @param list the list to fetch from
26 * @param <T> the type of the list's elements
27 * @return a random element from the list
28 */
29 public static <T> T random(List<T> list) {
30 Random random = new Random();
31 int randomNumber = random.nextInt(list.size());
32 return list.get(randomNumber);
33 }
34
35 /**
36 * Selects an element in the list as if it was a circular array
37 *
38 * @param startingElement the element from which to start counting forwards
39 * @param group the list to fetch from
40 * @param movement the amount of steps forward from the starting element to take in order to reach the fetched element
41 * @param <T> the type of elements of the group
42 * @return an element from the group
43 * @throws InvalidElementException if startingElement is not present in the list
44 */
45 public static <T> T modularSelection(T startingElement, List<T> group, int movement) throws InvalidElementException {
46 int index = group.indexOf(startingElement);
47 if (index == -1) {
48 throw new InvalidElementException("starting element");
49 }
50 return group.get((index + group.size() + movement) % group.size());
51 }
52
53 /**
54 * Given two maps, evaluate whether the second map contains values that can fit the first map
55 *
56 * @param originalSet the first map is the "original"
57 * @param possibleSubset the second map is a "possible subset" of the original
58 * @param <T> the key of both maps
59 * @param <C> an element extending the Integer class as the value of both maps
60 * @return true if the second map is a possible subset of the original, meaning the values for each key in the possible subset
61 * are lower than the values for the same key in the original map. otherwise returns false
62 */
63 public static <T, C extends Integer> boolean canMapFit(Map<T, C> originalSet, Map<T, C> possibleSubset) {
64 for (Map.Entry<T, C> entry : possibleSubset.entrySet()) {
65 T key = entry.getKey();
66 int count = entry.getValue();
67 if (!originalSet.containsKey(key) || ((int) originalSet.get(key) - count < 0)) {
68 return false;
69 }
70 }
71 return true;
72 }
73
74 /**
75 * Given a list, find occurrences of elements of the same class within it.
76 *
77 * @param selected the class object to search for in the list
78 * @param list the list to search in
79 * @param <T> the elements of the list
80 * @param <E> an inheritor of the class of elements contained in the list
81 * @return the count of how many elements of the list match the input class
82 */
83 public static <T, E extends T> int countSimilarClassOccurrences(Class<E> selected, List<T> list) {
84 return (int) list.stream().filter(pa -> pa.getClass() == selected).count();
85 }
86
87 /**
88 * Given a {@link List}, sorts its elements in order of frequency from highest to lowest
89 *
90 * @param array the list to sort
91 * @param <T> the type of the elements in the list
92 * @return a new {@link List}, containing the elements of the previous one sorted by frequency
93 */
94 public static <T> List<T> sortByFrequency(List<T> array) {
95 Map<T, ArrayList<T>> frequencyMap = new HashMap<>();
96 for (T current : array) {
97 ArrayList<T> list = frequencyMap.getOrDefault(current, new ArrayList<>());
98 list.add(current);
99 frequencyMap.put(current, list);
100 }
101 ArrayList<T> sorted = new ArrayList<>();
102 frequencyMap.entrySet().stream()
103 .sorted(Comparator.comparingInt(o -> o.getValue().size()))
104 .map(Map.Entry::getValue)
105 .forEach(sorted::addAll);
106 Collections.reverse(sorted);
107 return sorted;
108 }
109 }