2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.concepts;
24 import java.util.NavigableMap;
26 import java.util.TreeSet;
28 import org.onap.policy.common.utils.validation.Assertions;
31 * Implements concept getting from navigable maps.
33 * @author Liam Fallon (liam.fallon@ericsson.com)
34 * @param <C> the type of concept on which the interface implementation is applied.
36 public class AxConceptGetterImpl<C> implements AxConceptGetter<C> {
38 // The map from which to get concepts
39 private final NavigableMap<AxArtifactKey, C> conceptMap;
42 * Constructor that sets the concept map on which the getter methods in the interface will operate..
44 * @param conceptMap the concept map on which the method will operate
46 public AxConceptGetterImpl(final NavigableMap<AxArtifactKey, C> conceptMap) {
47 this.conceptMap = conceptMap;
54 public C get(final AxArtifactKey conceptKey) {
55 return conceptMap.get(conceptKey);
62 public C get(final String conceptKeyName) {
63 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
65 // The very fist key that could have this name
66 final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
68 // Check if we found a key for our name
69 AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
70 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
74 // Look for higher versions of the key
76 final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
77 if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
84 return conceptMap.get(foundKey);
91 public C get(final String conceptKeyName, final String conceptKeyVersion) {
92 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
94 if (conceptKeyVersion != null) {
95 return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
97 return this.get(conceptKeyName);
105 public Set<C> getAll(final String conceptKeyName) {
106 return getAll(conceptKeyName, null);
113 public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
114 final Set<C> returnSet = new TreeSet<>();
116 if (conceptKeyName == null) {
117 returnSet.addAll(conceptMap.values());
121 // The very fist key that could have this name
122 final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
123 if (conceptKeyVersion != null) {
124 lowestArtifactKey.setVersion(conceptKeyVersion);
127 // Check if we found a key for our name
128 AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
129 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
132 returnSet.add(conceptMap.get(foundKey));
134 // Look for higher versions of the key
136 foundKey = conceptMap.higherKey(foundKey);
137 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
140 returnSet.add(conceptMap.get(foundKey));