Abstract infrastructure for normalized nodes translation.
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / parser / LeafSetNodeBaseParser.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.LeafSetEntryNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
14 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
16 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParser;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18
19 /**
20  * Abstract(base) parser for LeafSetNodes, parses elements of type E.
21  *
22  * @param <E> type of elements to be parsed
23  */
24 public abstract class LeafSetNodeBaseParser<E> implements
25         ToNormalizedNodeParser<E, LeafSetNode<?>, LeafListSchemaNode> {
26
27     @Override
28     public final LeafSetNode<?> parse(Iterable<E> childNodes, LeafListSchemaNode schema) {
29
30         ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafListBuilder = Builders.leafSetBuilder(schema);
31         for (E childNode : childNodes) {
32             LeafSetEntryNode<?> builtChild = getLeafSetEntryNodeBaseParser().parse(
33                     Collections.singletonList(childNode), schema);
34
35             // TODO: can we get rid of this cast/SuppressWarnings somehow?
36             @SuppressWarnings("unchecked")
37             final LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>) builtChild;
38             leafListBuilder.withChild(child);
39         }
40
41         return leafListBuilder.build();
42     }
43
44     /**
45      *
46      * @return parser for inner LeafSetEntryNodes used to parse every entry of LeafSetNode, might be the same instance in case its immutable
47      */
48     protected abstract ToNormalizedNodeParser<E, LeafSetEntryNode<?>, LeafListSchemaNode> getLeafSetEntryNodeBaseParser();
49 }