lukecwik commented on a change in pull request #11821:
URL: https://github.com/apache/beam/pull/11821#discussion_r440427931
##########
File path:
sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java
##########
@@ -533,4 +1251,141 @@ public String toString() {
return Collections.singletonMap(tag, pCollection);
}
}
+
+ /** A {@link MultimapView} to {@link Map Map<K, V>} adapter. */
+ private static class MultimapViewToMapAdapter<K, V> extends AbstractMap<K,
V> {
+ private final MultimapView<K, V> primitiveViewT;
+ private final Supplier<Integer> size;
+
+ private MultimapViewToMapAdapter(MultimapView<K, V> primitiveViewT) {
+ this.primitiveViewT = primitiveViewT;
+ this.size = Suppliers.memoize(() ->
Iterables.size(primitiveViewT.get()));
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return primitiveViewT.get((K) key).iterator().hasNext();
+ }
+
+ @Override
+ public V get(Object key) {
+ Iterator<V> iterator = primitiveViewT.get((K) key).iterator();
+ if (!iterator.hasNext()) {
+ return null;
+ }
+ V value = iterator.next();
+ if (iterator.hasNext()) {
+ throw new IllegalArgumentException("Duplicate values for " + key);
+ }
+ return value;
+ }
+
+ @Override
+ public int size() {
+ return size.get();
+ }
+
+ @Override
+ public Set<Entry<K, V>> entrySet() {
+ return new AbstractSet<Entry<K, V>>() {
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return FluentIterable.from(primitiveViewT.get())
+ .<Entry<K, V>>transform((K key) -> new SimpleEntry<>(key,
get(key)))
+ .iterator();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ if (!(o instanceof Entry)) {
+ return false;
+ }
+ Entry<?, ?> entry = (Entry<?, ?>) o;
+ // We treat the absence of the key in the map as a difference in
these abstract sets. The
+ // underlying primitive view represents missing keys as empty
iterables so we use this
+ // to check if the map contains the key first before comparing
values.
+ Iterable<V> value = primitiveViewT.get((K) entry.getKey());
+ if (value.iterator().hasNext()) {
+ return false;
+ }
+ return Objects.equals(entry.getValue(), value);
+ }
+
+ @Override
+ public int size() {
+ return size.get();
+ }
+ };
+ }
+ }
+
+ /** A {@link MultimapView} to {@link Map Map<K, Iterable<V>>} adapter. */
+ private static class MultimapViewToMultimapAdapter<K, V> extends
AbstractMap<K, Iterable<V>> {
+ private final MultimapView<K, V> primitiveViewT;
+ private final Supplier<Integer> size;
+
+ private MultimapViewToMultimapAdapter(MultimapView<K, V> primitiveViewT) {
+ this.primitiveViewT = primitiveViewT;
+ this.size = Suppliers.memoize(() ->
Iterables.size(primitiveViewT.get()));
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return primitiveViewT.get((K) key).iterator().hasNext();
+ }
+
+ @Override
+ public Iterable<V> get(Object key) {
+ Iterable<V> values = primitiveViewT.get((K) key);
+ // The only was for the values iterable to be empty is for us to have
never seen such a key
Review comment:
Done
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]