Skip to content

Commit

Permalink
Merge pull request #1402 from paplorinc/groupBy
Browse files Browse the repository at this point in the history
Changed and unified all `groupBy` methods to keep insertion order
  • Loading branch information
danieldietrich authored Jun 25, 2016
2 parents 8b574c3 + 1b730ef commit 065a1a6
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 243 deletions.
26 changes: 7 additions & 19 deletions javaslang/src/main/java/javaslang/collection/AbstractMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
*/
package javaslang.collection;

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

import java.util.Comparator;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.*;
import java.util.function.*;

/**
* An abstract {@link Map} implementation (not intended to be public).
Expand Down Expand Up @@ -48,7 +43,7 @@ public M put(Tuple2<? extends K, ? extends V> entry) {
@SuppressWarnings("unchecked")
@Override
public <U extends V> M put(K key, U value,
BiFunction<? super V, ? super U, ? extends V> merge) {
BiFunction<? super V, ? super U, ? extends V> merge) {
Objects.requireNonNull(merge, "the merge function is null");
final Option<V> currentValue = get(key);
if (currentValue.isEmpty()) {
Expand All @@ -60,14 +55,14 @@ public <U extends V> M put(K key, U value,

@Override
public <U extends V> M put(Tuple2<? extends K, U> entry,
BiFunction<? super V, ? super U, ? extends V> merge) {
BiFunction<? super V, ? super U, ? extends V> merge) {
Objects.requireNonNull(merge, "the merge function is null");
final Option<V> currentValue = get(entry._1);
if (currentValue.isEmpty()) {
return put(entry);
} else {
return put(entry.map2(
value -> merge.apply(currentValue.get(), value)));
value -> merge.apply(currentValue.get(), value)));
}
}

Expand Down Expand Up @@ -134,14 +129,7 @@ public M filter(Predicate<? super Tuple2<K, V>> predicate) {
@SuppressWarnings("unchecked")
@Override
public <C> Map<C, M> groupBy(Function<? super Tuple2<K, V>, ? extends C> classifier) {
Objects.requireNonNull(classifier, "classifier is null");
return foldLeft(HashMap.empty(), (map, entry) -> {
final C key = classifier.apply(entry);
final Map<K, V> values = map.get(key)
.map(entries -> entries.put(entry._1, entry._2))
.getOrElse(createFromEntries(Iterator.of(entry)));
return map.put(key, (M) values);
});
return Collections.groupBy(this, classifier, this::createFromEntries);
}

@Override
Expand Down
20 changes: 5 additions & 15 deletions javaslang/src/main/java/javaslang/collection/AbstractMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
*/
package javaslang.collection;

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

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.*;
import java.util.function.*;

