static ArrayMembership checkArrayConsistency()

in src/main/software/amazon/event/ruler/ArrayMembership.java [64:100]


    static ArrayMembership checkArrayConsistency(final ArrayMembership membershipSoFar, final ArrayMembership fieldMembership) {

        // no existing memberships, so we'll take the ones from the field, if any
        if (membershipSoFar.isEmpty()) {
            return fieldMembership.isEmpty() ? membershipSoFar : new ArrayMembership(fieldMembership);
        }

        // any change will come from memberships in the new field we're investigating. For each of its memberships
        ArrayMembership newMembership = null;
        for (IntIntMap.Entry arrayEntry : fieldMembership.membership.entries()) {
            final int array = arrayEntry.getKey();
            final int indexInThisArrayOfThisField = arrayEntry.getValue();
            final int indexInThisArrayPreviouslyAppearingInMatch = membershipSoFar.getMembership(array);

            if (indexInThisArrayPreviouslyAppearingInMatch == IntIntMap.NO_VALUE) {

                // if there's no membership so far, this is an acceptable delta. Update the new memberships, first
                //  creating it if necessary
                if (newMembership == null) {
                    newMembership = new ArrayMembership(membershipSoFar);
                }
                newMembership.putMembership(array, indexInThisArrayOfThisField);

            } else {

                // This field does appear within an index that has already appeared in the matching task so far.
                //  If it's in the same element, fine, no updates. If it's a different entry, return null to
                //  signal array-inconsistency.
                if (indexInThisArrayOfThisField != indexInThisArrayPreviouslyAppearingInMatch) {
                    return null;
                }
            }
        }

        // we may have scanned all the fields and not added anything, in which case return the input
        return (newMembership == null) ? membershipSoFar : newMembership;
    }