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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.util.NavigableMap;
25 import java.util.TreeSet;
27 import org.onap.policy.common.utils.validation.Assertions;
30 * Implements concept getting from navigable maps.
32 * @param <C> the type of concept on which the interface implementation is applied.
34 public class PfConceptGetterImpl<C> implements PfConceptGetter<C> {
36 // The map from which to get concepts
37 private final NavigableMap<PfConceptKey, C> conceptMap;
40 * Constructor that sets the concept map on which the getter methods in the interface will operate..
42 * @param conceptMap the concept map on which the method will operate
44 public PfConceptGetterImpl(final NavigableMap<PfConceptKey, C> conceptMap) {
45 this.conceptMap = conceptMap;
49 public C get(final PfConceptKey conceptKey) {
50 return conceptMap.get(conceptKey);
54 public C get(final String conceptKeyName) {
55 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
57 // The very fist key that could have this name
58 final PfConceptKey lowestArtifactKey = new PfConceptKey(conceptKeyName, "0.0.1");
60 // Check if we found a key for our name
61 PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
62 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
66 // Look for higher versions of the key
68 final PfConceptKey nextkey = conceptMap.higherKey(foundKey);
69 if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
76 return conceptMap.get(foundKey);
80 public C get(final String conceptKeyName, final String conceptKeyVersion) {
81 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
83 if (conceptKeyVersion != null) {
84 return conceptMap.get(new PfConceptKey(conceptKeyName, conceptKeyVersion));
86 return this.get(conceptKeyName);
91 public Set<C> getAll(final String conceptKeyName) {
92 return getAll(conceptKeyName, null);
96 public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
97 final Set<C> returnSet = new TreeSet<>();
99 if (conceptKeyName == null) {
100 returnSet.addAll(conceptMap.values());
104 // The very fist key that could have this name
105 final PfConceptKey lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
106 if (conceptKeyVersion != null) {
107 lowestArtifactKey.setVersion(conceptKeyVersion);
110 // Check if we found a key for our name
111 PfConceptKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
112 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
115 returnSet.add(conceptMap.get(foundKey));
117 // Look for higher versions of the key
119 foundKey = conceptMap.higherKey(foundKey);
120 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
123 returnSet.add(conceptMap.get(foundKey));