/**
Expand Down Expand Up @@ -148,7 +145,7 @@ public M remove(K key, V value) {
@SuppressWarnings("unchecked")
@Override
public M removeAll(Iterable<? extends K> keys) {
Map<K, Traversable<V>> result = back.removeAll(keys);
final Map<K, Traversable<V>> result = back.removeAll(keys);
return (M) (result == back ? this : createFromMap(result));
}

Expand Down Expand Up @@ -227,14 +224,7 @@ public M filter(Predicate<? super Tuple2<K, V>> predicate) {

@Override
public <C> Map<C, Multimap<K, V>> groupBy(Function<? super Tuple2<K, V>, ? extends C> classifier) {
Objects.requireNonNull(classifier, "classifier is null");
return foldLeft(HashMap.empty(), (map, entry) -> {
final C key = classifier.apply(entry);
final Multimap<K, V> values = map.get(key)
.map(entries -> entries.put(entry._1, entry._2))
.getOrElse(createFromEntries(Iterator.of(entry)));
return map.put(key, values);
});
return Collections.groupBy(this, classifier, this::createFromEntries);
}

@Override
Expand Down Expand Up @@ -302,7 +292,7 @@ public <K2 extends K, V2 extends V> Multimap<K, V> merge(Multimap<K2, V2> that,
} else if (that.isEmpty()) {
return this;
} else {
Map<K, Traversable<V>> result = that.keySet().foldLeft(this.back, (map, key) -> {
final Map<K, Traversable<V>> result = that.keySet().foldLeft(this.back, (map, key) -> {
final Traversable<V> thisValues = map.get(key).getOrElse((Traversable<V>) emptyContainer.get());
final Traversable<V2> thatValues = that.get(key).get();
final Traversable<V> newValues = collisionResolution.apply(thisValues, thatValues);
Expand Down
24 changes: 10 additions & 14 deletions javaslang/src/main/java/javaslang/collection/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private Array(Object[] back) {

static <T> Array<T> wrap(Object[] array) {
return array.length == 0
? empty()
: new Array<T>(array);
? empty()
: new Array<T>(array);
}

/**
Expand Down Expand Up @@ -116,8 +116,8 @@ public static <T> Array<T> of(T... elements) {
public static <T> Array<T> ofAll(Iterable<? extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
return elements instanceof Array
? (Array<T>) elements
: wrap(toArray(elements));
? (Array<T>) elements
: wrap(toArray(elements));
}

/**
Expand Down Expand Up @@ -482,7 +482,7 @@ public static Array<Long> rangeClosedBy(long from, long toInclusive, long step)
* @return an Array with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T,U> Array<U> unfoldRight(T seed, Function<? super T,Option<Tuple2<? extends U, ? extends T>>> f) {
static <T, U> Array<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
return Iterator.unfoldRight(seed, f).toArray();
}

Expand All @@ -509,7 +509,7 @@ static <T,U> Array<U> unfoldRight(T seed, Function<? super T,Option<Tuple2<? ext
* @return an Array with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T,U> Array<U> unfoldLeft(T seed, Function<? super T,Option<Tuple2<? extends T, ? extends U>>> f) {
static <T, U> Array<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
return Iterator.unfoldLeft(seed, f).toArray();
}

Expand All @@ -536,7 +536,7 @@ static <T,U> Array<U> unfoldLeft(T seed, Function<? super T,Option<Tuple2<? exte
* @return an Array with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T> Array<T> unfold(T seed, Function<? super T,Option<Tuple2<? extends T, ? extends T>>> f) {
static <T> Array<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
return Iterator.unfold(seed, f).toArray();
}

Expand Down Expand Up @@ -708,11 +708,7 @@ public <U> Array<U> flatMap(Function<? super T, ? extends Iterable<? extends U>>

@Override
public <C> Map<C, Array<T>> groupBy(Function<? super T, ? extends C> classifier) {
return foldLeft(HashMap.empty(), (map, t) -> {
final C key = classifier.apply(t);
final Array<T> values = map.get(key).map(ts -> ts.append(t)).getOrElse(of(t));
return map.put(key, values);
});
return Collections.groupBy(this, classifier, Array::ofAll);
}

@Override
Expand Down Expand Up @@ -1056,13 +1052,13 @@ public <U> Array<U> scanLeft(U zero, BiFunction<? super U, ? super T, ? extends
new java.util.ArrayList<>(), (c, u) -> {
c.add(u);
return c;
}, list -> Array.<U>wrap(list.toArray()));
}, list -> Array.<U> wrap(list.toArray()));
}

@Override
public <U> Array<U> scanRight(U zero, BiFunction<? super T, ? super U, ? extends U> operation) {
Objects.requireNonNull(operation, "operation is null");
return Collections.scanRight(this, zero, operation, List.empty(), List::prepend, list -> Array.<U>wrap(list.toJavaArray()));
return Collections.scanRight(this, zero, operation, List.empty(), List::prepend, list -> Array.<U> wrap(list.toJavaArray()));
}

@Override
Expand Down
40 changes: 17 additions & 23 deletions javaslang/src/main/java/javaslang/collection/BitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
*/
package javaslang.collection;

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

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

