+
+ private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
+ Verify.verify(normalizedNode instanceof ChoiceNode);
+ final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
+ if (!children.isEmpty()) {
+ final DataContainerChild<?, ?> firstChild = children.iterator().next();
+ final CaseEnforcer enforcer = Verify.verifyNotNull(caseEnforcers.get(firstChild.getIdentifier()),
+ "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
+ + "in current tree type.", firstChild.getIdentifier(), normalizedNode.getIdentifier());
+
+ // Make sure no leaves from other cases are present
+ for (final CaseEnforcer other : exclusions.get(enforcer)) {
+ for (final PathArgument id : other.getAllChildIdentifiers()) {
+ final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode,
+ id);
+ Preconditions.checkArgument(!maybeChild.isPresent(),
+ "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
+ firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
+ }
+ }
+
+ // Make sure all mandatory children are present
+ enforcer.enforceOnTreeNode(normalizedNode);
+ }
+ }
+
+ @Override
+ protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
+ final TreeNode ret = super.applyMerge(modification, currentMeta, version);
+ enforceCases(ret);
+ return ret;
+ }
+
+ @Override
+ protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
+ final Optional<TreeNode> currentMeta, final Version version) {
+ final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
+ enforceCases(ret);
+ return ret;
+ }
+
+ @Override
+ protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
+ final TreeNode ret = super.applyTouch(modification, currentMeta, version);
+ enforceCases(ret);
+ return ret;
+ }
+}
+