2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
24 import java.util.HashMap;
26 import org.json.JSONObject;
28 public class DataTreeObject extends HashMap<String, DataTreeChildObject> {
30 private static final long serialVersionUID = 1L;
31 private final String parentKey;
32 private final String childKey;
34 public DataTreeObject createTreeByPath(String[] pathFilter) {
36 if (pathFilter != null && pathFilter.length > 0) {
37 for (String key : this.keySet()) {
38 if (key.equals(pathFilter[0])) {
39 DataTreeChildObject o = this.getChildElemByPath(this.get(key), slice(pathFilter, 1));
40 DataTreeObject r = new DataTreeObject(this.parentKey, this.childKey);
51 private DataTreeChildObject getChildElemByPath(DataTreeChildObject source, String[] pathFilter) {
53 if (pathFilter != null && pathFilter.length > 0) {
54 // for(String key:source..keySet()) {
55 // if(key.equals(pathFilter[0])){
56 // DataTreeChildObject o= this.getChildElemByPath(this.get(key),slice(pathFilter,1));
64 public DataTreeObject(String parentKey, String childKey) {
65 this.parentKey = parentKey;
66 this.childKey = childKey;
74 public void put(long treeLevel, String id, DataTreeChildObject data) {
75 for (DataTreeChildObject entry : this.values()) {
76 if (entry.putChild(treeLevel, id, data, this.parentKey, this.childKey)) {
86 public String toJSON() {
87 JSONObject o = new JSONObject();
88 for (Entry<String, DataTreeChildObject> entry : this.entrySet()) {
89 o.put(entry.getKey(), entry.getValue().toJSONObject());
97 public void removeUnmatchedPaths() {
98 for (DataTreeChildObject entry : this.values()) {
99 entry.removeUnmatchedPaths();
109 public void putIfNotExists(long treeLevel, String id, DataTreeChildObject data) {
110 for (DataTreeChildObject entry : this.values()) {
111 if (entry.putChildIfNotExists(treeLevel, id, data, this.parentKey, this.childKey)) {
121 public void putIfNotExists(String id, DataTreeChildObject data) {
122 if (!this.containsKey(id)) {
132 public static String[] slice(String[] source, int start) {
133 String[] r = new String[source.length - start];
134 for (int i = 0; i < r.length; i++) {
135 r[i] = source[i + start];