Changes for Checkstyle 8.32
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / TypedOperator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
22
23 import java.util.Map;
24 import lombok.Getter;
25 import org.onap.policy.controlloop.actorserviceprovider.Operation;
26 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
27
28 /**
29  * Operator with typed parameter information.
30  *
31  * @param <C> type of configuration data
32  * @param <T> type of operation that the operator creates
33  */
34 public abstract class TypedOperator<C, T extends Operation> extends OperatorPartial {
35
36     /**
37      * Function to make an operation.
38      */
39     private final OperationMaker<C, T> operationMaker;
40
41     /**
42      * Current configuration. This is set by {@link #doConfigure(Map)}.
43      */
44     @Getter
45     private C currentConfig;
46
47
48     /**
49      * Constructs the object.
50      *
51      * @param actorName name of the actor with which this operator is associated
52      * @param name operation name
53      */
54     protected TypedOperator(String actorName, String name) {
55         this(actorName, name, null);
56     }
57
58     /**
59      * Constructs the object.
60      *
61      * @param actorName name of the actor with which this operator is associated
62      * @param name operation name
63      * @param operationMaker function to make an operation
64      */
65     public TypedOperator(String actorName, String name, OperationMaker<C, T> operationMaker) {
66         super(actorName, name);
67         this.operationMaker = operationMaker;
68     }
69
70     /**
71      * Translates the parameters, saving the relevant configuration data.
72      */
73     @Override
74     protected void doConfigure(Map<String, Object> parameters) {
75         currentConfig = makeConfiguration(parameters);
76     }
77
78     /**
79      * Makes a new configuration using the specified parameters.
80      *
81      * @param parameters operator parameters
82      * @return a new configuration
83      */
84     protected abstract C makeConfiguration(Map<String, Object> parameters);
85
86     @Override
87     public T buildOperation(ControlLoopOperationParams params) {
88         if (operationMaker == null) {
89             throw new UnsupportedOperationException("cannot make operation for " + getFullName());
90         }
91
92         verifyRunning();
93
94         return operationMaker.apply(params, currentConfig);
95     }
96 }