-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add shaded guava collections and use it #2086
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7b39d8f
add shaded guava collections and use it in Field
andimarek e0cd2bf
remove not needed guava files
andimarek cccba56
use immutable collections more and add Util class
andimarek 82f4c6d
simplify build
andimarek a834b78
add documentation
andimarek fda0f99
add Immutable map which allows null values
andimarek 3fff29f
More immutable support in more of the places
bbakerman dde4322
More immutable support in more and more of the places
bbakerman 59d2878
More Immutable code in and around the schema
bbakerman 24b0271
Optimised import
bbakerman d8d11c8
Made the map of types in schema be immutable
bbakerman f5b7614
More Immutable things
bbakerman 0c9039d
More Immutability and tweaked the ImmutableMapWithNullValues class
bbakerman b31850b
More Immutability on things
bbakerman 75f61ac
More Immutability on AST things
bbakerman 8b020b6
More Immutability on AST things and a few other things
bbakerman df52444
renamed listMap to just map and added more tests
bbakerman 0bcc618
Renamed to ImmutableKit
bbakerman de88d7a
Used ImmutableList collector
bbakerman fb7b6af
Used ImmutableList collector - except for sort
bbakerman 03df610
Merge remote-tracking branch 'origin/master' into immutable-collectio…
bbakerman d9e0f44
PR feedback
bbakerman 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package graphql.collect; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import graphql.Assert; | ||
import graphql.Internal; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
@Internal | ||
public final class ImmutableKit { | ||
|
||
public static <T> ImmutableList<T> emptyList() { | ||
return ImmutableList.of(); | ||
} | ||
|
||
public static <T> ImmutableList<T> nonNullCopyOf(Collection<T> collection) { | ||
return collection == null ? emptyList() : ImmutableList.copyOf(collection); | ||
} | ||
|
||
public static <K, V> ImmutableMap<K, V> emptyMap() { | ||
return ImmutableMap.of(); | ||
} | ||
|
||
/** | ||
* ImmutableMaps are hard to build via {@link Map#computeIfAbsent(Object, Function)} style. This methods | ||
* allows you to take a mutable map with mutable list of keys and make it immutable. | ||
* <p> | ||
* This of course has a cost - if the map is very large you will be using more memory. But for static | ||
* maps that live a long life it maybe be worth it. | ||
* | ||
* @param startingMap the starting input map | ||
* @param <K> for key | ||
* @param <V> for victory | ||
* | ||
* @return and Immutable map of ImmutableList values | ||
*/ | ||
|
||
public static <K, V> ImmutableMap<K, ImmutableList<V>> toImmutableMapOfLists(Map<K, List<V>> startingMap) { | ||
Assert.assertNotNull(startingMap); | ||
ImmutableMap.Builder<K, ImmutableList<V>> map = ImmutableMap.builder(); | ||
for (Map.Entry<K, List<V>> e : startingMap.entrySet()) { | ||
ImmutableList<V> value = ImmutableList.copyOf(startingMap.getOrDefault(e.getKey(), emptyList())); | ||
map.put(e.getKey(), value); | ||
} | ||
return map.build(); | ||
} | ||
|
||
|
||
public static <K, V> ImmutableMap<K, V> addToMap(Map<K, V> existing, K newKey, V newVal) { | ||
return ImmutableMap.<K, V>builder().putAll(existing).put(newKey, newVal).build(); | ||
} | ||
|
||
public static <K, V> ImmutableMap<K, V> mergeMaps(Map<K, V> m1, Map<K, V> m2) { | ||
return ImmutableMap.<K, V>builder().putAll(m1).putAll(m2).build(); | ||
} | ||
|
||
public static <T> ImmutableList<T> concatLists(List<T> l1, List<T> l2) { | ||
return ImmutableList.<T>builder().addAll(l1).addAll(l2).build(); | ||
} | ||
|
||
/** | ||
* This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed | ||
* for the flexible style. Benchmarking has shown this to outperform `stream()`. | ||
* | ||
* @param collection the collection to map | ||
* @param mapper the mapper function | ||
* @param <T> for two | ||
* @param <R> for result | ||
* | ||
* @return a map immutable list of results | ||
*/ | ||
public static <T, R> ImmutableList<R> map(Collection<T> collection, Function<? super T, ? extends R> mapper) { | ||
Assert.assertNotNull(collection); | ||
Assert.assertNotNull(mapper); | ||
@SuppressWarnings("RedundantTypeArguments") | ||
ImmutableList.Builder<R> builder = ImmutableList.<R>builder(); | ||
for (T t : collection) { | ||
R r = mapper.apply(t); | ||
builder.add(r); | ||
} | ||
return builder.build(); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.