Neon SR1 upgrade
[appc.git] / appc-core / appc-common-bundle / java / org / onap / appc / util / PathContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.util;
25
26
27 import java.util.Collections;
28 import java.util.LinkedHashMap;
29 import java.util.LinkedList;
30 import java.util.Map;
31
32 class PathContext {
33
34         private StringBuilder path = new StringBuilder(128);
35
36         private LinkedList<Integer> indexes = new LinkedList<>();
37         private Map<String, String> entries = new LinkedHashMap<>();
38         private int offset = 0;
39
40         private final String delimiter;
41
42         PathContext() {
43                 this(".");
44         }
45
46         PathContext(String delimiter) {
47                 this.delimiter = delimiter;
48         }
49
50         private void push(String elem, boolean delimit) {
51                 if (elem == null) {
52                         throw new IllegalArgumentException();
53                 }
54
55                 int length = elem.length();
56
57                 if (delimit && !indexes.isEmpty()) {
58                         path.append(delimiter);
59                         length += delimiter.length();
60                 }
61
62                 path.append(elem);
63                 offset += length;
64                 indexes.addLast(Integer.valueOf(length));
65         }
66
67         private void pop() {
68                 if (indexes.isEmpty()) {
69                         throw new IllegalStateException();
70                 }
71                 offset -= indexes.removeLast();
72                 path.setLength(offset);
73         }
74
75         void pushToken(String token) {
76                 push(token, true);
77         }
78
79         void popToken() {
80                 pop();
81         }
82
83         void pushModifier(String modifier) {
84                 push(modifier, false);
85         }
86
87         void popModifier() {
88                 pop();
89         }
90
91         String getPath() {
92                 return path.substring(0, offset);
93         }
94
95         void entry(String name, String value) {
96                 entries.put(name, value);
97         }
98
99         Map<String, String> entries() {
100                 return Collections.unmodifiableMap(entries);
101         }
102 }