34c704c7b18c521b3d3481840791784f44a951fa
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / concepts / AxConceptGetterImpl.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.concepts;
23
24 import java.util.NavigableMap;
25 import java.util.Set;
26 import java.util.TreeSet;
27
28 import org.onap.policy.common.utils.validation.Assertions;
29
30 /**
31  * Implements concept getting from navigable maps.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  * @param <C> the type of concept on which the interface implementation is applied.
35  */
36 public class AxConceptGetterImpl<C> implements AxConceptGetter<C> {
37
38     // The map from which to get concepts
39     private final NavigableMap<AxArtifactKey, C> conceptMap;
40
41     /**
42      * Constructor that sets the concept map on which the getter methods in the interface will operate..
43      *
44      * @param conceptMap the concept map on which the method will operate
45      */
46     public AxConceptGetterImpl(final NavigableMap<AxArtifactKey, C> conceptMap) {
47         this.conceptMap = conceptMap;
48     }
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public C get(final AxArtifactKey conceptKey) {
55         return conceptMap.get(conceptKey);
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public C get(final String conceptKeyName) {
63         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
64
65         // The very fist key that could have this name
66         final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
67
68         // Check if we found a key for our name
69         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
70         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
71             return null;
72         }
73
74         // Look for higher versions of the key
75         do {
76             final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
77             if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
78                 break;
79             }
80             foundKey = nextkey;
81         }
82         while (true);
83
84         return conceptMap.get(foundKey);
85     }
86
87     /**
88      * {@inheritDoc}.
89      */
90     @Override
91     public C get(final String conceptKeyName, final String conceptKeyVersion) {
92         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
93
94         if (conceptKeyVersion != null) {
95             return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
96         } else {
97             return this.get(conceptKeyName);
98         }
99     }
100
101     /**
102      * {@inheritDoc}.
103      */
104     @Override
105     public Set<C> getAll(final String conceptKeyName) {
106         return getAll(conceptKeyName, null);
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
114         final Set<C> returnSet = new TreeSet<>();
115
116         if (conceptKeyName == null) {
117             returnSet.addAll(conceptMap.values());
118             return returnSet;
119         }
120
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);
125         }
126
127         // Check if we found a key for our name
128         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
129         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
130             return returnSet;
131         }
132         returnSet.add(conceptMap.get(foundKey));
133
134         // Look for higher versions of the key
135         do {
136             foundKey = conceptMap.higherKey(foundKey);
137             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
138                 break;
139             }
140             returnSet.add(conceptMap.get(foundKey));
141         }
142         while (true);
143
144         return returnSet;
145     }
146 }