Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / service / ModelService.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.service;
23
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
28
29 /**
30  * The model service makes Apex models available to all classes in a JVM.
31  *
32  * <p>The reason for having a model service is to avoid having to pass concept and model definitions down long call
33  * chains in modules such as the Apex engine and editor. The model service makes the model and concept definitions
34  * available statically.
35  *
36  * <p>Note that the use of the model service means that only a single Apex model of a particular type may exist in Apex
37  * (particularly the engine) at any time. Of course the model in a JVM can be changed at any time provided all users of
38  * the model are stopped and restarted in an orderly manner.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public abstract class ModelService {
43     // The map holding the models
44     private static Map<Class<?>, AxConcept> modelMap = new ConcurrentHashMap<>();
45
46     /**
47      * This class is an abstract static class that cannot be extended.
48      */
49     private ModelService() {
50     }
51
52     /**
53      * Register a model with the model service.
54      *
55      * @param <M> the generic type
56      * @param modelClass the class of the model, used to index the model
57      * @param model The model
58      */
59     public static <M extends AxConcept> void registerModel(final Class<M> modelClass, final M model) {
60         modelMap.put(modelClass, model);
61     }
62
63     /**
64      * Remove a model from the model service.
65      *
66      * @param <M> the generic type
67      * @param modelClass the class of the model, used to index the model
68      */
69     public static <M extends AxConcept> void deregisterModel(final Class<M> modelClass) {
70         modelMap.remove(modelClass);
71     }
72
73     /**
74      * Get a model from the model service.
75      *
76      * @param <M> the generic type
77      * @param modelClass the class of the model, used to index the model
78      * @return The model
79      */
80     @SuppressWarnings("unchecked")
81     public static <M extends AxConcept> M getModel(final Class<M> modelClass) {
82         final M model = (M) modelMap.get(modelClass);
83
84         if (model == null) {
85             throw new ApexRuntimeException("Model for " + modelClass.getName() + " not found in model service");
86         }
87
88         return model;
89     }
90
91     /**
92      * Check if a model is defined on the model service.
93      *
94      * @param <M> the generic type
95      * @param modelClass the class of the model, used to index the model
96      * @return true if the model is defined
97      */
98     public static <M extends AxConcept> boolean existsModel(final Class<M> modelClass) {
99         return modelMap.get(modelClass) != null;
100     }
101
102     /**
103      * Clear all models in the model service.
104      */
105     public static void clear() {
106         modelMap.clear();
107     }
108 }