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