summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata/Multimap.java
blob: e9c19bbb85bde58ca058047ec64cee769b1a73e5 (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
105
106
107
/* 
 * esodata - data structures and other things, of varying utility
 * Copyright 2022, Ben Culkin
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package bjc.esodata;

import java.util.*;

import bjc.data.Pair;

/**
 * A map that supports multiple values for a given key.
 * 
 * The thing that will tend to vary with the implementation is what they do with
 * duplicate values for the same key.
 * 
 * @author bjcul
 *
 * @param <KeyType> The key type of the map
 * @param <ValueType> The value type of the map.
 */
public interface Multimap<KeyType, ValueType> {
	/**
	 * Add a key-value mapping to the map.
	 *
	 * @param key   The key to store the value under.
	 *
	 * @param value The value to store.
	 */
	void add(KeyType key, ValueType value);

	/**
	 * Delete a particular key-value mapping from the map.
	 *
	 * @param key   The key of the mapping to remove.
	 *
	 * @param value The value of the mapping to remove.
	 */
	void remove(KeyType key, ValueType value);

	/**
	 * Delete all of the values associated with a particular key.
	 *
	 * @param key The key to remove values for.
	 */
	void remove(KeyType key);

	/**
	 * Get a set containing all of the values that are recorded for that key.
	 *
	 * @param key The key to look up values for.
	 *
	 * @return A set containing all of the values that have been mapped to that key.
	 */
	Set<ValueType> get(KeyType key);

	/**
	 * Get the single value in the map, if there is one.
	 * 
	 * @param key The key to look up
	 * @return An optional containing the key if it is there once, or empty if it is
	 *         there either no or more than one times
	 */
	Optional<ValueType> getSingle(KeyType key);

	/**
	 * Check if there is at least one value mapped to the given key.
	 *
	 * @param key The key to check for mappings for.
	 *
	 * @return Whether or not there is at least one value mapped to the key.
	 */
	boolean contains(KeyType key);

	/**
	 * Check if there is at least one instance of a particular key-value mapping.
	 *
	 * @param key   The key to check for mappings for.
	 *
	 * @param value The value to check for mappings for.
	 *
	 * @return Whether or not there is at least one instance of the given key-value
	 *         mapping.
	 */
	boolean contains(KeyType key, ValueType value);

	/**
	 * Retrieve an iterator over the values in this map
	 * 
	 * @return An iterator over the values in this map
	 */
	Iterator<Pair<KeyType, ValueType>> iterator();

}