Skip to content

Commit

Permalink
Merge pull request #1420 from paplorinc/treeMap
Browse files Browse the repository at this point in the history
Fix TreeMap creation from Java Map
  • Loading branch information
danieldietrich authored Jul 9, 2016
2 parents 275fade + 8337cc8 commit 78931f6
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 127 deletions.
48 changes: 26 additions & 22 deletions javaslang/src/main/java/javaslang/collection/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
*/
package javaslang.collection;

import javaslang.*;
import javaslang.Kind2;
import javaslang.Tuple;
import javaslang.Tuple2;
import javaslang.control.Option;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.*;
import java.util.stream.Collector;

Expand All @@ -35,29 +40,29 @@ private TreeMap(RedBlackTree<Tuple2<K, V>> entries) {
}

/**
* Returns a {@link java.util.stream.Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a
* {@link javaslang.collection.TreeMap}.
* Returns a {@link Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(Collector)} to obtain a
* {@link TreeMap}.
* <p>
* The natural comparator is used to compare TreeMap keys.
*
* @param <K> The key type
* @param <V> The value type
* @return A {@link javaslang.collection.TreeMap} Collector.
* @return A {@link TreeMap} Collector.
*/
public static <K extends Comparable<? super K>, V> Collector<Tuple2<K, V>, ArrayList<Tuple2<K, V>>, TreeMap<K, V>> collector() {
return collector((Comparator<? super K> & Serializable) K::compareTo);
}

/**
* Returns a {@link java.util.stream.Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a
* {@link javaslang.collection.TreeMap}.
* Returns a {@link Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(Collector)} to obtain a
* {@link TreeMap}.
*
* @param <K> The key type
* @param <V> The value type
* @param keyComparator A key comparator
* @return A {@link javaslang.collection.TreeMap} Collector.
* @return A {@link TreeMap} Collector.
*/
public static <K, V> Collector<Tuple2<K, V>, ArrayList<Tuple2<K, V>>, TreeMap<K, V>> collector(Comparator<? super K> keyComparator) {
Objects.requireNonNull(keyComparator, "keyComparator is null");
Expand All @@ -67,7 +72,7 @@ public static <K, V> Collector<Tuple2<K, V>, ArrayList<Tuple2<K, V>>, TreeMap<K,
left.addAll(right);
return left;
};
final Function<ArrayList<Tuple2<K, V>>, TreeMap<K, V>> finisher = list -> TreeMap.ofEntries(keyComparator, list);
final Function<ArrayList<Tuple2<K, V>>, TreeMap<K, V>> finisher = list -> ofEntries(keyComparator, list);
return Collector.of(supplier, accumulator, combiner, finisher);
}

Expand Down Expand Up @@ -162,7 +167,7 @@ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, Objec
*/
public static <K extends Comparable<? super K>, V> TreeMap<K, V> ofAll(java.util.Map<? extends K, ? extends V> map) {
Objects.requireNonNull(map, "map is null");
RedBlackTree<Tuple2<K, V>> result = RedBlackTree.empty();
RedBlackTree<Tuple2<K, V>> result = RedBlackTree.empty(new EntryComparator<>((Comparator<? super K> & Serializable) K::compareTo));
for (java.util.Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
result = result.insert(Tuple.of(entry.getKey(), entry.getValue()));
}
Expand Down Expand Up @@ -328,7 +333,7 @@ public static <K, V> TreeMap<K, V> ofEntries(Comparator<? super K> keyComparator
for (Tuple2<? extends K, ? extends V> entry : entries) {
tree = tree.insert((Tuple2<K, V>) entry);
}
return tree.isEmpty() ? TreeMap.empty(keyComparator) : new TreeMap<>(tree);
return tree.isEmpty() ? empty(keyComparator) : new TreeMap<>(tree);
}

/**
Expand All @@ -348,7 +353,7 @@ public static <K, V> TreeMap<K, V> ofEntries(Comparator<? super K> keyComparator
for (java.util.Map.Entry<? extends K, ? extends V> entry : entries) {
tree = tree.insert(Tuple.of(entry.getKey(), entry.getValue()));
}
return tree.isEmpty() ? TreeMap.empty(keyComparator) : new TreeMap<>(tree);
return tree.isEmpty() ? empty(keyComparator) : new TreeMap<>(tree);
}

/**
Expand All @@ -359,8 +364,7 @@ public static <K, V> TreeMap<K, V> ofEntries(Comparator<? super K> keyComparator
* @param entries Map entries
* @return A new TreeMap containing the given entries.
*/
public static <K extends Comparable<? super K>, V> TreeMap<K, V> ofEntries(
Iterable<? extends Tuple2<? extends K, ? extends V>> entries) {
public static <K extends Comparable<? super K>, V> TreeMap<K, V> ofEntries(Iterable<? extends Tuple2<? extends K, ? extends V>> entries) {
return ofEntries((Comparator<? super K> & Serializable) K::compareTo, entries);
}

Expand Down Expand Up @@ -409,7 +413,7 @@ public <K2, V2> TreeMap<K2, V2> bimap(Comparator<? super K2> keyComparator,
Objects.requireNonNull(keyMapper, "keyMapper is null");
Objects.requireNonNull(valueMapper, "valueMapper is null");
return createTreeMap(new EntryComparator<>(keyComparator),
entries.iterator().map(entry -> Tuple.of(keyMapper.apply(entry._1), valueMapper.apply(entry._2))));
entries.iterator().map(entry -> Tuple.of(keyMapper.apply(entry._1), valueMapper.apply(entry._2))));
}

@Override
Expand All @@ -428,7 +432,7 @@ public <K2, V2> TreeMap<K2, V2> flatMap(Comparator<? super K2> keyComparator,
BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return createTreeMap(new EntryComparator<>(keyComparator),
entries.iterator().flatMap(entry -> mapper.apply(entry._1, entry._2)));
entries.iterator().flatMap(entry -> mapper.apply(entry._1, entry._2)));
}

@Override
Expand Down Expand Up @@ -487,7 +491,7 @@ public <K2, V2> TreeMap<K2, V2> map(Comparator<? super K2> keyComparator,
BiFunction<? super K, ? super V, Tuple2<K2, V2>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return createTreeMap(new EntryComparator<>(keyComparator),
entries.iterator().map(entry -> mapper.apply(entry._1, entry._2)));
entries.iterator().map(entry -> mapper.apply(entry._1, entry._2)));
}

@Override
Expand All @@ -498,7 +502,7 @@ public <K2> TreeMap<K2, V> mapKeys(Function<? super K, ? extends K2> keyMapper)

@Override
public <K2> TreeMap<K2, V> mapKeys(Function<? super K, ? extends K2> keyMapper, BiFunction<? super V, ? super V, ? extends V> valueMerge) {
final Comparator<K2> comparator = Comparators.naturalComparator();
final Comparator<K2> comparator = naturalComparator();
return Collections.mapKeys(this, TreeMap.<K2, V> empty(comparator), keyMapper, valueMerge);
}

Expand Down Expand Up @@ -594,8 +598,8 @@ private static <K, V> TreeMap<K, V> createTreeMap(Comparator<? super Tuple2<K, V
for (Tuple2<? extends K, ? extends V> entry : entries) {
tree = tree.insert((Tuple2<K, V>) entry);
}
return tree.isEmpty() ? (TreeMap<K, V>) TreeMap.empty()
: new TreeMap<>(tree);
return tree.isEmpty() ? (TreeMap<K, V>) empty()
: new TreeMap<>(tree);
}

// -- Object
Expand Down
Loading

0 comments on commit 78931f6

Please sign in to comment.