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
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.foundation;
import java.util.Enumeration;
import java.util.Map;
/**
* A pure java implementation of NSMutableDictionary that implements Map for
* greater java interoperability.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 893 $
*/
public class NSMutableDictionary<K, V> extends NSDictionary<K, V> {
private static final long serialVersionUID = -150787274911739812L;
/**
* Default constructor produces an empty dictionary.
*/
public NSMutableDictionary() {
super();
}
/**
* Default constructor produces an empty dictionary.
*/
public NSMutableDictionary(int initialSize) {
super(initialSize);
}
/**
* Produces a dictionary that contains one key referencing one value.
*/
public NSMutableDictionary(K key, V value) {
super(key, value);
}
/**
* Produces a dictionary containing the specified keys and values. An
* IllegalArgumentException is thrown if the arrays are not of the same length.
*/
public NSMutableDictionary(K[] keys, V[] values) {
super(values, keys);
}
/**
* Produces a dictionary that is a copy of the specified map (or dictionary).
*/
public NSMutableDictionary(Map<K, V> aMap) {
super(aMap);
}
/**
* Removes the key-value pair for the specified key.
*/
public void removeObjectForKey(K aKey) {
remove(aKey);
}
/**
* Copies all mappings from the specified dictionary to this dictionary,
* replacing any mappings this map had for any keys in the specified map.
*/
public void addEntriesFromDictionary(Map<K, V> aMap) {
putAll(aMap);
}
/**
* Removes all mappings from this dictionary.
*/
public void removeAllObjects() {
clear();
}
/**
* Removes all keys in the specified array from this dictionary.
*/
public void removeObjectsForKeys(NSArray<K> anArray) {
Enumeration<K> enumeration = anArray.objectEnumerator();
while (enumeration.hasMoreElements()) {
removeObjectForKey(enumeration.nextElement());
}
}
/**
* Clears all mappings in this dictionary and then adds all entries in the
* specified dictionary.
*/
public void setDictionary(Map<K, V> aMap) {
removeAllObjects();
addEntriesFromDictionary(aMap);
}
/**
* Sets the value for the specified key. If the key currently exists to the
* dictionary, the old value is replaced with the specified value. An
* IllegalArgumentException is thrown if either the key or value is null.
*/
public void setObjectForKey(V aValue, K aKey) {
if ((aKey == null) || (aValue == null)) {
throw new IllegalArgumentException("Cannot use null objects with an NSMutableDictionary.");
}
put(aKey, aValue);
}
}
/*
* $Log$ Revision 1.2 2006/02/16 13:15:00 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.4 2005/05/11 15:21:53 cgruber Change enum to enumeration, since
* enum is now a keyword as of Java 5.0
*
* A few other comments in the code.
*
* Revision 1.3 2002/06/30 17:16:26 mpowers Added new constructor taking an int:
* thanks cgruber.
*
*
* Revision 1.2 2001/02/23 23:43:41 mpowers Removed ill-advised this.
*
* Revision 1.1.1.1 2000/12/21 15:47:34 mpowers Contributing wotonomy.
*
* Revision 1.3 2000/12/20 16:25:38 michael Added log to all files.
*
*
*/
|