-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMap.java
More file actions
126 lines (110 loc) · 4.7 KB
/
AbstractMap.java
File metadata and controls
126 lines (110 loc) · 4.7 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
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
/*
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
*
* Developed for use with the book:
*
* Data Structures and Algorithms in Java, Sixth Edition
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
* John Wiley & Sons, 2014
*
* 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 <http://www.gnu.org/licenses/>.
*/
import java.util.Iterator;
/**
* An abstract base class to ease the implementation of the Map interface.
*
* The base class provides three means of support:
* 1) It provides an isEmpty implementation based upon the abstract size() method.
* 2) It defines a protected MapEntry class as a concrete implementation of the
* entry interface
* 3) It provides implemenations of the keySet and values methods, based upon use
* of a presumed implementation of the entrySet method.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
public abstract class AbstractMap<K,V> implements Map<K,V> {
/**
* Tests whether the map is empty.
* @return true if the map is empty, false otherwise
*/
@Override
public boolean isEmpty() { return size() == 0; }
//---------------- nested MapEntry class ----------------
/**
* A concrete implementation of the Entry interface to be used
* within a Map implementation.
*/
protected static class MapEntry<K,V> implements Entry<K,V> {
private K k; // key
private V v; // value
public MapEntry(K key, V value) {
k = key;
v = value;
}
// public methods of the Entry interface
public K getKey() { return k; }
public V getValue() { return v; }
// utilities not exposed as part of the Entry interface
protected void setKey(K key) { k = key; }
protected V setValue(V value) {
V old = v;
v = value;
return old;
}
/** Returns string representation (for debugging only) */
public String toString() { return "<" + k + ", " + v + ">"; }
} //----------- end of nested MapEntry class -----------
// Provides support for keySet() and values() methods, based upon
// the entrySet() method that must be provided by subclasses
//---------------- nested KeyIterator class ----------------
private class KeyIterator implements Iterator<K> {
private Iterator<Entry<K,V>> entries = entrySet().iterator(); // reuse entrySet
public boolean hasNext() { return entries.hasNext(); }
public K next() { return entries.next().getKey(); } // return key!
public void remove() { throw new UnsupportedOperationException("remove not supported"); }
} //----------- end of nested KeyIterator class -----------
//---------------- nested KeyIterable class ----------------
private class KeyIterable implements Iterable<K> {
public Iterator<K> iterator() { return new KeyIterator(); }
} //----------- end of nested KeyIterable class -----------
/**
* Returns an iterable collection of the keys contained in the map.
*
* @return iterable collection of the map's keys
*/
@Override
public Iterable<K> keySet() { return new KeyIterable(); }
//---------------- nested ValueIterator class ----------------
private class ValueIterator implements Iterator<V> {
private Iterator<Entry<K,V>> entries = entrySet().iterator(); // reuse entrySet
public boolean hasNext() { return entries.hasNext(); }
public V next() { return entries.next().getValue(); } // return value!
public void remove() { throw new UnsupportedOperationException("remove not supported"); }
} //----------- end of nested ValueIterator class -----------
//---------------- nested ValueIterable class ----------------
private class ValueIterable implements Iterable<V> {
public Iterator<V> iterator() { return new ValueIterator(); }
} //----------- end of nested ValueIterable class -----------
/**
* Returns an iterable collection of the values contained in the map.
* Note that the same value will be given multiple times in the result
* if it is associated with multiple keys.
*
* @return iterable collection of the map's values
*/
@Override
public Iterable<V> values() { return new ValueIterable(); }
}