49dd8cf96fb9feca371f0c7ba2ef1c5fa5f36754
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / IncorrectNestingException.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 javax.annotation.Nonnull;
11 import javax.annotation.Nullable;
12
13 /**
14  * Thrown where incorrect nesting of data structures was detected
15  * and was caused by user.
16  */
17 public class IncorrectNestingException extends IllegalArgumentException {
18
19     private static final long serialVersionUID = 1L;
20
21     protected IncorrectNestingException(final String message) {
22         super(message);
23     }
24
25     public static IncorrectNestingException create(final String message, final Object... args) {
26         return new IncorrectNestingException(String.format(message, args));
27     }
28
29     public static void check(final boolean check, final String message, final Object... args) {
30         if(!check) {
31             throw IncorrectNestingException.create(message, args);
32         }
33     }
34
35     @Nonnull
36     public static <V> V checkNonNull(@Nullable final V nullable, final String message, final Object... args) {
37         if(nullable != null) {
38             return nullable;
39         }
40         throw IncorrectNestingException.create(message, args);
41     }
42 }