[OOM-CERT-SERVICE] Fix sonar and checkstyle issues, code cleanup
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / certification / model / CertificateData.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.model;
22
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.stream.Collectors;
29 import org.bouncycastle.asn1.x500.X500Name;
30 import org.bouncycastle.asn1.x509.GeneralName;
31
32 public class CertificateData {
33
34     private final X500Name subject;
35     private final List<GeneralName> sortedSans;
36
37     public CertificateData(X500Name subject, GeneralName[] sans) {
38         this.subject = subject;
39         this.sortedSans = sans != null ? getSortedSansList(sans) : Collections.emptyList();
40     }
41
42     public X500Name getSubject() {
43         return subject;
44     }
45
46     public GeneralName[] getSortedSans() {
47         return sortedSans.toArray(new GeneralName[0]);
48     }
49
50     @Override
51     public boolean equals(Object obj) {
52         if (this == obj) {
53             return true;
54         }
55         if (obj == null || getClass() != obj.getClass()) {
56             return false;
57         }
58         CertificateData that = (CertificateData) obj;
59         return Objects.equals(subject, that.subject) && Objects.equals(sortedSans, that.sortedSans);
60     }
61
62     @Override
63     public int hashCode() {
64         return Objects.hash(subject, sortedSans);
65     }
66
67     private List<GeneralName> getSortedSansList(GeneralName[] sans) {
68         return Arrays.stream(sans).sorted(Comparator.comparing(GeneralName::toString))
69             .collect(Collectors.toUnmodifiableList());
70     }
71
72 }