Switch to Objects.requireNonNull
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / serializer / ChoiceDispatchSerializer.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.codec.impl.serializer;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.io.IOException;
14 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
15 import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
16 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
17 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
18 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerImplementation;
20 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerRegistry;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Dispatch serializer, which emit DOM data from Binding v2 via stream writer.
26  */
27 @Beta
28 public final class ChoiceDispatchSerializer implements TreeNodeSerializerImplementation {
29
30     private static final Logger LOG = LoggerFactory.getLogger(ChoiceDispatchSerializer.class);
31
32     @SuppressWarnings("rawtypes")
33     private final Class choiceClass;
34
35     @SuppressWarnings("rawtypes")
36     private ChoiceDispatchSerializer(final Class choiceClass) {
37         this.choiceClass = requireNonNull(choiceClass);
38     }
39
40     /**
41      * Prepare instance of serializer from choice class.
42      *
43      * @param choiceClass
44      *            - class choice
45      * @return instance of serializer
46      */
47     public static ChoiceDispatchSerializer from(final Class<? extends Instantiable<?>> choiceClass) {
48         return new ChoiceDispatchSerializer(choiceClass);
49     }
50
51     @SuppressWarnings("unchecked")
52     @Override
53     public void serialize(final TreeNodeSerializerRegistry reg, final TreeNode obj,
54             final BindingStreamEventWriter stream) throws IOException {
55         @SuppressWarnings("rawtypes")
56         final Class cazeClass = ((Instantiable<?>) obj).implementedInterface();
57         stream.startChoiceNode(new Item<>(choiceClass), BindingStreamEventWriter.UNKNOWN_SIZE);
58         final TreeNodeSerializer caseSerializer = reg.getSerializer(cazeClass);
59         if (caseSerializer != null) {
60             caseSerializer.serialize(obj, stream);
61         } else {
62             LOG.warn("No serializer for case {} is available in registry {}", cazeClass, reg);
63         }
64         stream.endNode();
65     }
66 }