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;
27 import org.onap.policy.common.utils.validation.Assertions;
30 * Implements concept getting from navigable maps.
32 * @author Liam Fallon (liam.fallon@ericsson.com)
33 * @param <C> the type of concept on which the interface implementation is applied.
35 public class AxConceptGetterImpl<C> implements AxConceptGetter<C> {
37 // The map from which to get concepts
38 private final NavigableMap<AxArtifactKey, C> conceptMap;
41 * Constructor that sets the concept map on which the getter methods in the interface will operate..
43 * @param conceptMap the concept map on which the method will operate
45 public AxConceptGetterImpl(final NavigableMap<AxArtifactKey, C> conceptMap) {
46 this.conceptMap = conceptMap;
53 public C get(final AxArtifactKey conceptKey) {
54 return conceptMap.get(conceptKey);
61 public C get(final String conceptKeyName) {
62 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
64 // The very fist key that could have this name
65 final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
67 // Check if we found a key for our name
68 AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
69 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
73 // Look for higher versions of the key
75 final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
76 if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
82 return conceptMap.get(foundKey);
89 public C get(final String conceptKeyName, final String conceptKeyVersion) {
90 Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
92 if (conceptKeyVersion != null) {
93 return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
95 return this.get(conceptKeyName);
103 public Set<C> getAll(final String conceptKeyName) {
104 return getAll(conceptKeyName, null);
111 public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
112 final Set<C> returnSet = new TreeSet<>();
114 if (conceptKeyName == null) {
115 returnSet.addAll(conceptMap.values());
119 // The very fist key that could have this name
120 final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
121 if (conceptKeyVersion != null) {
122 lowestArtifactKey.setVersion(conceptKeyVersion);
125 // Check if we found a key for our name
126 AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
127 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
130 returnSet.add(conceptMap.get(foundKey));
132 // Look for higher versions of the key
134 foundKey = conceptMap.higherKey(foundKey);
135 if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
138 returnSet.add(conceptMap.get(foundKey));