Skip to content

Commit

Permalink
Improved some methods in CharSeq, Vector, TreeSet and HashSet classes… (
Browse files Browse the repository at this point in the history
#2208)

* Improved some methods in CharSeq, Vector, TreeSet and HashSet classes using Collections.isEmpty(Iterable) method (#1958)

* Reverted changes in addAll(Iterable) methods in TreeSet and HashSet classes

* Fixed ofAll(Iterable) method in Vector class in order to pass Euler67 and Euler18 tests
  • Loading branch information
NataliiaPrivezentseva authored and danieldietrich committed Jan 6, 2019
1 parent 2537ee7 commit b080ce7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
21 changes: 15 additions & 6 deletions vavr/src/main/java/io/vavr/collection/CharSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@ public static CharSeq of(char... characters) {
*/
public static CharSeq ofAll(Iterable<? extends Character> elements) {
Objects.requireNonNull(elements, "elements is null");
if (Collections.isEmpty(elements)){
return EMPTY;
}
if (elements instanceof CharSeq) {
return (CharSeq) elements;
}
final StringBuilder sb = new StringBuilder();
for (char character : elements) {
sb.append(character);
}
return sb.length() == 0 ? EMPTY : of(sb);
return of(sb);
}

/**
Expand Down Expand Up @@ -772,12 +775,18 @@ public CharSeq prepend(Character element) {
@Override
public CharSeq prependAll(Iterable<? extends Character> elements) {
Objects.requireNonNull(elements, "elements is null");
final StringBuilder sb = new StringBuilder();
for (char element : elements) {
sb.append(element);
if (Collections.isEmpty(elements)) {
return this;
} else if (isEmpty()) {
return ofAll(elements);
} else {
final StringBuilder sb = new StringBuilder();
for (char element : elements) {
sb.append(element);
}
sb.append(back);
return CharSeq.of(sb);
}
sb.append(back);
return sb.length() == 0 ? EMPTY : of(sb);
}

@Override
Expand Down
23 changes: 14 additions & 9 deletions vavr/src/main/java/io/vavr/collection/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.*;
import java.util.stream.Collector;

import static io.vavr.collection.Collections.removeAll;
import static io.vavr.collection.Collections.withSize;
import static io.vavr.collection.JavaConverters.ChangePolicy.IMMUTABLE;
import static io.vavr.collection.JavaConverters.ChangePolicy.MUTABLE;
Expand Down Expand Up @@ -170,12 +171,14 @@ public static <T> Vector<T> fill(int n, Supplier<? extends T> s) {
@SuppressWarnings("unchecked")
public static <T> Vector<T> ofAll(Iterable<? extends T> iterable) {
Objects.requireNonNull(iterable, "iterable is null");
if (iterable instanceof Traversable && io.vavr.collection.Collections.isEmpty(iterable)){
return empty();
}
if (iterable instanceof Vector) {
return (Vector<T>) iterable;
} else {
final Object[] values = withSize(iterable).toArray();
return ofAll(BitMappedTrie.ofAll(values));
}
final Object[] values = withSize(iterable).toArray();
return ofAll(BitMappedTrie.ofAll(values));
}

/**
Expand Down Expand Up @@ -606,10 +609,11 @@ public Vector<T> appendAll(Iterable<? extends T> iterable) {
Objects.requireNonNull(iterable, "iterable is null");
if (isEmpty()) {
return ofAll(iterable);
} else {
final BitMappedTrie<T> that = trie.appendAll(iterable);
return (that == trie) ? this : new Vector<>(that);
}
if (io.vavr.collection.Collections.isEmpty(iterable)){
return this;
}
return new Vector<>(trie.appendAll(iterable));
}

@GwtIncompatible
Expand Down Expand Up @@ -918,10 +922,11 @@ public Vector<T> prependAll(Iterable<? extends T> iterable) {
Objects.requireNonNull(iterable, "iterable is null");
if (isEmpty()) {
return ofAll(iterable);
} else {
final BitMappedTrie<T> that = trie.prependAll(iterable);
return (that == trie) ? this : new Vector<>(that);
}
if (io.vavr.collection.Collections.isEmpty(iterable)){
return this;
}
return new Vector<>(trie.prependAll(iterable));
}

@Override
Expand Down

0 comments on commit b080ce7

Please sign in to comment.