Refactor ImmutableMapEntryNodeSchemaAwareBuilder
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapEntryNodeSchemaAwareBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import org.opendaylight.yangtools.util.ImmutableMapTemplate;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException.IllegalListKeyException;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26
27 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder {
28
29     private final ListSchemaNode schema;
30     private final DataNodeContainerValidator validator;
31
32     ImmutableMapEntryNodeSchemaAwareBuilder(final ListSchemaNode schema) {
33         this.schema = requireNonNull(schema);
34         this.validator = new DataNodeContainerValidator(schema);
35     }
36
37     @Override
38     public ImmutableMapEntryNodeBuilder withNodeIdentifier(final NodeIdentifierWithPredicates withNodeIdentifier) {
39         throw new UnsupportedOperationException("Node identifier created from schema");
40     }
41
42     @Override
43     public DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> withChild(
44             final DataContainerChild<?, ?> child) {
45         validator.validateChild(child.getIdentifier());
46         return super.withChild(child);
47     }
48
49     @Override
50     public MapEntryNode build() {
51         super.withNodeIdentifier(constructNodeIdentifier());
52         return super.build();
53     }
54
55     public static DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> create(
56             final ListSchemaNode schema) {
57         return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);
58     }
59
60     /**
61      * Build map entry node identifier from schema and provided children.
62      */
63     private NodeIdentifierWithPredicates constructNodeIdentifier() {
64         final Map<QName, Object> predicates;
65         final Collection<QName> keys = schema.getKeyDefinition();
66         if (!keys.isEmpty()) {
67             predicates = keyDefToPredicates(keys);
68         } else if (!childrenQNamesToPaths.isEmpty()) {
69             predicates = childrenToPredicates();
70         } else {
71             predicates = ImmutableMap.of();
72         }
73         return new NodeIdentifierWithPredicates(schema.getQName(), predicates);
74     }
75
76     private Map<QName, Object> childrenToPredicates() {
77         final Object[] values = new Object[childrenQNamesToPaths.size()];
78         int offset = 0;
79         for (Entry<QName, PathArgument> entry : childrenQNamesToPaths.entrySet()) {
80             values[offset++] = nonnullKeyValue(entry.getKey(), getChild(entry.getValue())).getValue();
81         }
82         return ImmutableMapTemplate.ordered(childrenQNamesToPaths.keySet()).instantiateWithValues(values);
83     }
84
85     private Map<QName, Object> keyDefToPredicates(final Collection<QName> keys) {
86         final Object[] values = new Object[keys.size()];
87         int offset = 0;
88         for (QName key : keys) {
89             values[offset++] = nonnullKeyValue(key, getChild(childrenQNamesToPaths.get(key))).getValue();
90         }
91         return ImmutableMapTemplate.ordered(keys).instantiateWithValues(values);
92     }
93
94     private DataContainerChild<?, ?> nonnullKeyValue(final QName key, final DataContainerChild<?, ?> value) {
95         if (value != null) {
96             return value;
97         }
98         throw new IllegalListKeyException("Key value not present for key: %s, in: %s values %s", key, schema.getQName(),
99             buildValue());
100     }
101 }