Expand Down Expand Up @@ -59,9 +54,9 @@ public BitSet<T> empty() {

public BitSet<T> of(T t) {
final int value = toInt.apply(t);
if(value < BitSetModule.BITS_PER_WORD) {
if (value < BitSetModule.BITS_PER_WORD) {
return new BitSetModule.BitSet1<>(fromInt, toInt, 1L << value);
} else if(value < 2 * BitSetModule.BITS_PER_WORD) {
} else if (value < 2 * BitSetModule.BITS_PER_WORD) {
return new BitSetModule.BitSet2<>(fromInt, toInt, 0L, 1L << value);
} else {
return empty().add(t);
Expand Down Expand Up @@ -657,7 +652,7 @@ long[] shrink(long[] elements) {
while (newlen > 0 && elements[newlen - 1] == 0) {
newlen--;
}
long[] newelems = new long[newlen];
final long[] newelems = new long[newlen];
System.arraycopy(elements, 0, newelems, 0, newlen);
return newelems;
}
Expand Down Expand Up @@ -715,11 +710,11 @@ public BitSet<T> intersect(Set<? extends T> elements) {
if (isEmpty() || elements.isEmpty()) {
return createEmpty();
} else {
int size = size();
final int size = size();
if (size <= elements.size()) {
return retainAll(elements);
} else {
BitSet<T> results = createFromAll(elements).retainAll(this);
final BitSet<T> results = createFromAll(elements).retainAll(this);
return (size == results.size()) ? this : results;
}
}
Expand Down Expand Up @@ -759,9 +754,8 @@ public BitSet<T> filter(Predicate<? super T> predicate) {
}

@Override
public <C> Map<C, BitSet<T>> groupBy(Function<? super T, ? extends C> classifier) {
Objects.requireNonNull(classifier, "classifier is null");
return iterator().groupBy(classifier).map((key, iterator) -> Tuple.of(key, createFromAll(iterator)));
public <C> Map<C, BitSet<T>> groupBy(Function<? super T, ? extends C> classifier) {
return Collections.groupBy(this, classifier, this::createFromAll);
}

@Override
Expand Down Expand Up @@ -899,7 +893,7 @@ class BitSetIterator<T> extends AbstractIterator<T> {

@Override
protected T getNext() {
int pos = Long.numberOfTrailingZeros(element);
final int pos = Long.numberOfTrailingZeros(element);
element &= ~(1L << pos);
return bitSet.fromInt.apply(pos + (index << ADDRESS_BITS_PER_WORD));
}
Expand Down Expand Up @@ -937,10 +931,10 @@ int getWordsNum() {

@Override
long[] copyExpand(int wordsNum) {
if(wordsNum < 1) {
if (wordsNum < 1) {
wordsNum = 1;
}
long[] arr = new long[wordsNum];
final long[] arr = new long[wordsNum];
arr[0] = elements;
return arr;
}
Expand Down Expand Up @@ -1004,10 +998,10 @@ int getWordsNum() {

@Override
long[] copyExpand(int wordsNum) {
if(wordsNum < 2) {
if (wordsNum < 2) {
wordsNum = 2;
}
long[] arr = new long[wordsNum];
final long[] arr = new long[wordsNum];
arr[0] = elements1;
arr[1] = elements2;
return arr;
Expand Down Expand Up @@ -1089,10 +1083,10 @@ int getWordsNum() {

@Override
long[] copyExpand(int wordsNum) {
if(wordsNum < elements.length) {
if (wordsNum < elements.length) {
wordsNum = elements.length;
}
long[] arr = new long[wordsNum];
final long[] arr = new long[wordsNum];
System.arraycopy(elements, 0, arr, 0, elements.length);
return arr;
}
Expand All @@ -1107,7 +1101,7 @@ public T head() {
int offset = 0;
int element = 0;
for (int i = 0; i < getWordsNum(); i++) {
if(elements[i] == 0) {
if (elements[i] == 0) {
offset += BITS_PER_WORD;
} else {
element = offset + Long.numberOfTrailingZeros(elements[i]);
Expand Down
Loading

0 comments on commit 065a1a6

Please sign in to comment.