dba2c5db5c4dad7f58e7c52649ee732dbbe1f1a8
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / serializer / MapNodeBaseSerializer.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.serializer;
9
10 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
11 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
12 import org.opendaylight.yangtools.yang.data.impl.schema.transform.FromNormalizedNodeSerializer;
13 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
14
15 import com.google.common.base.Function;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Iterables;
18
19 /**
20  * Abstract(base) serializer for MapNodes, serializes elements of type E.
21  *
22  * @param <E> type of serialized elements
23  */
24 public abstract class MapNodeBaseSerializer<E> implements FromNormalizedNodeSerializer<E, MapNode, ListSchemaNode> {
25
26     @Override
27     public final Iterable<E> serialize(final ListSchemaNode schema, final MapNode node) {
28         return Iterables.concat(Iterables.transform(node.getValue(), new Function<MapEntryNode, Iterable<E>>() {
29             @Override
30             public Iterable<E> apply(MapEntryNode input) {
31                 final Iterable<E> serializedChild = getMapEntryNodeDomSerializer().serialize(schema, input);
32                 final int size = Iterables.size(serializedChild);
33
34                 Preconditions.checkState(size == 1,
35                         "Unexpected count of entries  for list serialized from: %s, should be 1, was: %s",
36                         input, size);
37                 return serializedChild;
38             }
39         }));
40     }
41
42     /**
43      *
44      * @return serializer for inner MapEntryNodes used to serialize every entry of MapNode, might be the same instance in case its immutable
45      */
46     protected abstract FromNormalizedNodeSerializer<E, MapEntryNode, ListSchemaNode> getMapEntryNodeDomSerializer();
47 }