summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java
blob: 9bd62bcd9c250dccf932c32599363990bc5586d8 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package bjc.utils.funcdata;

import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * Functional wrapper over map providing some useful things
 * 
 * @author ben
 * 
 * @param <K>
 *            The type of this map's keys
 * @param <V>
 *            The type of this map's values
 *
 */
public interface IFunctionalMap<K, V> {

	/**
	 * Add an entry to the map
	 * 
	 * @param key
	 *            The key to put the value under
	 * @param val
	 *            The value to add
	 * @return The previous value of the key in the map, or null if the key
	 *         wasn't in the map. However, note that it may also return
	 *         null if the key was set to null.
	 * 
	 * @throws UnsupportedOperationException
	 *             if the map implementation doesn't support modifying the
	 *             map
	 */
	V put(K key, V val);

	/**
	 * Get the value assigned to the given key
	 * 
	 * @param key
	 *            The key to look for a value under
	 * @return The value of the key
	 * 
	 * 
	 */
	V get(K key);

	/**
	 * Transform the values returned by this map.
	 * 
	 * NOTE: This transform is applied once for each lookup of a value, so
	 * the transform passed should be a proper function, or things will
	 * likely not work as expected.
	 * 
	 * @param <V2>
	 *            The new type of returned values
	 * @param transformer
	 *            The function to use to transform values
	 * @return The map where each value will be transformed after lookup
	 */
	<V2> IFunctionalMap<K, V2> mapValues(Function<V, V2> transformer);

	/**
	 * Check if this map contains the specified key
	 * 
	 * @param key
	 *            The key to check
	 * @return Whether or not the map contains the key
	 */
	boolean containsKey(K key);

	/**
	 * Get a list of all the keys in this map
	 * 
	 * @return A list of all the keys in this map
	 */
	IFunctionalList<K> keyList();

	/**
	 * Execute an action for each entry in the map
	 * 
	 * @param action
	 *            the action to execute for each entry in the map
	 */
	void forEach(BiConsumer<K, V> action);

	/**
	 * Remove the value bound to the key
	 * 
	 * @param key
	 *            The key to remove from the map
	 * @return The previous value for the key in the map, or null if the
	 *         key wasn't in the class. NOTE: Just because you recieved
	 *         null, doesn't mean the map wasn't changed. It may mean that
	 *         someone put a null value for that key into the map
	 */
	V remove(K key);

	/**
	 * Get the number of entries in this map
	 * 
	 * @return The number of entries in this map
	 */
	int getSize();
}