Changes for checkstyle 8.32
[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 import org.onap.policy.common.utils.validation.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      * {@inheritDoc}.
51      */
52     @Override
53     public C get(final AxArtifactKey conceptKey) {
54         return conceptMap.get(conceptKey);
55     }
56
57     /**
58      * {@inheritDoc}.
59      */
60     @Override
61     public C get(final String conceptKeyName) {
62         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
63
64         // The very fist key that could have this name
65         final AxArtifactKey lowestArtifactKey = new AxArtifactKey(conceptKeyName, "0.0.1");
66
67         // Check if we found a key for our name
68         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
69         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
70             return null;
71         }
72
73         // Look for higher versions of the key
74         do {
75             final AxArtifactKey nextkey = conceptMap.higherKey(foundKey);
76             if (nextkey == null || !nextkey.getName().equals(conceptKeyName)) {
77                 break;
78             }
79             foundKey = nextkey;
80         } while (true);
81
82         return conceptMap.get(foundKey);
83     }
84
85     /**
86      * {@inheritDoc}.
87      */
88     @Override
89     public C get(final String conceptKeyName, final String conceptKeyVersion) {
90         Assertions.argumentNotNull(conceptKeyName, "conceptKeyName may not be null");
91
92         if (conceptKeyVersion != null) {
93             return conceptMap.get(new AxArtifactKey(conceptKeyName, conceptKeyVersion));
94         } else {
95             return this.get(conceptKeyName);
96         }
97     }
98
99     /**
100      * {@inheritDoc}.
101      */
102     @Override
103     public Set<C> getAll(final String conceptKeyName) {
104         return getAll(conceptKeyName, null);
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
112         final Set<C> returnSet = new TreeSet<>();
113
114         if (conceptKeyName == null) {
115             returnSet.addAll(conceptMap.values());
116             return returnSet;
117         }
118
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);
123         }
124
125         // Check if we found a key for our name
126         AxArtifactKey foundKey = conceptMap.ceilingKey(lowestArtifactKey);
127         if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
128             return returnSet;
129         }
130         returnSet.add(conceptMap.get(foundKey));
131
132         // Look for higher versions of the key
133         do {
134             foundKey = conceptMap.higherKey(foundKey);
135             if (foundKey == null || !foundKey.getName().equals(conceptKeyName)) {
136                 break;
137             }
138             returnSet.add(conceptMap.get(foundKey));
139         } while (true);
140
141         return returnSet;
142     }
143 }