Skip to content

Commit

Permalink
Minor optimization for the common case of merging empty string sets. (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwb authored Aug 5, 2024
1 parent bfc64d5 commit 80ae932
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.auto.value.AutoValue;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -50,12 +49,16 @@ public static StringSetData empty() {
* Combines this {@link StringSetData} with other, both original StringSetData are left intact.
*/
public StringSetData combine(StringSetData other) {
// do not merge other on this as this StringSetData might hold an immutable set like in case
// of EmptyStringSetData
Set<String> combined = new HashSet<>();
combined.addAll(this.stringSet());
combined.addAll(other.stringSet());
return StringSetData.create(combined);
if (this.stringSet().isEmpty()) {
return other;
} else if (other.stringSet().isEmpty()) {
return this;
} else {
ImmutableSet.Builder<String> combined = ImmutableSet.builder();
combined.addAll(this.stringSet());
combined.addAll(other.stringSet());
return StringSetData.create(combined.build());
}
}

/**
Expand Down

0 comments on commit 80ae932

Please sign in to comment.