@Test public void replaceExactKeyYieldsSameMap() { ImmutableSortedMap<String, Integer> map = new ArraySortedMap<>(StringComparator); String key = "1"; Integer value = 1; map = map.insert(key, value); Assert.assertSame(map, map.insert(key, value)); }
@Override public ImmutableSortedMap<K, V> insert(K key, V value) { int pos = findKey(key); if (pos != -1) { if (this.keys[pos] == key && this.values[pos] == value) { return this; } else { K[] newKeys = replaceInArray(this.keys, pos, key); V[] newValues = replaceInArray(this.values, pos, value); return new ArraySortedMap<K, V>(this.comparator, newKeys, newValues); } } else { if (this.keys.length > Builder.ARRAY_TO_RB_TREE_SIZE_THRESHOLD) { @SuppressWarnings("unchecked") Map<K, V> map = new HashMap<K, V>(this.keys.length + 1); for (int i = 0; i < this.keys.length; i++) { map.put(this.keys[i], this.values[i]); } map.put(key, value); return RBTreeSortedMap.fromMap(map, this.comparator); } else { int newPos = findKeyOrInsertPosition(key); K[] keys = addToArray(this.keys, newPos, key); V[] values = addToArray(this.values, newPos, value); return new ArraySortedMap<K, V>(this.comparator, keys, values); } } }
ArraySortedMap extends ImmutableSortedMap<K, V> { @Override public ImmutableSortedMap<K, V> insert(K key, V value) { int pos = findKey(key); if (pos != -1) { if (this.keys[pos] == key && this.values[pos] == value) { return this; } else { K[] newKeys = replaceInArray(this.keys, pos, key); V[] newValues = replaceInArray(this.values, pos, value); return new ArraySortedMap<K, V>(this.comparator, newKeys, newValues); } } else { if (this.keys.length > Builder.ARRAY_TO_RB_TREE_SIZE_THRESHOLD) { @SuppressWarnings("unchecked") Map<K, V> map = new HashMap<K, V>(this.keys.length + 1); for (int i = 0; i < this.keys.length; i++) { map.put(this.keys[i], this.values[i]); } map.put(key, value); return RBTreeSortedMap.fromMap(map, this.comparator); } else { int newPos = findKeyOrInsertPosition(key); K[] keys = addToArray(this.keys, newPos, key); V[] values = addToArray(this.values, newPos, value); return new ArraySortedMap<K, V>(this.comparator, keys, values); } } } }
ArraySortedMap extends ImmutableSortedMap<K, V> { @Override public ImmutableSortedMap<K, V> insert(K key, V value) { int pos = findKey(key); if (pos != -1) { if (this.keys[pos] == key && this.values[pos] == value) { return this; } else { K[] newKeys = replaceInArray(this.keys, pos, key); V[] newValues = replaceInArray(this.values, pos, value); return new ArraySortedMap<K, V>(this.comparator, newKeys, newValues); } } else { if (this.keys.length > Builder.ARRAY_TO_RB_TREE_SIZE_THRESHOLD) { @SuppressWarnings("unchecked") Map<K, V> map = new HashMap<K, V>(this.keys.length + 1); for (int i = 0; i < this.keys.length; i++) { map.put(this.keys[i], this.values[i]); } map.put(key, value); return RBTreeSortedMap.fromMap(map, this.comparator); } else { int newPos = findKeyOrInsertPosition(key); K[] keys = addToArray(this.keys, newPos, key); V[] values = addToArray(this.values, newPos, value); return new ArraySortedMap<K, V>(this.comparator, keys, values); } } } @SuppressWarnings("unchecked")  ArraySortedMap(Comparator<K> comparator); @SuppressWarnings("unchecked") private  ArraySortedMap(Comparator<K> comparator, K[] keys, V[] values); }
ArraySortedMap extends ImmutableSortedMap<K, V> { @Override public ImmutableSortedMap<K, V> insert(K key, V value) { int pos = findKey(key); if (pos != -1) { if (this.keys[pos] == key && this.values[pos] == value) { return this; } else { K[] newKeys = replaceInArray(this.keys, pos, key); V[] newValues = replaceInArray(this.values, pos, value); return new ArraySortedMap<K, V>(this.comparator, newKeys, newValues); } } else { if (this.keys.length > Builder.ARRAY_TO_RB_TREE_SIZE_THRESHOLD) { @SuppressWarnings("unchecked") Map<K, V> map = new HashMap<K, V>(this.keys.length + 1); for (int i = 0; i < this.keys.length; i++) { map.put(this.keys[i], this.values[i]); } map.put(key, value); return RBTreeSortedMap.fromMap(map, this.comparator); } else { int newPos = findKeyOrInsertPosition(key); K[] keys = addToArray(this.keys, newPos, key); V[] values = addToArray(this.values, newPos, value); return new ArraySortedMap<K, V>(this.comparator, keys, values); } } } @SuppressWarnings("unchecked")  ArraySortedMap(Comparator<K> comparator); @SuppressWarnings("unchecked") private  ArraySortedMap(Comparator<K> comparator, K[] keys, V[] values); @SuppressWarnings("unchecked") static ArraySortedMap<A, C> buildFrom(
      List<A> keys,
      Map<B, C> values,
      Builder.KeyTranslator<A, B> translator,
      Comparator<A> comparator); static ArraySortedMap<K, V> fromMap(Map<K, V> map, Comparator<K> comparator); @Override boolean containsKey(K key); @Override V get(K key); @Override ImmutableSortedMap<K, V> remove(K key); @Override ImmutableSortedMap<K, V> insert(K key, V value); @Override K getMinKey(); @Override K getMaxKey(); @Override int size(); @Override boolean isEmpty(); @Override void inOrderTraversal(LLRBNode.NodeVisitor<K, V> visitor); @Override Iterator<Map.Entry<K, V>> iterator(); @Override Iterator<Map.Entry<K, V>> iteratorFrom(K key); @Override Iterator<Map.Entry<K, V>> reverseIteratorFrom(K key); @Override Iterator<Map.Entry<K, V>> reverseIterator(); @Override K getPredecessorKey(K key); @Override K getSuccessorKey(K key); @Override int indexOf(K key); @Override Comparator<K> getComparator(); }
