-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend ImmutableMapRules
Refaster rule collection
#1488
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa4af1e
Extend `ImmutableMapRules` Refaster rule collection
Stephan202 5787d11
This belongs in the other PR
Stephan202 eca7f60
Also this one
Stephan202 b7d09da
Update integration tests
Stephan202 b53c382
Merge branch 'master' into sschroevers/extend-ImmutableMapRules
Stephan202 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import com.google.errorprone.refaster.annotation.Matches; | ||
import com.google.errorprone.refaster.annotation.MayOptionallyUse; | ||
import com.google.errorprone.refaster.annotation.Placeholder; | ||
import com.google.errorprone.refaster.annotation.Repeated; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
@@ -44,12 +45,28 @@ ImmutableMap.Builder<K, V> after() { | |
} | ||
} | ||
|
||
/** | ||
* Prefer {@link ImmutableMap.Builder#buildOrThrow()} over the less explicit {@link | ||
* ImmutableMap.Builder#build()}. | ||
*/ | ||
static final class ImmutableMapBuilderBuildOrThrow<K, V> { | ||
@BeforeTemplate | ||
ImmutableMap<K, V> before(ImmutableMap.Builder<K, V> builder) { | ||
return builder.build(); | ||
} | ||
|
||
@AfterTemplate | ||
ImmutableMap<K, V> after(ImmutableMap.Builder<K, V> builder) { | ||
return builder.buildOrThrow(); | ||
} | ||
} | ||
|
||
/** Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives. */ | ||
static final class EntryToImmutableMap<K, V> { | ||
@BeforeTemplate | ||
ImmutableMap<K, V> before(Map.Entry<? extends K, ? extends V> entry) { | ||
return Refaster.anyOf( | ||
ImmutableMap.<K, V>builder().put(entry).build(), | ||
ImmutableMap.<K, V>builder().put(entry).buildOrThrow(), | ||
Stream.of(entry).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))); | ||
} | ||
|
||
|
@@ -104,16 +121,17 @@ ImmutableMap<K, V> after( | |
/** Prefer {@link ImmutableMap#copyOf(Iterable)} over more contrived alternatives. */ | ||
static final class EntryIterableToImmutableMap<K, V> { | ||
@BeforeTemplate | ||
ImmutableMap<K, V> before(Map<? extends K, ? extends V> iterable) { | ||
Map<K, V> before(Map<? extends K, ? extends V> iterable) { | ||
return Refaster.anyOf( | ||
ImmutableMap.copyOf(iterable.entrySet()), | ||
ImmutableMap.<K, V>builder().putAll(iterable).build()); | ||
ImmutableMap.<K, V>builder().putAll(iterable).buildOrThrow(), | ||
Map.copyOf(iterable)); | ||
} | ||
|
||
@BeforeTemplate | ||
ImmutableMap<K, V> before(Iterable<? extends Map.Entry<? extends K, ? extends V>> iterable) { | ||
return Refaster.anyOf( | ||
ImmutableMap.<K, V>builder().putAll(iterable).build(), | ||
ImmutableMap.<K, V>builder().putAll(iterable).buildOrThrow(), | ||
Streams.stream(iterable).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))); | ||
} | ||
|
||
|
@@ -139,8 +157,6 @@ abstract static class StreamOfMapEntriesToImmutableMap<E, K, V> { | |
@Placeholder(allowsIdentity = true) | ||
abstract V valueFunction(@MayOptionallyUse E element); | ||
|
||
// XXX: We could add variants in which the entry is created some other way, but we have another | ||
// rule that covers canonicalization to `Map.entry`. | ||
Comment on lines
-142
to
-143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropped, because this describes our approach more generally these days. |
||
@BeforeTemplate | ||
ImmutableMap<K, V> before(Stream<E> stream) { | ||
return stream | ||
|
@@ -224,7 +240,11 @@ ImmutableMap<K, V2> after(Map<K, V1> map) { | |
static final class ImmutableMapOf<K, V> { | ||
@BeforeTemplate | ||
Map<K, V> before() { | ||
return Refaster.anyOf(ImmutableMap.<K, V>builder().build(), emptyMap(), Map.of()); | ||
return Refaster.anyOf( | ||
ImmutableMap.<K, V>builder().buildOrThrow(), | ||
ImmutableMap.ofEntries(), | ||
emptyMap(), | ||
Map.of()); | ||
} | ||
|
||
@AfterTemplate | ||
|
@@ -243,7 +263,10 @@ static final class ImmutableMapOf1<K, V> { | |
@BeforeTemplate | ||
Map<K, V> before(K k1, V v1) { | ||
return Refaster.anyOf( | ||
ImmutableMap.<K, V>builder().put(k1, v1).build(), singletonMap(k1, v1), Map.of(k1, v1)); | ||
ImmutableMap.<K, V>builder().put(k1, v1).buildOrThrow(), | ||
ImmutableMap.ofEntries(Map.entry(k1, v1)), | ||
singletonMap(k1, v1), | ||
Map.of(k1, v1)); | ||
} | ||
|
||
@AfterTemplate | ||
|
@@ -261,7 +284,8 @@ ImmutableMap<K, V> after(K k1, V v1) { | |
static final class ImmutableMapOf2<K, V> { | ||
@BeforeTemplate | ||
Map<K, V> before(K k1, V v1, K k2, V v2) { | ||
return Map.of(k1, v1, k2, v2); | ||
return Refaster.anyOf( | ||
ImmutableMap.ofEntries(Map.entry(k1, v1), Map.entry(k2, v2)), Map.of(k1, v1, k2, v2)); | ||
} | ||
|
||
@AfterTemplate | ||
|
@@ -279,7 +303,9 @@ ImmutableMap<K, V> after(K k1, V v1, K k2, V v2) { | |
static final class ImmutableMapOf3<K, V> { | ||
@BeforeTemplate | ||
Map<K, V> before(K k1, V v1, K k2, V v2, K k3, V v3) { | ||
return Map.of(k1, v1, k2, v2, k3, v3); | ||
return Refaster.anyOf( | ||
ImmutableMap.ofEntries(Map.entry(k1, v1), Map.entry(k2, v2), Map.entry(k3, v3)), | ||
Map.of(k1, v1, k2, v2, k3, v3)); | ||
} | ||
|
||
@AfterTemplate | ||
|
@@ -299,7 +325,10 @@ ImmutableMap<K, V> after(K k1, V v1, K k2, V v2, K k3, V v3) { | |
static final class ImmutableMapOf4<K, V> { | ||
@BeforeTemplate | ||
Map<K, V> before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { | ||
return Map.of(k1, v1, k2, v2, k3, v3, k4, v4); | ||
return Refaster.anyOf( | ||
ImmutableMap.ofEntries( | ||
Map.entry(k1, v1), Map.entry(k2, v2), Map.entry(k3, v3), Map.entry(k4, v4)), | ||
Map.of(k1, v1, k2, v2, k3, v3, k4, v4)); | ||
} | ||
|
||
@AfterTemplate | ||
|
@@ -319,7 +348,14 @@ ImmutableMap<K, V> after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { | |
static final class ImmutableMapOf5<K, V> { | ||
@BeforeTemplate | ||
Map<K, V> before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { | ||
return Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); | ||
return Refaster.anyOf( | ||
ImmutableMap.ofEntries( | ||
Map.entry(k1, v1), | ||
Map.entry(k2, v2), | ||
Map.entry(k3, v3), | ||
Map.entry(k4, v4), | ||
Map.entry(k5, v5)), | ||
Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5)); | ||
} | ||
|
||
@AfterTemplate | ||
|
@@ -370,6 +406,22 @@ ImmutableMap<K, V> after(Map<K, V> map) { | |
} | ||
} | ||
|
||
/** | ||
* Prefer {@link ImmutableMap#ofEntries(Map.Entry[])} over alternatives that don't communicate the | ||
* immutability of the resulting map at the type level. | ||
*/ | ||
static final class ImmutableMapOfEntries<K, V> { | ||
@BeforeTemplate | ||
Map<K, V> before(@Repeated Map.Entry<? extends K, ? extends V> entries) { | ||
return Map.ofEntries(entries); | ||
} | ||
|
||
@AfterTemplate | ||
ImmutableMap<K, V> after(@Repeated Map.Entry<? extends K, ? extends V> entries) { | ||
return ImmutableMap.ofEntries(entries); | ||
} | ||
} | ||
|
||
// XXX: Add a rule for this: | ||
// Maps.transformValues(streamOfEntries.collect(groupBy(fun)), ImmutableMap::copyOf) | ||
// -> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The builder subtypes for
ImmutableBiMap
andImmutableSortedMap
extend fromImmutableMap.Builder
, so thosebuild()
calls are also covered.