Skip to content

Commit

Permalink
Use ImmutableMap.Builder.buildKeepingLast() where appropriate.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 417835702
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Dec 23, 2021
1 parent 53fd4ef commit a66f502
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.truth.Correspondence;
import com.google.common.truth.extensions.proto.DiffResult.RepeatedField;
Expand Down Expand Up @@ -272,14 +271,14 @@ private static ImmutableMap<Object, Object> toProtoMap(@Nullable Object containe
}
List<?> entryMessages = (List<?>) container;

// Can't use an ImmutableMap.Builder because proto wire format could have multiple entries with
// the same key. Documented behaviour is to use the last seen entry.
Map<Object, Object> retVal = Maps.newHashMap();
ImmutableMap.Builder<Object, Object> retVal = ImmutableMap.builder();
for (Object entry : entryMessages) {
Message message = (Message) entry;
retVal.put(valueAtFieldNumber(message, 1), valueAtFieldNumber(message, 2));
}
return ImmutableMap.copyOf(retVal);
// Use buildKeepingLast() because proto wire format could have multiple entries with
// the same key. Documented behaviour is to use the last seen entry.
return retVal.buildKeepingLast();
}

private static Object valueAtFieldNumber(Message message, int fieldNumber) {
Expand Down

0 comments on commit a66f502

Please sign in to comment.