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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.map;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.ResettableIterator;
import org.apache.commons.collections4.iterators.EmptyIterator;
import org.apache.commons.collections4.iterators.EmptyMapIterator;
/**
* A <code>Map</code> implementation that stores data in simple fields until
* the size is greater than 3.
* <p>
* This map is designed for performance and can outstrip HashMap.
* It also has good garbage collection characteristics.
* <ul>
* <li>Optimised for operation at size 3 or less.
* <li>Still works well once size 3 exceeded.
* <li>Gets at size 3 or less are about 0-10% faster than HashMap,
* <li>Puts at size 3 or less are over 4 times faster than HashMap.
* <li>Performance 5% slower than HashMap once size 3 exceeded once.
* </ul>
* The design uses two distinct modes of operation - flat and delegate.
* While the map is size 3 or less, operations map straight onto fields using
* switch statements. Once size 4 is reached, the map switches to delegate mode
* and only switches back when cleared. In delegate mode, all operations are
* forwarded straight to a HashMap resulting in the 5% performance loss.
* <p>
* The performance gains on puts are due to not needing to create a Map Entry
* object. This is a large saving not only in performance but in garbage collection.
* <p>
* Whilst in flat mode this map is also easy for the garbage collector to dispatch.
* This is because it contains no complex objects or arrays which slow the progress.
* <p>
* Do not use <code>Flat3Map</code> if the size is likely to grow beyond 3.
* <p>
* <strong>Note that Flat3Map is not synchronized and is not thread-safe.</strong>
* If you wish to use this map from multiple threads concurrently, you must use
* appropriate synchronization. The simplest approach is to wrap this map
* using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
* exceptions when accessed by concurrent threads without synchronization.
*
* @since 3.0
* @version $Id: Flat3Map.java 1477799 2013-04-30 19:56:11Z tn $
*/
public class Flat3Map<K, V> implements IterableMap<K, V>, Serializable, Cloneable {
/** Serialization version */
private static final long serialVersionUID = -6701087419741928296L;
/** The size of the map, used while in flat mode */
private transient int size;
/** Hash, used while in flat mode */
private transient int hash1;
/** Hash, used while in flat mode */
private transient int hash2;
/** Hash, used while in flat mode */
private transient int hash3;
/** Key, used while in flat mode */
private transient K key1;
/** Key, used while in flat mode */
private transient K key2;
/** Key, used while in flat mode */
private transient K key3;
/** Value, used while in flat mode */
private transient V value1;
/** Value, used while in flat mode */
private transient V value2;
/** Value, used while in flat mode */
private transient V value3;
/** Map, used while in delegate mode */
private transient AbstractHashedMap<K, V> delegateMap;
/**
* Constructor.
*/
public Flat3Map() {
super();
}
/**
* Constructor copying elements from another map.
*
* @param map the map to copy
* @throws NullPointerException if the map is null
*/
public Flat3Map(final Map<? extends K, ? extends V> map) {
super();
putAll(map);
}
//-----------------------------------------------------------------------
/**
* Gets the value mapped to the key specified.
*
* @param key the key
* @return the mapped value, null if no match
*/
public V get(final Object key) {
if (delegateMap != null) {
return delegateMap.get(key);
}
if (key == null) {
switch (size) {
// drop through
case 3:
if (key3 == null) {
return value3;
}
case 2:
if (key2 == null) {
return value2;
}
case 1:
if (key1 == null) {
return value1;
}
}
} else {
if (size > 0) {
final int hashCode = key.hashCode();
switch (size) {
// drop through
case 3:
if (hash3 == hashCode && key.equals(key3)) {
return value3;
}
case 2:
if (hash2 == hashCode && key.equals(key2)) {
return value2;
}
case 1:
if (hash1 == hashCode && key.equals(key1)) {
return value1;
}
}
}
}
return null;
}
/**
* Gets the size of the map.
*
* @return the size
*/
public int size() {
if (delegateMap != null) {
return delegateMap.size();
}
return size;
}
/**
* Checks whether the map is currently empty.
*
* @return true if the map is currently size zero
*/
public boolean isEmpty() {
return size() == 0;
}
//-----------------------------------------------------------------------
/**
* Checks whether the map contains the specified key.
*
* @param key the key to search for
* @return true if the map contains the key
*/
public boolean containsKey(final Object key) {
if (delegateMap != null) {
return delegateMap.containsKey(key);
}
if (key == null) {
switch (size) { // drop through
case 3:
if (key3 == null) {
return true;
}
case 2:
if (key2 == null) {
return true;
}
case 1:
if (key1 == null) {
return true;
}
}
} else {
if (size > 0) {
final int hashCode = key.hashCode();
switch (size) { // drop through
case 3:
if (hash3 == hashCode && key.equals(key3)) {
return true;
}
case 2:
if (hash2 == hashCode && key.equals(key2)) {
return true;
}
case 1:
if (hash1 == hashCode && key.equals(key1)) {
return true;
}
}
}
}
return false;
}
/**
* Checks whether the map contains the specified value.
*
* @param value the value to search for
* @return true if the map contains the key
*/
public boolean containsValue(final Object value) {
if (delegateMap != null) {
return delegateMap.containsValue(value);
}
if (value == null) { // drop through
switch (size) {
case 3:
if (value3 == null) {
return true;
}
case 2:
if (value2 == null) {
return true;
}
case 1:
if (value1 == null) {
return true;
}
}
} else {
switch (size) { // drop through
case 3:
if (value.equals(value3)) {
return true;
}
case 2:
if (value.equals(value2)) {
return true;
}
case 1:
if (value.equals(value1)) {
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------
/**
* Puts a key-value mapping into this map.
*
* @param key the key to add
* @param value the value to add
* @return the value previously mapped to this key, null if none
*/
public V put(final K key, final V value) {
if (delegateMap != null) {
return delegateMap.put(key, value);
}
// change existing mapping
if (key == null) {
switch (size) { // drop through
case 3:
if (key3 == null) {
final V old = value3;
value3 = value;
return old;
}
case 2:
if (key2 == null) {
final V old = value2;
value2 = value;
return old;
}
case 1:
if (key1 == null) {
final V old = value1;
value1 = value;
return old;
}
}
} else {
if (size > 0) {
final int hashCode = key.hashCode();
switch (size) { // drop through
case 3:
if (hash3 == hashCode && key.equals(key3)) {
final V old = value3;
value3 = value;
return old;
}
case 2:
if (hash2 == hashCode && key.equals(key2)) {
final V old = value2;
value2 = value;
return old;
}
case 1:
if (hash1 == hashCode && key.equals(key1)) {
final V old = value1;
value1 = value;
return old;
}
}
}
}
// add new mapping
switch (size) {
default:
convertToMap();
delegateMap.put(key, value);
return null;
case 2:
hash3 = key == null ? 0 : key.hashCode();
key3 = key;
value3 = value;
break;
case 1:
hash2 = key == null ? 0 : key.hashCode();
key2 = key;
value2 = value;
break;
case 0:
hash1 = key == null ? 0 : key.hashCode();
key1 = key;
value1 = value;
break;
}
size++;
return null;
}
/**
* Puts all the values from the specified map into this map.
*
* @param map the map to add
* @throws NullPointerException if the map is null
*/
public void putAll(final Map<? extends K, ? extends V> map) {
final int size = map.size();
if (size == 0) {
return;
}
if (delegateMap != null) {
delegateMap.putAll(map);
return;
}
if (size < 4) {
for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
} else {
convertToMap();
delegateMap.putAll(map);
}
}
/**
* Converts the flat map data to a map.
*/
private void convertToMap() {
delegateMap = createDelegateMap();
switch (size) { // drop through
case 3:
delegateMap.put(key3, value3);
case 2:
delegateMap.put(key2, value2);
case 1:
delegateMap.put(key1, value1);
case 0:
break;
default:
throw new IllegalStateException("Invalid map index: " + size);
}
size = 0;
hash1 = hash2 = hash3 = 0;
key1 = key2 = key3 = null;
value1 = value2 = value3 = null;
}
/**
* Create an instance of the map used for storage when in delegation mode.
* <p>
* This can be overridden by subclasses to provide a different map implementation.
* Not every AbstractHashedMap is suitable, identity and reference based maps
* would be poor choices.
*
* @return a new AbstractHashedMap or subclass
* @since 3.1
*/
protected AbstractHashedMap<K, V> createDelegateMap() {
return new HashedMap<K, V>();
}
/**
* Removes the specified mapping from this map.
*
* @param key the mapping to remove
* @return the value mapped to the removed key, null if key not in map
*/
public V remove(final Object key) {
if (delegateMap != null) {
return delegateMap.remove(key);
}
if (size == 0) {
return null;
}
if (key == null) {
switch (size) { // drop through
case 3:
if (key3 == null) {
final V old = value3;
hash3 = 0;
key3 = null;
value3 = null;
size = 2;
return old;
}
if (key2 == null) {
final V old = value2;
hash2 = hash3;
key2 = key3;
value2 = value3;
hash3 = 0;
key3 = null;
value3 = null;
size = 2;
return old;
}
if (key1 == null) {
final V old = value1;
hash1 = hash3;
key1 = key3;
value1 = value3;
hash3 = 0;
key3 = null;
value3 = null;
size = 2;
return old;
}
return null;
case 2:
if (key2 == null) {
final V old = value2;
hash2 = 0;
key2 = null;
value2 = null;
size = 1;
return old;
}
if (key1 == null) {
final V old = value1;
hash1 = hash2;
key1 = key2;
value1 = value2;
hash2 = 0;
key2 = null;
value2 = null;
size = 1;
return old;
}
return null;
case 1:
if (key1 == null) {
final V old = value1;
hash1 = 0;
key1 = null;
value1 = null;
size = 0;
return old;
}
}
} else {
if (size > 0) {
final int hashCode = key.hashCode();
switch (size) { // drop through
case 3:
if (hash3 == hashCode && key.equals(key3)) {
final V old = value3;
hash3 = 0;
key3 = null;
value3 = null;
size = 2;
return old;
}
if (hash2 == hashCode && key.equals(key2)) {
final V old = value2;
hash2 = hash3;
key2 = key3;
value2 = value3;
hash3 = 0;
key3 = null;
value3 = null;
size = 2;
return old;
}
if (hash1 == hashCode && key.equals(key1)) {
final V old = value1;
hash1 = hash3;
key1 = key3;
value1 = value3;
hash3 = 0;
key3 = null;
value3 = null;
size = 2;
return old;
}
return null;
case 2:
if (hash2 == hashCode && key.equals(key2)) {
final V old = value2;
hash2 = 0;
key2 = null;
value2 = null;
size = 1;
return old;
}
if (hash1 == hashCode && key.equals(key1)) {
final V old = value1;
hash1 = hash2;
key1 = key2;
value1 = value2;
hash2 = 0;
key2 = null;
value2 = null;
size = 1;
return old;
}
return null;
case 1:
if (hash1 == hashCode && key.equals(key1)) {
final V old = value1;
hash1 = 0;
key1 = null;
value1 = null;
size = 0;
return old;
}
}
}
}
return null;
}
/**
* Clears the map, resetting the size to zero and nullifying references
* to avoid garbage collection issues.
*/
public void clear() {
if (delegateMap != null) {
delegateMap.clear(); // should aid gc
delegateMap = null; // switch back to flat mode
} else {
size = 0;
hash1 = hash2 = hash3 = 0;
key1 = key2 = key3 = null;
value1 = value2 = value3 = null;
}
}
//-----------------------------------------------------------------------
/**
* Gets an iterator over the map.
* Changes made to the iterator affect this map.
* <p>
* A MapIterator returns the keys in the map. It also provides convenient
* methods to get the key and value, and set the value.
* It avoids the need to create an entrySet/keySet/values object.
* It also avoids creating the Map Entry object.
*
* @return the map iterator
*/
public MapIterator<K, V> mapIterator() {
if (delegateMap != null) {
return delegateMap.mapIterator();
}
if (size == 0) {
return EmptyMapIterator.<K, V>emptyMapIterator();
}
return new FlatMapIterator<K, V>(this);
}
/**
* FlatMapIterator
*/
static class FlatMapIterator<K, V> implements MapIterator<K, V>, ResettableIterator<K> {
private final Flat3Map<K, V> parent;
private int nextIndex = 0;
private boolean canRemove = false;
FlatMapIterator(final Flat3Map<K, V> parent) {
super();
this.parent = parent;
}
public boolean hasNext() {
return nextIndex < parent.size;
}
public K next() {
if (hasNext() == false) {
throw new NoSuchElementException(AbstractHashedMap.NO_NEXT_ENTRY);
}
canRemove = true;
nextIndex++;
return getKey();
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID);
}
parent.remove(getKey());
nextIndex--;
canRemove = false;
}
public K getKey() {
if (canRemove == false) {
throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID);
}
switch (nextIndex) {
case 3:
return parent.key3;
case 2:
return parent.key2;
case 1:
return parent.key1;
}
throw new IllegalStateException("Invalid map index: " + nextIndex);
}
public V getValue() {
if (canRemove == false) {
throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID);
}
switch (nextIndex) {
case 3:
return parent.value3;
case 2:
return parent.value2;
case 1:
return parent.value1;
}
throw new IllegalStateException("Invalid map index: " + nextIndex);
}
public V setValue(final V value) {
if (canRemove == false) {
throw new IllegalStateException(AbstractHashedMap.SETVALUE_INVALID);
}
final V old = getValue();
switch (nextIndex) {
case 3:
parent.value3 = value;
break;
case 2:
parent.value2 = value;
break;
case 1:
parent.value1 = value;
break;
default:
throw new IllegalStateException("Invalid map index: " + nextIndex);
}
return old;
}
public void reset() {
nextIndex = 0;
canRemove = false;
}
@Override
public String toString() {
if (canRemove) {
return "Iterator[" + getKey() + "=" + getValue() + "]";
}
return "Iterator[]";
}
}
/**
* Gets the entrySet view of the map.
* Changes made to the view affect this map.
* <p>
* NOTE: from 4.0, the returned Map Entry will be an independent object and will
* not change anymore as the iterator progresses. To avoid this additional object
* creation and simply iterate through the entries, use {@link #mapIterator()}.
*
* @return the entrySet view
*/
public Set<Map.Entry<K, V>> entrySet() {
if (delegateMap != null) {
return delegateMap.entrySet();
}
return new EntrySet<K, V>(this);
}
/**
* EntrySet
*/
static class EntrySet<K, V> extends AbstractSet<Map.Entry<K, V>> {
private final Flat3Map<K, V> parent;
EntrySet(final Flat3Map<K, V> parent) {
super();
this.parent = parent;
}
@Override
public int size() {
return parent.size();
}
@Override
public void clear() {
parent.clear();
}
@Override
public boolean remove(final Object obj) {
if (obj instanceof Map.Entry == false) {
return false;
}
final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
final Object key = entry.getKey();
final boolean result = parent.containsKey(key);
parent.remove(key);
return result;
}
@Override
public Iterator<Map.Entry<K, V>> iterator() {
if (parent.delegateMap != null) {
return parent.delegateMap.entrySet().iterator();
}
if (parent.size() == 0) {
return EmptyIterator.<Map.Entry<K, V>>emptyIterator();
}
return new EntrySetIterator<K, V>(parent);
}
}
static class FlatMapEntry<K, V> implements Map.Entry<K, V> {
private final Flat3Map<K, V> parent;
private final int index;
private volatile boolean removed;
public FlatMapEntry(final Flat3Map<K, V> parent, final int index) {
this.parent = parent;
this.index = index;
this.removed = false;
}
/**
* Used by the iterator that created this entry to indicate that
* {@link java.util.Iterator#remove()} has been called.
* <p>
* As a consequence, all subsequent call to {@link #getKey()},
* {@link #setValue(Object)} and {@link #getValue()} will fail.
*
* @param flag
*/
void setRemoved(final boolean flag) {
this.removed = flag;
}
public K getKey() {
if (removed) {
throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID);
}
switch (index) {
case 3:
return parent.key3;
case 2:
return parent.key2;
case 1:
return parent.key1;
}
throw new IllegalStateException("Invalid map index: " + index);
}
public V getValue() {
if (removed) {
throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID);
}
switch (index) {
case 3:
return parent.value3;
case 2:
return parent.value2;
case 1:
return parent.value1;
}
throw new IllegalStateException("Invalid map index: " + index);
}
public V setValue(final V value) {
if (removed) {
throw new IllegalStateException(AbstractHashedMap.SETVALUE_INVALID);
}
final V old = getValue();
switch (index) {
case 3:
parent.value3 = value;
break;
case 2:
parent.value2 = value;
break;
case 1:
parent.value1 = value;
break;
default:
throw new IllegalStateException("Invalid map index: " + index);
}
return old;
}
@Override
public boolean equals(final Object obj) {
if (removed) {
return false;
}
if (obj instanceof Map.Entry == false) {
return false;
}
final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
final Object key = getKey();
final Object value = getValue();
return (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
(value == null ? other.getValue() == null : value.equals(other.getValue()));
}
@Override
public int hashCode() {
if (removed) {
return 0;
}
final Object key = getKey();
final Object value = getValue();
return (key == null ? 0 : key.hashCode()) ^
(value == null ? 0 : value.hashCode());
}
@Override
public String toString() {
if (!removed) {
return getKey() + "=" + getValue();
}
return "";
}
}
static abstract class EntryIterator<K, V> {
private final Flat3Map<K, V> parent;
private int nextIndex = 0;
private FlatMapEntry<K, V> currentEntry = null;
/**
* Create a new Flat3Map.EntryIterator.
*/
public EntryIterator(final Flat3Map<K, V> parent) {
this.parent = parent;
}
public boolean hasNext() {
return nextIndex < parent.size;
}
public Map.Entry<K, V> nextEntry() {
if (!hasNext()) {
throw new NoSuchElementException(AbstractHashedMap.NO_NEXT_ENTRY);
}
currentEntry = new FlatMapEntry<K, V>(parent, ++nextIndex);
return currentEntry;
}
public void remove() {
if (currentEntry == null) {
throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID);
}
currentEntry.setRemoved(true);
parent.remove(currentEntry.getKey());
nextIndex--;
currentEntry = null;
}
}
/**
* EntrySetIterator and MapEntry
*/
static class EntrySetIterator<K, V> extends EntryIterator<K, V> implements Iterator<Map.Entry<K, V>> {
EntrySetIterator(final Flat3Map<K, V> parent) {
super(parent);
}
public Map.Entry<K, V> next() {
return nextEntry();
}
}
/**
* Gets the keySet view of the map.
* Changes made to the view affect this map.
* To simply iterate through the keys, use {@link #mapIterator()}.
*
* @return the keySet view
*/
public Set<K> keySet() {
if (delegateMap != null) {
return delegateMap.keySet();
}
return new KeySet<K>(this);
}
/**
* KeySet
*/
static class KeySet<K> extends AbstractSet<K> {
private final Flat3Map<K, ?> parent;
KeySet(final Flat3Map<K, ?> parent) {
super();
this.parent = parent;
}
@Override
public int size() {
return parent.size();
}
@Override
public void clear() {
parent.clear();
}
@Override
public boolean contains(final Object key) {
return parent.containsKey(key);
}
@Override
public boolean remove(final Object key) {
final boolean result = parent.containsKey(key);
parent.remove(key);
return result;
}
@Override
public Iterator<K> iterator() {
if (parent.delegateMap != null) {
return parent.delegateMap.keySet().iterator();
}
if (parent.size() == 0) {
return EmptyIterator.<K>emptyIterator();
}
return new KeySetIterator<K>(parent);
}
}
/**
* KeySetIterator
*/
static class KeySetIterator<K> extends EntryIterator<K, Object> implements Iterator<K>{
@SuppressWarnings("unchecked")
KeySetIterator(final Flat3Map<K, ?> parent) {
super((Flat3Map<K, Object>) parent);
}
public K next() {
return nextEntry().getKey();
}
}
/**
* Gets the values view of the map.
* Changes made to the view affect this map.
* To simply iterate through the values, use {@link #mapIterator()}.
*
* @return the values view
*/
public Collection<V> values() {
if (delegateMap != null) {
return delegateMap.values();
}
return new Values<V>(this);
}
/**
* Values
*/
static class Values<V> extends AbstractCollection<V> {
private final Flat3Map<?, V> parent;
Values(final Flat3Map<?, V> parent) {
super();
this.parent = parent;
}
@Override
public int size() {
return parent.size();
}
@Override
public void clear() {
parent.clear();
}
@Override
public boolean contains(final Object value) {
return parent.containsValue(value);
}
@Override
public Iterator<V> iterator() {
if (parent.delegateMap != null) {
return parent.delegateMap.values().iterator();
}
if (parent.size() == 0) {
return EmptyIterator.<V>emptyIterator();
}
return new ValuesIterator<V>(parent);
}
}
/**
* ValuesIterator
*/
static class ValuesIterator<V> extends EntryIterator<Object, V> implements Iterator<V> {
@SuppressWarnings("unchecked")
ValuesIterator(final Flat3Map<?, V> parent) {
super((Flat3Map<Object, V>) parent);
}
public V next() {
return nextEntry().getValue();
}
}
//-----------------------------------------------------------------------
/**
* Write the map out using a custom routine.
*/
private void writeObject(final ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(size());
for (final MapIterator<?, ?> it = mapIterator(); it.hasNext();) {
out.writeObject(it.next()); // key
out.writeObject(it.getValue()); // value
}
}
/**
* Read the map in using a custom routine.
*/
@SuppressWarnings("unchecked")
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
final int count = in.readInt();
if (count > 3) {
delegateMap = createDelegateMap();
}
for (int i = count; i > 0; i--) {
put((K) in.readObject(), (V) in.readObject());
}
}
//-----------------------------------------------------------------------
/**
* Clones the map without cloning the keys or values.
*
* @return a shallow clone
* @since 3.1
*/
@Override
@SuppressWarnings("unchecked")
public Flat3Map<K, V> clone() {
try {
final Flat3Map<K, V> cloned = (Flat3Map<K, V>) super.clone();
if (cloned.delegateMap != null) {
cloned.delegateMap = cloned.delegateMap.clone();
}
return cloned;
} catch (final CloneNotSupportedException ex) {
throw new InternalError();
}
}
/**
* Compares this map with another.
*
* @param obj the object to compare to
* @return true if equal
*/
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (delegateMap != null) {
return delegateMap.equals(obj);
}
if (obj instanceof Map == false) {
return false;
}
final Map<?, ?> other = (Map<?, ?>) obj;
if (size != other.size()) {
return false;
}
if (size > 0) {
Object otherValue = null;
switch (size) { // drop through
case 3:
if (other.containsKey(key3) == false) {
return false;
}
otherValue = other.get(key3);
if (value3 == null ? otherValue != null : !value3.equals(otherValue)) {
return false;
}
case 2:
if (other.containsKey(key2) == false) {
return false;
}
otherValue = other.get(key2);
if (value2 == null ? otherValue != null : !value2.equals(otherValue)) {
return false;
}
case 1:
if (other.containsKey(key1) == false) {
return false;
}
otherValue = other.get(key1);
if (value1 == null ? otherValue != null : !value1.equals(otherValue)) {
return false;
}
}
}
return true;
}
/**
* Gets the standard Map hashCode.
*
* @return the hash code defined in the Map interface
*/
@Override
public int hashCode() {
if (delegateMap != null) {
return delegateMap.hashCode();
}
int total = 0;
switch (size) { // drop through
case 3:
total += hash3 ^ (value3 == null ? 0 : value3.hashCode());
case 2:
total += hash2 ^ (value2 == null ? 0 : value2.hashCode());
case 1:
total += hash1 ^ (value1 == null ? 0 : value1.hashCode());
case 0:
break;
default:
throw new IllegalStateException("Invalid map index: " + size);
}
return total;
}
/**
* Gets the map as a String.
*
* @return a string version of the map
*/
@Override
public String toString() {
if (delegateMap != null) {
return delegateMap.toString();
}
if (size == 0) {
return "{}";
}
final StringBuilder buf = new StringBuilder(128);
buf.append('{');
switch (size) { // drop through
case 3:
buf.append(key3 == this ? "(this Map)" : key3);
buf.append('=');
buf.append(value3 == this ? "(this Map)" : value3);
buf.append(',');
case 2:
buf.append(key2 == this ? "(this Map)" : key2);
buf.append('=');
buf.append(value2 == this ? "(this Map)" : value2);
buf.append(',');
case 1:
buf.append(key1 == this ? "(this Map)" : key1);
buf.append('=');
buf.append(value1 == this ? "(this Map)" : value1);
break;
// case 0: has already been dealt with
default:
throw new IllegalStateException("Invalid map index: " + size);
}
buf.append('}');
return buf.toString();
}
}
|