-
-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathHeterogeneousMaxHolder.java
More file actions
84 lines (80 loc) · 3.27 KB
/
HeterogeneousMaxHolder.java
File metadata and controls
84 lines (80 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.bobocode.basics;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
/**
* {@link HeterogeneousMaxHolder} is a multi-type container that holds maximum values per each type. It's kind of a
* key/value map, where the key is a type and the value is the maximum among all values of this type that were put.
* <p>
* It's based on the {@link Map} and provides an API that allows to put a value by type, and get a max value by type.
* <p>
* <p>
* <strong>TODO: to get the most out of your learning, <a href="https://www.bobocode.com">visit our website</a></strong>
* <p>
*
* @author Taras Boychuk
*/
public class HeterogeneousMaxHolder {
private final Map<Class<?>, Object> map = new HashMap<>();
/**
* A method put stores a provided value by its type, if the value is greater than the current maximum. In other words, the logic
* of this method makes sure that only max value is stored and everything else is ignored.
* <p>
* If the current max value is less than a provided one, or if it's null, then a provided value gets stored and the old
* max is returned. Otherwise, nothing new is added, and the provided value is returned.
* <p>
* So technically, this method always stores the greater value and returns the smaller one.
*
* @param key a provided value type
* @param value a value to put
* @param <T> value type parameter
* @return a smaller value among the provided value and the current maximum
*/
// todo: implement a method according to javadoc
public <T extends Comparable<? super T>> T put(Class<T> key, T value) {
if (!map.containsKey(key)) {
map.put(key, value);
return null;
}
var storedValue = (T) map.get(key);
if (value.compareTo(storedValue) > 0) {
map.put(key, value);
return storedValue;
} else return value;
}
/**
* An overloaded method put implements the same logic using a custom comparator. A given comparator is wrapped with
* a null-safe comparator, considering null smaller than any non-null object.
* <p>
* All arguments must not be null.
*
* @param key a provided value type
* @param value a value to put
* @param comparator a custom comparator for the given type
* @param <T> value type parameter
* @return a smaller value among the provided value and the current maximum
*/
// todo: implement a method according to javadoc
public <T> T put(Class<T> key, T value, Comparator<? super T> comparator) {
if (!map.containsKey(key)) {
map.put(key, value);
return null;
}
var storedValue = (T) map.get(key);
if (comparator.compare(value, storedValue) > 0) {
map.put(key, value);
return storedValue;
} else return value;
}
/**
* A method getMax returns a max value by the given type. If no value is stored by this type, then it returns null.
*
* @param key a provided value type
* @param <T> value type parameter
* @return current max value or null
*/
// todo: implement a method according to javadoc
public <T> T getMax(Class<T> key) {
return (T) map.get(key);
}
}