b4bff80c8a36f89520637b7e49b2c98355f420f7
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.concepts;
22
23 import java.util.NavigableMap;
24 import java.util.Set;
25 import java.util.TreeSet;
26
27 import org.onap.policy.apex.model.utilities.Assertions;
28
29 /**
30  * Implements concept getting from navigable maps.
31  *
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  * @param <C> the type of concept on which the interface implementation is applied.
34  */
35 public class AxConceptGetterImpl<C> implements AxConceptGetter<C> {
36
37     // The map from which to get concepts
38     private final NavigableMap<AxArtifactKey, C> conceptMap;
39
40     /**
41      * Constructor that sets the concept map on which the getter methods in the interface will operate..
42      *
43      * @param conceptMap the concept map on which the method will operate
44      */
45     public AxConceptGetterImpl(final NavigableMap<AxArtifactKey, C> conceptMap) {
46         this.conceptMap = conceptMap;
47     }
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.apex.core.basicmodel.
53      * concepts.AxArtifactKey)
54      */
55     @Override
56     public C get(final AxArtifactKey conceptKey) {
57         return conceptMap.get(conceptKey);
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String)
64      */
65     @Override
66     public C get(final String conceptKeyName) {
67         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
68
69         // The very fist key that could have this name
70         final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
71
72         // Check if we found a key for our name
73         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
74         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
75             return null;
76         }
77
78         // Look for higher versions of the key
79         do {
80             final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
81             if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
82                 break;
83             }
84             foundKey = nextkey;
85         } while (true);
86
87         return conceptMap.get(foundKey);
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String, java.lang.String)
94      */
95     @Override
96     public C get(final String conceptKeyName, final String conceptKeyVersion) {
97         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
98
99         if (conceptKeyVersion != null) {
100             return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
101         } else {
102             return this.get(conceptKeyName);
103         }
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String)
110      */
111     @Override
112     public Set<C> getAll(final String conceptKeyName) {
113         return getAll(conceptKeyName, null);
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String, java.lang.String)
120      */
121     @Override
122     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
123         final Set<C> returnSet = new TreeSet<>();
124
125         if (conceptKeyName == null) {
126             returnSet.addAll(conceptMap.values());
127             return returnSet;
128         }
129
130         // The very fist key that could have this name
131         final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
132         if (conceptKeyVersion != null) {
133             lowestArtifactKey.setVersion(conceptKeyVersion);
134         }
135
136         // Check if we found a key for our name
137         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
138         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
139             return returnSet;
140         }
141         returnSet.add(conceptMap.get(foundKey));
142
143         // Look for higher versions of the key
144         do {
145             foundKey = conceptMap.higherKey(foundKey);
146             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
147                 break;
148             }
149             returnSet.add(conceptMap.get(foundKey));
150         } while (true);
151
152         return returnSet;
153     }
154 }