600 } else {
601 return new Enumerator<>(type, false);
602 }
603 }
604
605 private <T> Iterator<T> getIterator(int type) {
606 if (count == 0) {
607 return Collections.emptyIterator();
608 } else {
609 return new Enumerator<>(type, true);
610 }
611 }
612
613 // Views
614
615 /**
616 * Each of these fields are initialized to contain an instance of the
617 * appropriate view the first time this view is requested. The views are
618 * stateless, so there's no reason to create more than one of each.
619 */
620 private transient volatile Set<K> keySet = null;
621 private transient volatile Set<Map.Entry<K,V>> entrySet = null;
622 private transient volatile Collection<V> values = null;
623
624 /**
625 * Returns a {@link Set} view of the keys contained in this map.
626 * The set is backed by the map, so changes to the map are
627 * reflected in the set, and vice-versa. If the map is modified
628 * while an iteration over the set is in progress (except through
629 * the iterator's own <tt>remove</tt> operation), the results of
630 * the iteration are undefined. The set supports element removal,
631 * which removes the corresponding mapping from the map, via the
632 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
633 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
634 * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
635 * operations.
636 *
637 * @since 1.2
638 */
639 public Set<K> keySet() {
640 if (keySet == null)
641 keySet = Collections.synchronizedSet(new KeySet(), this);
642 return keySet;
1283 public String toString() {
1284 return key.toString()+"="+value.toString();
1285 }
1286 }
1287
1288 // Types of Enumerations/Iterations
1289 private static final int KEYS = 0;
1290 private static final int VALUES = 1;
1291 private static final int ENTRIES = 2;
1292
1293 /**
1294 * A hashtable enumerator class. This class implements both the
1295 * Enumeration and Iterator interfaces, but individual instances
1296 * can be created with the Iterator methods disabled. This is necessary
1297 * to avoid unintentionally increasing the capabilities granted a user
1298 * by passing an Enumeration.
1299 */
1300 private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
1301 Entry<?,?>[] table = Hashtable.this.table;
1302 int index = table.length;
1303 Entry<?,?> entry = null;
1304 Entry<?,?> lastReturned = null;
1305 int type;
1306
1307 /**
1308 * Indicates whether this Enumerator is serving as an Iterator
1309 * or an Enumeration. (true -> Iterator).
1310 */
1311 boolean iterator;
1312
1313 /**
1314 * The modCount value that the iterator believes that the backing
1315 * Hashtable should have. If this expectation is violated, the iterator
1316 * has detected concurrent modification.
1317 */
1318 protected int expectedModCount = modCount;
1319
1320 Enumerator(int type, boolean iterator) {
1321 this.type = type;
1322 this.iterator = iterator;
1323 }
1324
|
600 } else {
601 return new Enumerator<>(type, false);
602 }
603 }
604
605 private <T> Iterator<T> getIterator(int type) {
606 if (count == 0) {
607 return Collections.emptyIterator();
608 } else {
609 return new Enumerator<>(type, true);
610 }
611 }
612
613 // Views
614
615 /**
616 * Each of these fields are initialized to contain an instance of the
617 * appropriate view the first time this view is requested. The views are
618 * stateless, so there's no reason to create more than one of each.
619 */
620 private transient volatile Set<K> keySet;
621 private transient volatile Set<Map.Entry<K,V>> entrySet;
622 private transient volatile Collection<V> values;
623
624 /**
625 * Returns a {@link Set} view of the keys contained in this map.
626 * The set is backed by the map, so changes to the map are
627 * reflected in the set, and vice-versa. If the map is modified
628 * while an iteration over the set is in progress (except through
629 * the iterator's own <tt>remove</tt> operation), the results of
630 * the iteration are undefined. The set supports element removal,
631 * which removes the corresponding mapping from the map, via the
632 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
633 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
634 * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
635 * operations.
636 *
637 * @since 1.2
638 */
639 public Set<K> keySet() {
640 if (keySet == null)
641 keySet = Collections.synchronizedSet(new KeySet(), this);
642 return keySet;
1283 public String toString() {
1284 return key.toString()+"="+value.toString();
1285 }
1286 }
1287
1288 // Types of Enumerations/Iterations
1289 private static final int KEYS = 0;
1290 private static final int VALUES = 1;
1291 private static final int ENTRIES = 2;
1292
1293 /**
1294 * A hashtable enumerator class. This class implements both the
1295 * Enumeration and Iterator interfaces, but individual instances
1296 * can be created with the Iterator methods disabled. This is necessary
1297 * to avoid unintentionally increasing the capabilities granted a user
1298 * by passing an Enumeration.
1299 */
1300 private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
1301 Entry<?,?>[] table = Hashtable.this.table;
1302 int index = table.length;
1303 Entry<?,?> entry;
1304 Entry<?,?> lastReturned;
1305 int type;
1306
1307 /**
1308 * Indicates whether this Enumerator is serving as an Iterator
1309 * or an Enumeration. (true -> Iterator).
1310 */
1311 boolean iterator;
1312
1313 /**
1314 * The modCount value that the iterator believes that the backing
1315 * Hashtable should have. If this expectation is violated, the iterator
1316 * has detected concurrent modification.
1317 */
1318 protected int expectedModCount = modCount;
1319
1320 Enumerator(int type, boolean iterator) {
1321 this.type = type;
1322 this.iterator = iterator;
1323 }
1324
|