Bug 8163: getDataTreeChangeListenerExecutor() & DataBrokerTestModule
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / test / SchemaContextSingleton.java
1 /*
2  * Copyright (c) 2016 Red Hat, 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.controller.md.sal.binding.test;
9
10 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
11
12 /**
13  * {@link SchemaContext} singleton holder (static).
14  *
15  * <p>This is useful in scenarios such as unit tests, but not OSGi environments,
16  * where there is a flat classpath and thus really only one single
17  * SchemaContext.
18  *
19  * @author Michael Vorburger
20  */
21 public final class SchemaContextSingleton {
22
23     private static SchemaContext staticSchemaContext;
24
25     public static synchronized SchemaContext getSchemaContext(Supplier<SchemaContext> supplier) throws Exception {
26         if (staticSchemaContext == null) {
27             staticSchemaContext = supplier.get();
28         }
29         return staticSchemaContext;
30     }
31
32     private SchemaContextSingleton() { }
33
34     @FunctionalInterface
35     public interface Supplier<T> {
36         T get() throws Exception;
37     }
38 }