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