-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDecorator.java
More file actions
99 lines (89 loc) · 2.62 KB
/
MyDecorator.java
File metadata and controls
99 lines (89 loc) · 2.62 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package graph;
import java.util.HashMap;
import java.util.Set;
import support.graph.CS16Decorator;
/**
*
* Your implementation of decorations. All methods must run in O(1) time.
*
* <p>
* This program makes heavy use of the decorator pattern. It is
* absolutely essential that you understand this design pattern before you start
* coding. In previous programs, like Heap, you were allowed to modify your
* Position classes when you wanted that class to store an additional piece of
* information. However, when you want to potentially store a lot of things in
* your classes, and especially when you do not know in advance what you will be
* storing, the decorator's usefulness is profound.
* </p>
*
* <p>
* Think of the decorator pattern like a post-it
* note. You write what you want on it, then stick it on the class. Later on,
* when you want to retrieve that information, you just retrieve the note, and
* read what it has to say. Please refer to the handout for more details on the
* decorator pattern and some examples on its usage.
* </p>
*
*
*/
public class MyDecorator<K, V> implements CS16Decorator<K, V> {
private HashMap<K, V> _myHashMap;
public MyDecorator() {
_myHashMap = new HashMap<K,V>();
}
/**
* Gets the decoration associated with the given key.
*
* @param key
* the key used to retrieve a specific value.
* @return the value associated with the key parameter
*/
@Override
public V getDecoration(K key) {
return _myHashMap.get(key);
}
/**
* Sets the decoration for the specified key to value.
*
* @param key
* the key that will be used to retrieve your value
* @param value
* the value associated with your key
*/
@Override
public void setDecoration(K key, V value) {
_myHashMap.put(key, value);
}
/**
* Returns true if there is a decoration for the given key, false otherwise.
*
* @param key
* a key that may or may not be in your decorator
* @return a boolean value that returns true if the key is valid
*/
@Override
public boolean hasDecoration(K key) {
return _myHashMap.containsKey(key);
}
/**
* Removes the decoration for the given key and returns the value associated
* with it.
*
* @param key
* key associated with a value
* @return value removed
*/
@Override
public V removeDecoration(K key) {
return _myHashMap.remove(key);
}
/**
* Returns a Set of all keys for this decoration.
*
* @return a Set of all keys in your decorator
*/
@Override
public Set<K> getKeys() {
return _myHashMap.keySet();
}
}