Optimize the areas where its creating extra memory
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / nodes / CaseFormatStore.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 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 package org.onap.aai.nodes;
21
22 import com.google.common.base.CaseFormat;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.NodeList;
25
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Optional;
29
30 /**
31  * CaseFormatStore stores the converted strings from
32  * lower hyphen (example-object) to lower camel case (exampleObject)
33  * so it avoids the creation of the object for every single request
34  * and cause an issue with taking too much memory just for the conversion
35  */
36 public class CaseFormatStore {
37
38     private final Map<String, String> lowerHyphenToLowerCamel = new HashMap<>();
39     private final Map<String, String> lowerHyphenToUpperCamel = new HashMap<>();
40     private final Map<String, String> lowerCamelToLowerHyphen = new HashMap<>();
41     private final Map<String, String> upperCamelToLowerHyphen = new HashMap<>();
42
43     CaseFormatStore(){}
44
45     /**
46      * Parses the document and creates a lower camel case string
47      * upper camel string, lower hyphen and lower camel case
48      *
49      * @param doc   Takes an xml document and adds it to the hash maps as appropriate
50      */
51     void parse(Document doc){
52
53         // Get the xml-root-element and add those nodes
54         // with the attribute name and it to the hashmaps
55         // For the attribute with name, it is going to be lower-hyphen
56         // If the attribute is javaAttribute then it will be lower camel case
57         NodeList list = doc.getElementsByTagName("xml-root-element");
58         addCaseFormatForNodesAndProperties(list, "name");
59
60         list = doc.getElementsByTagName("xml-element");
61         addCaseFormatForNodesAndProperties(list, "java-attribute");
62
63         list = doc.getElementsByTagName("xml-any-element");
64         addCaseFormatForNodesAndProperties(list, "java-attribute");
65     }
66
67     private void addCaseFormatForNodesAndProperties(NodeList list, String attributeName) {
68         for (int i = 0; i < list.getLength(); i++) {
69
70             String lowerCamel = null;
71             String lowerHyphen = null;
72
73             if ("java-attribute".equals(attributeName)) {
74                 lowerCamel = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
75                 lowerHyphen = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, lowerCamel);
76             } else {
77                 lowerHyphen = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
78                 lowerCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, lowerHyphen);
79             }
80
81             String upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, lowerHyphen);
82             lowerHyphenToLowerCamel.put(lowerHyphen, lowerCamel);
83             lowerHyphenToUpperCamel.put(lowerHyphen, upperCamel);
84             upperCamelToLowerHyphen.put(upperCamel, lowerHyphen);
85             lowerCamelToLowerHyphen.put(lowerCamel, lowerHyphen);
86         }
87     }
88
89     public Optional<String> fromLowerHyphenToLowerCamel(String value){
90         return Optional.ofNullable(lowerHyphenToLowerCamel.get(value));
91     }
92
93     public Optional<String> fromLowerHyphenToUpperCamel(String value){
94         return Optional.ofNullable(lowerHyphenToUpperCamel.get(value));
95     }
96
97     public Optional<String> fromUpperCamelToLowerHyphen(String value){
98         return Optional.ofNullable(upperCamelToLowerHyphen.get(value));
99     }
100
101     public Optional<String> fromLowerCamelToLowerHyphen(String value){
102         return Optional.ofNullable(lowerCamelToLowerHyphen.get(value));
103     }
104
105 }