3c186bbf85c50921a95f778ab897486ab4c8d26d
[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 org.onap.policy.common.utils.validation.Assertions;
28
29 /**
30  * Implements concept getting from navigable maps.
31  *
32  * @param <C> the type of concept on which the interface implementation is applied.
33  */
34 public class PfConceptGetterImpl<C> implements PfConceptGetter<C> {
35
36     // The map from which to get concepts
37     private final NavigableMap<PfConceptKey, C> conceptMap;
38
39     /**
40      * Constructor that sets the concept map on which the getter methods in the interface will operate..
41      *
42      * @param conceptMap the concept map on which the method will operate
43      */
44     public PfConceptGetterImpl(final NavigableMap<PfConceptKey, C> conceptMap) {
45         this.conceptMap = conceptMap;
46     }
47
48     @Override
49     public C get(final PfConceptKey conceptKey) {
50         return conceptMap.get(conceptKey);
51     }
52
53     @Override
54     public C get(final String conceptKeyName) {
55         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
56
57         // The very fist key that could have this name
58         final var lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
59
60         // Check if we found a key for our name
61         PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
62         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
63             return null;
64         }
65
66         // Look for higher versions of the key
67         do {
68             final PfConceptKey nextkey = conceptMap.higherKey(foundKey);
69             if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
70                 break;
71             }
72             foundKey = nextkey;
73         } while (true);
74
75         return conceptMap.get(foundKey);
76     }
77
78     @Override
79     public C get(final String conceptKeyName, final String conceptKeyVersion) {
80         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
81
82         if (conceptKeyVersion != null) {
83             return conceptMap.get(new PfConceptKey(conceptKeyName, conceptKeyVersion));
84         } else {
85             return this.get(conceptKeyName);
86         }
87     }
88
89     @Override
90     public Set<C> getAll(final String conceptKeyName) {
91         return getAll(conceptKeyName, null);
92     }
93
94     @Override
95     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
96         final Set<C> returnSet = new TreeSet<>();
97
98         if (conceptKeyName == null) {
99             returnSet.addAll(conceptMap.values());
100             return returnSet;
101         }
102
103         // The very fist key that could have this name
104         final var lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
105         if (conceptKeyVersion != null) {
106             lowestArtifactKey.setVersion(conceptKeyVersion);
107         }
108
109         // Check if we found a key for our name
110         PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
111         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
112             return returnSet;
113         }
114         returnSet.add(conceptMap.get(foundKey));
115
116         // Look for higher versions of the key
117         do {
118             foundKey = conceptMap.higherKey(foundKey);
119             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
120                 break;
121             }
122             returnSet.add(conceptMap.get(foundKey));
123         } while (true);
124
125         return returnSet;
126     }
127 }