Skip to content

Commit

Permalink
Remove ArrayList allocation from Mappings#bijection, and add helpful …
Browse files Browse the repository at this point in the history
…message in case NPE is thrown

bijection requires all indices from [0..targets.size()-1) to be mapped
to non-null, and NPE was thrown if that is not the case.

The change adds extra message, so it is easier to see what aas the mapping.
  • Loading branch information
vlsi committed Sep 5, 2020
1 parent ad164ed commit fb2a02f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/org/apache/calcite/util/mapping/Mappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,15 @@ public static Mapping bijection(List<Integer> targets) {
*
* <p>Throws if sources and targets are not one to one. */
public static Mapping bijection(Map<Integer, Integer> targets) {
final List<Integer> targetList = new ArrayList<>();
int[] ints = new int[targets.size()];
for (int i = 0; i < targets.size(); i++) {
targetList.add(targets.get(i));
Integer value = targets.get(i);
if (value == null) {
throw new NullPointerException("Index " + i + " is not mapped in " + targets);
}
ints[i] = value;
}
return new Permutation(Ints.toArray(targetList));
return new Permutation(ints);
}

/**
Expand Down

0 comments on commit fb2a02f

Please sign in to comment.