Fix odlparent-3.0.0 checkstyle issues
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / MissingSchemaForClassException.java
1 /*
2  * Copyright (c) 2015 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.mdsal.binding.dom.codec.impl;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
12 import org.opendaylight.yangtools.yang.binding.Augmentation;
13
14 /**
15  * Thrown when Java Binding class was used in data for which codec does not have schema.
16  *
17  * <p>
18  * By serialization /  deserialization of this exception {@link #getBindingClass()} will return null.
19  */
20 public final class MissingSchemaForClassException extends MissingSchemaException {
21
22     private static final long serialVersionUID = 1L;
23
24     private final transient Class<?> bindingClass;
25
26     private MissingSchemaForClassException(final Class<?> clz) {
27         super(String.format("Schema is not available for %s", clz));
28         this.bindingClass = Preconditions.checkNotNull(clz);
29     }
30
31     static MissingSchemaForClassException forClass(final Class<?> clz) {
32         return new MissingSchemaForClassException(clz);
33     }
34
35     public Class<?> getBindingClass() {
36         return bindingClass;
37     }
38
39     public static void check(final BindingRuntimeContext runtimeContext, final Class<?> bindingClass) {
40         final Object schema;
41         if (Augmentation.class.isAssignableFrom(bindingClass)) {
42             schema = runtimeContext.getAugmentationDefinition(bindingClass);
43         } else {
44             schema = runtimeContext.getSchemaDefinition(bindingClass);
45         }
46         if (schema == null) {
47             throw MissingSchemaForClassException.forClass(bindingClass);
48         }
49     }
50 }