-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapping.java
154 lines (136 loc) · 4.57 KB
/
Mapping.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cpsc331.collections;
import java.util.NoSuchElementException;
/**
*
* Provides an interface for a mapping whose keys are of type K
* and whose values are of type E.<br><br>
*
* Mapping Invariant: A finite collection S of pairs (k, v) of values
* where k has type K and v has type V, is maintained. For each
* element k of K, the collection includes at most one ordered pair
* whose first entry is k, so that a partial function from K to V
* is being represented.
* <br><br>.
*
* @author Wayne Eberly
*
*/
public interface Mapping<K, V> {
/**
*
* Returns the value associated with a given key, throwing a
* NoSuchElementException if no value is currently associated
* with the given key.<br><br>
*
* @param k the key whose value is to be returned
* @return the value of this key
* @throws NoSuchElementException if no value is defined for this key
* <br><br>
*
* Precondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* <li> A value k with type K has been provided as input. </li>
* </ol>
* Postcondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* <li> This Mapping has not been changed. </li>
* <li> If this Mapping includes some ordered pair whose first
* entry is the input key k, then the value v that is the
* second entry of this ordered pair (that is, the value
* associated with k) is returned as output.
* A NoSuchElementException is thrown otherwise. </li>
* </ol>
*
*/
public V get (K k) throws NoSuchElementException;
/**
*
* @param k the key to be searched for
* @return true if k is found; false otherwise
* <br><br>
*
* Reports whether a value for an input key k is presently
* defined.<br><br>
*
* Precondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* </ol>
* Postcondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* <li> This Mapping has not been changed. </li>
* <li> If this Mapping includes some ordered pair whose
* first entry is the input key k, then true is returned.
* Otherwise, false is returned. </li>
* </ol>
*
*/
public default boolean defined (K k) {
try {
this.get(k);
return true;
} catch (NoSuchElementException e) {
return false;
}
};
/**
*
* Sets the value associated with an input key to be an input
* value — replacing the value formerly associated with
* this key if one already exists.<br><br>
*
* @param k the key for which a value is to be defined
* @param v the value that is to be defined to this key
* <br><br>
*
* Precondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* <li> A value k with type K and v with type V are provided
* as input. </li>
* </ol>
* Postcondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* <li> If this Mapping included an ordered pair whose first entry
* is the input value k, then this is replaced by the
* ordered pair (k, v). Otherwise a new ordered pair
* (k, v) is added. No other changes to this Mapping
* are made. </li>
* </ol>
*
*/
public void set (K k, V v);
/**
*
* Removes the ordered pair with a given input key k, returning a
* NoSuchElementException and leaving the Mapping unchanged if no
* such ordered pair exists.<br><br>
*
* @param k the key for which a value is to be undefined
* @return the former value of this key
* @throws NoSuchElementException if no value was defined for this key
* <br><br>
*
* Precondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Dictionary Invariant is satisfied. </li>
* <li> A value k with type K is provided as input. </li>
* </ol>
* Postcondition:<br>
* <ol style="list-style-type: lower-alpha">
* <li> The Mapping Invariant is satisfied. </li>
* <li> If this Mapping includes an ordered pair (k, v) whose
* first entry is the input key k then this ordered pair is
* removed and the second entry v (that is, the value
* associated with this key) is returned as output; no other
* changes are made. Otherwise a NoSuchElementException
* is thrown and this Mapping is not changed at all. </li>
* </ol>
*
*/
public V remove (K k) throws NoSuchElementException;
}