X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Ftree%2FAbstractModifiedNodeBasedCandidateNode.java;h=25e44a74fe66935791c2d792f24811abc2633511;hb=refs%2Fchanges%2F01%2F93901%2F1;hp=6c947843ccd57f989891a1442f0a81767934651d;hpb=3c3c83239263f8b18fc092e3babac50201de58b5;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java index 6c947843cc..25e44a74fe 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java @@ -1,44 +1,39 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl.schema.tree; -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.base.Verify; + +import static com.google.common.base.Verify.verifyNotNull; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; import java.util.Collection; -import java.util.Collections; -import javax.annotation.Nullable; +import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNodes; import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode; abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode { - - private static final Function, DataTreeCandidateNode> TO_UNMODIFIED_NODES = new Function, DataTreeCandidateNode>() { - @Override - public DataTreeCandidateNode apply(final NormalizedNode input) { - return AbstractRecursiveCandidateNode.unmodifiedNode(input); - } - }; - private final ModifiedNode mod; private final TreeNode newMeta; private final TreeNode oldMeta; - protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, - final TreeNode oldMeta, final TreeNode newMeta) { + protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta, + final TreeNode newMeta) { this.newMeta = newMeta; this.oldMeta = oldMeta; - this.mod = Preconditions.checkNotNull(mod); + this.mod = requireNonNull(mod); } protected final ModifiedNode getMod() { @@ -53,15 +48,11 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida return oldMeta; } - private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) { - if (parent != null) { - return parent.getChild(id).orNull(); - } else { - return null; - } + private static TreeNode childMeta(final TreeNode parent, final PathArgument id) { + return parent == null ? null : parent.getChild(id).orElse(null); } - private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) { + private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) { if (oldMeta != null) { return oldMeta.getData() instanceof NormalizedNodeContainer; } @@ -72,8 +63,9 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida } @SuppressWarnings("unchecked") - private static NormalizedNodeContainer> getContainer(@Nullable final TreeNode meta) { - return (meta == null ? null : (NormalizedNodeContainer>)meta.getData()); + private static NormalizedNodeContainer> getContainer( + final @Nullable TreeNode meta) { + return meta == null ? null : (NormalizedNodeContainer>)meta.getData(); } private ChildNode childNode(final ModifiedNode childMod) { @@ -84,80 +76,72 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida @Override public Collection getChildNodes() { switch (mod.getModificationType()) { - case SUBTREE_MODIFIED: - return Collections2.transform(mod.getChildren(), new Function() { - @Override - public DataTreeCandidateNode apply(final ModifiedNode input) { - return childNode(input); + case APPEARED: + case DISAPPEARED: + case SUBTREE_MODIFIED: + return Collections2.transform(mod.getChildren(), this::childNode); + case UNMODIFIED: + // Unmodified node, but we still need to resolve potential children. canHaveChildren returns + // false if both arguments are null. + if (!canHaveChildren(oldMeta, newMeta)) { + return ImmutableList.of(); } - }); - case UNMODIFIED: - // Unmodified node, but we still need to resolve potential children. canHaveChildren returns - // false if both arguments are null. - if (canHaveChildren(oldMeta, newMeta)) { - return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), TO_UNMODIFIED_NODES); - } else { - return Collections.emptyList(); - } - case DELETE: - case WRITE: - // This is unusual, the user is requesting we follow into an otherwise-terminal node. - // We need to fudge things based on before/after data to correctly fake the expectations. - if (canHaveChildren(oldMeta, newMeta)) { - return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta)); - } else { - return Collections.emptyList(); - } - default: - throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); + + return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), + DataTreeCandidateNodes::unmodified); + case DELETE: + case WRITE: + // This is unusual, the user is requesting we follow into an otherwise-terminal node. + // We need to fudge things based on before/after data to correctly fake the expectations. + if (!canHaveChildren(oldMeta, newMeta)) { + return ImmutableList.of(); + } + return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta)); + default: + throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); } } @Override public ModificationType getModificationType() { - return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod); + return verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod); } - private static Optional> optionalData(final TreeNode meta) { - if (meta != null) { - return Optional.>of(meta.getData()); - } else { - return Optional.absent(); - } + private static @NonNull Optional> optionalData(final TreeNode meta) { + return meta == null ? Optional.empty() : Optional.of(meta.getData()); } @Override - public Optional> getDataAfter() { + public final Optional> getDataAfter() { return optionalData(newMeta); } @Override - public Optional> getDataBefore() { + public final Optional> getDataBefore() { return optionalData(oldMeta); } @Override - public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) { + public final Optional getModifiedChild(final PathArgument identifier) { switch (mod.getModificationType()) { - case SUBTREE_MODIFIED: - final Optional childMod = mod.getChild(identifier); - if (childMod.isPresent()) { - return childNode(childMod.get()); - } - return null; - case DELETE: - case UNMODIFIED: - case WRITE: - // FIXME: this is a linear walk. We need a Map of these in order to - // do something like getChildMap().get(identifier); - for (DataTreeCandidateNode c : getChildNodes()) { - if (identifier.equals(c.getIdentifier())) { - return c; + case APPEARED: + case DISAPPEARED: + case SUBTREE_MODIFIED: + return mod.getChild(identifier).map(this::childNode); + case UNMODIFIED: + if (!canHaveChildren(oldMeta, newMeta)) { + return Optional.empty(); + } + return getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier) + .map(DataTreeCandidateNodes::unmodified); + case DELETE: + case WRITE: + if (!canHaveChildren(oldMeta, newMeta)) { + return Optional.empty(); } - } - return null; - default: - throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); + return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier); + default: + throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); } } @@ -171,4 +155,10 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida return getMod().getIdentifier(); } } -} \ No newline at end of file + + @Override + public String toString() { + return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " + + this.newMeta + "}"; + } +}