34459067f1a400b6df380946b7064c0f523265fd
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / Template.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update;
25
26 import java.util.ArrayList;
27
28 public class Template {
29
30     /**
31      * name parameter is used as "key", in the LinkedHashMap of Templates.
32      */
33     private String name;
34     private ArrayList<String> fields;
35
36     public Template(String name) {
37         this.name = name;
38         this.fields = new ArrayList<String>();
39     }
40
41     public Template(String name, ArrayList<String> fields) {
42         this.name = name;
43         this.fields = fields;
44     }
45
46     public String getName() {
47         return name;
48     }
49
50     public void setName(String name) {
51         this.name = name;
52     }
53
54     public ArrayList<String> getFields() {
55         return fields;
56     }
57
58     public void setFields(ArrayList<String> fields) {
59         this.fields = fields;
60     }
61
62     public boolean hasFields(String name) {
63         return fields.contains(name);
64     }
65
66     public void addField(String field) {
67         fields.add(field);
68     }
69
70     public void removeField(String field) {
71         fields.remove(field);
72     }
73
74     /**
75      * Compare two templates : size and their contents.
76      *
77      * @param template the template
78      * @return a boolean
79      */
80     public boolean checkFields(Template template) {
81
82         boolean duplicateFields = false;
83         if (template.getFields().size() == this.getFields().size()) {
84             int countMatchingFields = 0;
85             //loop each component of first
86             for (String templateField : template.getFields()) {
87                 //if component.key is present in the second
88                 if (this.getFields().contains(templateField)) {
89                     countMatchingFields++;
90                 }
91             }
92
93             if (template.getFields().size() == countMatchingFields) {
94                 duplicateFields = true;
95             }
96         }
97         return duplicateFields;
98     }
99
100     @Override
101     public String toString() {
102         return " fields : " + fields;
103     }
104 }