Deprecate schema-aware builders
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapNodeSchemaAwareBuilder.java
index 77a0e496d5c083870b9eddab867bdf0ead5d49d9..412dbfe41bbbfea9a32ee66f5d35596df636449f 100644 (file)
@@ -7,37 +7,54 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Collections;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 
-import com.google.common.base.Preconditions;
-
+@Deprecated(since = "6.0.7", forRemoval = true)
 public class ImmutableMapNodeSchemaAwareBuilder extends ImmutableMapNodeBuilder {
-
     private final ListSchemaNode schema;
 
-    protected ImmutableMapNodeSchemaAwareBuilder(ListSchemaNode schema) {
-        super();
-        this.schema = schema;
-        super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
+    protected ImmutableMapNodeSchemaAwareBuilder(final ListSchemaNode schema) {
+        this.schema = requireNonNull(schema);
+        super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
+    }
+
+    protected ImmutableMapNodeSchemaAwareBuilder(final ListSchemaNode schema, final ImmutableMapNode node) {
+        super(node);
+        this.schema = requireNonNull(schema);
+        super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
     }
 
-    public static CollectionNodeBuilder<MapEntryNode, MapNode> create(ListSchemaNode schema) {
+    public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create(final ListSchemaNode schema) {
         return new ImmutableMapNodeSchemaAwareBuilder(schema);
     }
 
+    public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create(final ListSchemaNode schema,
+            final MapNode node) {
+        if (!(node instanceof ImmutableMapNode)) {
+            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        }
+
+        return new ImmutableMapNodeSchemaAwareBuilder(schema, (ImmutableMapNode) node);
+    }
+
     @Override
-    public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(MapEntryNode child) {
-        Preconditions.checkArgument(schema.getQName().equals(child.getNodeType()),
-                "Incompatible node type, should be: %s, is: %s", schema.getQName(), child.getNodeType());
+    public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
+        DataValidationException.checkLegalChild(schema.getQName().equals(child.getNodeType()), child.getIdentifier(),
+            schema, Collections.singleton(schema.getQName()));
         return super.withChild(child);
     }
 
     @Override
-    public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
+    public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
         throw new UnsupportedOperationException("Node identifier created from schema");
     }
 }