866de80533f9c8c2537df7401649bf95d6f87138
[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         }
86         while (true);
87
88         return conceptMap.get(foundKey);
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#get(java.lang. String, java.lang.String)
95      */
96     @Override
97     public C get(final String conceptKeyName, final String conceptKeyVersion) {
98         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
99
100         if (conceptKeyVersion != null) {
101             return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
102         } else {
103             return this.get(conceptKeyName);
104         }
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String)
111      */
112     @Override
113     public Set<C> getAll(final String conceptKeyName) {
114         return getAll(conceptKeyName, null);
115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see org.onap.policy.apex.core.basicmodel.concepts.AxConceptGetter#getAll(java. lang.String, java.lang.String)
121      */
122     @Override
123     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
124         final Set<C> returnSet = new TreeSet<>();
125
126         if (conceptKeyName == null) {
127             returnSet.addAll(conceptMap.values());
128             return returnSet;
129         }
130
131         // The very fist key that could have this name
132         final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
133         if (conceptKeyVersion != null) {
134             lowestArtifactKey.setVersion(conceptKeyVersion);
135         }
136
137         // Check if we found a key for our name
138         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
139         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
140             return returnSet;
141         }
142         returnSet.add(conceptMap.get(foundKey));
143
144         // Look for higher versions of the key
145         do {
146             foundKey = conceptMap.higherKey(foundKey);
147             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
148                 break;
149             }
150             returnSet.add(conceptMap.get(foundKey));
151         }
152         while (true);
153
154         return returnSet;
155     }
156 }