Add UniqueValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractModifiedNodeBasedCandidateNode.java
index 80d63ba83f0d07c05c838d07a184cf3ac4c87ef6..25e44a74fe66935791c2d792f24811abc2633511 100644 (file)
@@ -6,18 +6,21 @@
  * 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.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 javax.annotation.Nonnull;
-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;
 
@@ -30,7 +33,7 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
             final TreeNode newMeta) {
         this.newMeta = newMeta;
         this.oldMeta = oldMeta;
-        this.mod = Preconditions.checkNotNull(mod);
+        this.mod = requireNonNull(mod);
     }
 
     protected final ModifiedNode getMod() {
@@ -46,10 +49,10 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
     }
 
     private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
-        return parent == null ? null : parent.getChild(id).orNull();
+        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;
         }
@@ -61,7 +64,7 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
 
     @SuppressWarnings("unchecked")
     private static NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getContainer(
-            @Nullable final TreeNode meta) {
+            final @Nullable TreeNode meta) {
         return meta == null ? null : (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>)meta.getData();
     }
 
@@ -71,7 +74,6 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
     }
 
     @Override
-    @Nonnull
     public Collection<DataTreeCandidateNode> getChildNodes() {
         switch (mod.getModificationType()) {
             case APPEARED:
@@ -86,7 +88,7 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
                 }
 
                 return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(),
-                    AbstractRecursiveCandidateNode::unmodifiedNode);
+                    DataTreeCandidateNodes::unmodified);
             case DELETE:
             case WRITE:
                 // This is unusual, the user is requesting we follow into an otherwise-terminal node.
@@ -94,59 +96,50 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
                 if (!canHaveChildren(oldMeta, newMeta)) {
                     return ImmutableList.of();
                 }
-                return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta));
+                return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta));
             default:
                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
         }
     }
 
     @Override
-    @Nonnull
     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<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
-        return meta == null ? Optional.absent() : Optional.of(meta.getData());
+    private static @NonNull Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
+        return meta == null ? Optional.empty() : Optional.of(meta.getData());
     }
 
     @Override
-    @Nonnull
     public final Optional<NormalizedNode<?, ?>> getDataAfter() {
         return optionalData(newMeta);
     }
 
     @Override
-    @Nonnull
     public final Optional<NormalizedNode<?, ?>> getDataBefore() {
         return optionalData(oldMeta);
     }
 
     @Override
-    public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
+    public final Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument identifier) {
         switch (mod.getModificationType()) {
             case APPEARED:
             case DISAPPEARED:
             case SUBTREE_MODIFIED:
-                final Optional<ModifiedNode> childMod = mod.getChild(identifier);
-                if (childMod.isPresent()) {
-                    return childNode(childMod.get());
-                }
-                return null;
+                return mod.getChild(identifier).map(this::childNode);
             case UNMODIFIED:
                 if (!canHaveChildren(oldMeta, newMeta)) {
-                    return null;
+                    return Optional.empty();
                 }
-                final Optional<NormalizedNode<?, ?>> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta)
-                        .getChild(identifier);
-                return maybeChild.isPresent() ? AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get()) : null;
+                return getContainer(newMeta != null ? newMeta : oldMeta).getChild(identifier)
+                        .map(DataTreeCandidateNodes::unmodified);
             case DELETE:
             case WRITE:
                 if (!canHaveChildren(oldMeta, newMeta)) {
-                    return null;
+                    return Optional.empty();
                 }
-                return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta),
-                    identifier);
+                return DataTreeCandidateNodes.containerDelta(getContainer(oldMeta), getContainer(newMeta), identifier);
             default:
                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
         }
@@ -158,7 +151,6 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida
         }
 
         @Override
-        @Nonnull
         public PathArgument getIdentifier() {
             return getMod().getIdentifier();
         }