Merge "Parsing and serialization of composite and simple node to normalized nodes"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / parser / MapNodeBaseParser.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 java.util.Collections;
11
12 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
14 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
16 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParser;
17 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
18
19 /**
20  * Abstract(base) parser for MapNodes, parses elements of type E.
21  *
22  * @param <E> type of elements to be parsed
23  */
24 public abstract class MapNodeBaseParser<E> implements ToNormalizedNodeParser<E, MapNode, ListSchemaNode> {
25
26     @Override
27     public final MapNode parse(Iterable<E> childNodes, ListSchemaNode schema) {
28         CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder = Builders.mapBuilder(schema);
29
30         for (E childNode : childNodes) {
31             MapEntryNode listChild = getMapEntryNodeParser().parse(Collections.singletonList(childNode), schema);
32             listBuilder.withChild(listChild);
33         }
34
35         return listBuilder.build();
36     }
37
38     /**
39      *
40      * @return parser for inner MapEntryNodes used to parse every entry of MapNode, might be the same instance in case its immutable
41      */
42     protected abstract ToNormalizedNodeParser<E, MapEntryNode, ListSchemaNode> getMapEntryNodeParser();
43
44 }