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