Cleanup codecs a bit
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / parser / LeafSetEntryNodeBaseParser.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.transform.base.parser;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Iterables;
12 import java.util.Map;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
20
21 /**
22  * Abstract(base) parser for LeafSetEntryNodes, parses elements of type E.
23  *
24  * @param <E> type of elements to be parsed
25  */
26 public abstract class LeafSetEntryNodeBaseParser<E> implements ExtensibleParser<NodeWithValue, E, LeafSetEntryNode<?>, LeafListSchemaNode> {
27
28     private final BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> buildingStrategy;
29
30     public LeafSetEntryNodeBaseParser() {
31         buildingStrategy = new SimpleLeafSetEntryBuildingStrategy();
32     }
33
34     public LeafSetEntryNodeBaseParser(final BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> buildingStrategy) {
35         this.buildingStrategy = buildingStrategy;
36     }
37
38     @SuppressWarnings("unchecked")
39     @Override
40     public final LeafSetEntryNode<?> parse(final Iterable<E> elements, final LeafListSchemaNode schema) {
41         final int size = Iterables.size(elements);
42         Preconditions.checkArgument(size == 1, "Xml elements mapped to leaf node illegal count: %s", size);
43
44         final E e = elements.iterator().next();
45         Object value = parseLeafListEntry(e,schema);
46
47         NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafEntryBuilder = Builders
48                 .leafSetEntryBuilder(schema);
49         leafEntryBuilder.withAttributes(getAttributes(e));
50         leafEntryBuilder.withValue(value);
51
52         final BuildingStrategy rawBuildingStrat = buildingStrategy;
53         return (LeafSetEntryNode<?>) rawBuildingStrat.build(leafEntryBuilder);
54     }
55
56     @Override
57     public BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> getBuildingStrategy() {
58         return buildingStrategy;
59     }
60
61     /**
62      *
63      * Parse the inner value of a LeafSetEntryNode from element of type E.
64      *
65      * @param element to be parsed
66      * @param schema schema for leaf-list
67      * @return parsed element as an Object
68      */
69     protected abstract Object parseLeafListEntry(E element, LeafListSchemaNode schema);
70
71     /**
72      *
73      * @param e element to be parsed
74      * @return attributes mapped to QNames
75      */
76     protected abstract Map<QName, String> getAttributes(E e);
77
78     public static class SimpleLeafSetEntryBuildingStrategy implements BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> {
79
80         @Override
81         public LeafSetEntryNode<?> build(final NormalizedNodeBuilder<NodeWithValue, ?, LeafSetEntryNode<?>> builder) {
82             return builder.build();
83         }
84
85         @Override
86         public void prepareAttributes(final Map<QName, String> attributes, final NormalizedNodeBuilder<NodeWithValue, ?, LeafSetEntryNode<?>> containerBuilder) {
87         }
88     }
89 }