e9c878346a7fddb27b58e05aeebd2e52cc29a441
[policy/apex-pdp.git] /
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      * (non-Javadoc)
52      *
53      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.apex.core.basicmodel.
54      * concepts.AxArtifactKey)
55      */
56     @Override
57     public C get(final AxArtifactKey conceptKey) {
58         return conceptMap.get(conceptKey);
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String)
65      */
66     @Override
67     public C get(final String conceptKeyName) {
68         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
69
70         // The very fist key that could have this name
71         final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
72
73         // Check if we found a key for our name
74         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
75         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
76             return null;
77         }
78
79         // Look for higher versions of the key
80         do {
81             final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
82             if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
83                 break;
84             }
85             foundKey = nextkey;
86         }
87         while (true);
88
89         return conceptMap.get(foundKey);
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String, java.lang.String)
96      */
97     @Override
98     public C get(final String conceptKeyName, final String conceptKeyVersion) {
99         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
100
101         if (conceptKeyVersion != null) {
102             return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
103         } else {
104             return this.get(conceptKeyName);
105         }
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String)
112      */
113     @Override
114     public Set<C> getAll(final String conceptKeyName) {
115         return getAll(conceptKeyName, null);
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String, java.lang.String)
122      */
123     @Override
124     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
125         final Set<C> returnSet = new TreeSet<>();
126
127         if (conceptKeyName == null) {
128             returnSet.addAll(conceptMap.values());
129             return returnSet;
130         }
131
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);
136         }
137
138         // Check if we found a key for our name
139         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
140         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
141             return returnSet;
142         }
143         returnSet.add(conceptMap.get(foundKey));
144
145         // Look for higher versions of the key
146         do {
147             foundKey = conceptMap.higherKey(foundKey);
148             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
149                 break;
150             }
151             returnSet.add(conceptMap.get(foundKey));
152         }
153         while (true);
154
155         return returnSet;
156     }
157 }