Change nexus values to properties
[appc.git] / appc-common / src / main / java / org / openecomp / appc / util / PathContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.openecomp.appc.util;
23
24
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.LinkedList;
28 import java.util.Map;
29
30 class PathContext {
31
32         private StringBuilder path = new StringBuilder(128);
33
34         private LinkedList<Integer> indexes = new LinkedList<>();
35         private Map<String, String> entries = new LinkedHashMap<>();
36         private int offset = 0;
37
38         private final String delimiter;
39
40         PathContext() {
41                 this(".");
42         }
43
44         PathContext(String delimiter) {
45                 this.delimiter = delimiter;
46         }
47
48         private void push(String elem, boolean delimit) {
49                 if (elem == null) {
50                         throw new IllegalArgumentException();
51                 }
52
53                 int length = elem.length();
54
55                 if (delimit && !indexes.isEmpty()) {
56                         path.append(delimiter);
57                         length += delimiter.length();
58                 }
59
60                 path.append(elem);
61                 offset += length;
62                 indexes.addLast(Integer.valueOf(length));
63         }
64
65         private void pop() {
66                 if (indexes.isEmpty()) {
67                         throw new IllegalStateException();
68                 }
69                 offset -= indexes.removeLast();
70                 path.setLength(offset);
71         }
72
73         void pushToken(String token) {
74                 push(token, true);
75         }
76
77         void popToken() {
78                 pop();
79         }
80
81         void pushModifier(String modifier) {
82                 push(modifier, false);
83         }
84
85         void popModifier() {
86                 pop();
87         }
88
89         String getPath() {
90                 return path.substring(0, offset);
91         }
92
93         void entry(String name, String value) {
94                 entries.put(name, value);
95         }
96
97         Map<String, String> entries() {
98                 return Collections.unmodifiableMap(entries);
99         }
100 }