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
diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/transform/base/parser/LeafSetNodeBaseParser.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/transform/base/parser/LeafSetNodeBaseParser.java
new file mode 100644 (file)
index 0000000..37ff5c9
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.schema.transform.base.parser;
+
+import java.util.Collections;
+
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParser;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+
+/**
+ * Abstract(base) parser for LeafSetNodes, parses elements of type E.
+ *
+ * @param <E> type of elements to be parsed
+ */
+public abstract class LeafSetNodeBaseParser<E> implements
+        ToNormalizedNodeParser<E, LeafSetNode<?>, LeafListSchemaNode> {
+
+    @Override
+    public final LeafSetNode<?> parse(Iterable<E> childNodes, LeafListSchemaNode schema) {
+
+        ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafListBuilder = Builders.leafSetBuilder(schema);
+        for (E childNode : childNodes) {
+            LeafSetEntryNode<?> builtChild = getLeafSetEntryNodeBaseParser().parse(
+                    Collections.singletonList(childNode), schema);
+
+            // TODO: can we get rid of this cast/SuppressWarnings somehow?
+            @SuppressWarnings("unchecked")
+            final LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>) builtChild;
+            leafListBuilder.withChild(child);
+        }
+
+        return leafListBuilder.build();
+    }
+
+    /**
+     *
+     * @return parser for inner LeafSetEntryNodes used to parse every entry of LeafSetNode, might be the same instance in case its immutable
+     */
+    protected abstract ToNormalizedNodeParser<E, LeafSetEntryNode<?>, LeafListSchemaNode> getLeafSetEntryNodeBaseParser();
+}