ArraySortedMap extends ImmutableSortedMap<K, V> { @Override public ImmutableSortedMap<K, V> insert(K key, V value) { int pos = findKey(key); if (pos != -1) { if (this.keys[pos] == key && this.values[pos] == value) { return this; } else { K[] newKeys = replaceInArray(this.keys, pos, key); V[] newValues = replaceInArray(this.values, pos, value); return new ArraySortedMap<K, V>(this.comparator, newKeys, newValues); } } else { if (this.keys.length > Builder.ARRAY_TO_RB_TREE_SIZE_THRESHOLD) { @SuppressWarnings("unchecked") Map<K, V> map = new HashMap<K, V>(this.keys.length + 1); for (int i = 0; i < this.keys.length; i++) { map.put(this.keys[i], this.values[i]); } map.put(key, value); return RBTreeSortedMap.fromMap(map, this.comparator); } else { int newPos = findKeyOrInsertPosition(key); K[] keys = addToArray(this.keys, newPos, key); V[] values = addToArray(this.values, newPos, value); return new ArraySortedMap<K, V>(this.comparator, keys, values); } } } @SuppressWarnings("unchecked")  ArraySortedMap(Comparator<K> comparator); @SuppressWarnings("unchecked") private  ArraySortedMap(Comparator<K> comparator, K[] keys, V[] values); @SuppressWarnings("unchecked") static ArraySortedMap<A, C> buildFrom(
      List<A> keys,
      Map<B, C> values,
      Builder.KeyTranslator<A, B> translator,
      Comparator<A> comparator); static ArraySortedMap<K, V> fromMap(Map<K, V> map, Comparator<K> comparator); @Override boolean containsKey(K key); @Override V get(K key); @Override ImmutableSortedMap<K, V> remove(K key); @Override ImmutableSortedMap<K, V> insert(K key, V value); @Override K getMinKey(); @Override K getMaxKey(); @Override int size(); @Override boolean isEmpty(); @Override void inOrderTraversal(LLRBNode.NodeVisitor<K, V> visitor); @Override Iterator<Map.Entry<K, V>> iterator(); @Override Iterator<Map.Entry<K, V>> iteratorFrom(K key); @Override Iterator<Map.Entry<K, V>> reverseIteratorFrom(K key); @Override Iterator<Map.Entry<K, V>> reverseIterator(); @Override K getPredecessorKey(K key); @Override K getSuccessorKey(K key); @Override int indexOf(K key); @Override Comparator<K> getComparator();  }