fa1bbb4e782ac17f53bbb3cc91e302a994f366c6
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfConceptGetterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.util.NavigableMap;
25 import java.util.Set;
26 import java.util.TreeSet;
27 import lombok.AllArgsConstructor;
28 import org.onap.policy.common.utils.validation.Assertions;
29
30 /**
31  * Implements concept getting from navigable maps.
32  *
33  * @param <C> the type of concept on which the interface implementation is applied.
34  */
35 @AllArgsConstructor
36 public class PfConceptGetterImpl<C> implements PfConceptGetter<C> {
37
38     // The map from which to get concepts
39     private final NavigableMap<PfConceptKey, C> conceptMap;
40
41
42     @Override
43     public C get(final PfConceptKey conceptKey) {
44         return conceptMap.get(conceptKey);
45     }
46
47     @Override
48     public C get(final String conceptKeyName) {
49         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
50
51         // The very fist key that could have this name
52         final var lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
53
54         // Check if we found a key for our name
55         PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
56         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
57             return null;
58         }
59
60         // Look for higher versions of the key
61         do {
62             final PfConceptKey nextkey = conceptMap.higherKey(foundKey);
63             if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
64                 break;
65             }
66             foundKey = nextkey;
67         } while (true);
68
69         return conceptMap.get(foundKey);
70     }
71
72     @Override
73     public C get(final String conceptKeyName, final String conceptKeyVersion) {
74         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
75
76         if (conceptKeyVersion != null) {
77             return conceptMap.get(new PfConceptKey(conceptKeyName, conceptKeyVersion));
78         } else {
79             return this.get(conceptKeyName);
80         }
81     }
82
83     @Override
84     public Set<C> getAll(final String conceptKeyName) {
85         return getAll(conceptKeyName, null);
86     }
87
88     @Override
89     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
90         final Set<C> returnSet = new TreeSet<>();
91
92         if (conceptKeyName == null) {
93             returnSet.addAll(conceptMap.values());
94             return returnSet;
95         }
96
97         // The very fist key that could have this name
98         final var lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
99         if (conceptKeyVersion != null) {
100             lowestArtifactKey.setVersion(conceptKeyVersion);
101         }
102
103         // Check if we found a key for our name
104         PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
105         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
106             return returnSet;
107         }
108         returnSet.add(conceptMap.get(foundKey));
109
110         // Look for higher versions of the key
111         do {
112             foundKey = conceptMap.higherKey(foundKey);
113             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
114                 break;
115             }
116             returnSet.add(conceptMap.get(foundKey));
117         } while (true);
118
119         return returnSet;
120     }
121 }