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;
53 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.apex.core.basicmodel.
54 * concepts.AxArtifactKey)
57 public C get(final AxArtifactKey conceptKey) {
58 return conceptMap.get(conceptKey);
64 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String)
67 public C get(final String conceptKeyName) {
68 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
70 // The very fist key that could have this name
71 final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
73 // Check if we found a key for our name
74 AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
75 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
79 // Look for higher versions of the key
81 final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
82 if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
89 return conceptMap.get(foundKey);
95 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String, java.lang.String)
98 public C get(final String conceptKeyName, final String conceptKeyVersion) {
99 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
101 if (conceptKeyVersion != null) {
102 return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
104 return this.get(conceptKeyName);
111 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String)
114 public Set<C> getAll(final String conceptKeyName) {
115 return getAll(conceptKeyName, null);
121 * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String, java.lang.String)
124 public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
125 final Set<C> returnSet = new TreeSet<>();
127 if (conceptKeyName == null) {
128 returnSet.addAll(conceptMap.values());
132 // The very fist key that could have this name
133 final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
134 if (conceptKeyVersion != null) {
135 lowestArtifactKey.setVersion(conceptKeyVersion);
138 // Check if we found a key for our name
139 AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
140 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
143 returnSet.add(conceptMap.get(foundKey));
145 // Look for higher versions of the key
147 foundKey = conceptMap.higherKey(foundKey);
148 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
151 returnSet.add(conceptMap.get(foundKey));