Merge "Sonar Fixes - CadiAuthFilter.java"
[music.git] / src / main / java / org / onap / music / lockingservice / zookeeper / ZNodeName.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  * Modifications Copyright (c) 2018 IBM
8  * ===================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  * 
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.lockingservice.zookeeper;
26
27 import org.onap.music.eelf.logging.EELFLoggerDelegate;
28 import org.onap.music.eelf.logging.format.ErrorSeverity;
29 import org.onap.music.eelf.logging.format.ErrorTypes;
30
31 /**
32  * Represents an ephemeral znode name which has an ordered sequence number and can be sorted in
33  * order
34  *
35  */
36 class ZNodeName implements Comparable<ZNodeName> {
37     private final String name;
38     private String prefix;
39     private int sequence = -1;
40     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ZNodeName.class);
41     
42     public ZNodeName(String name) {
43         if (name == null) {
44             throw new NullPointerException("id cannot be null");
45         }
46         this.name = name;
47         this.prefix = name;
48         int idx = name.lastIndexOf('-');
49         if (idx >= 0) {
50             this.prefix = name.substring(0, idx);
51             try {
52                 this.sequence = Integer.parseInt(name.substring(idx + 1));
53                 // If an exception occurred we misdetected a sequence suffix,
54                 // so return -1.
55             } catch (NumberFormatException e) {
56                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Number format exception "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
57             } catch (ArrayIndexOutOfBoundsException e) {
58                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Array out of bounds for  "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
59             }
60         }
61     }
62
63     @Override
64     public String toString() {
65         return name;
66     }
67
68     @Override
69     public boolean equals(Object o) {
70         if (this == o)
71             return true;
72         if (o == null || getClass() != o.getClass())
73             return false;
74
75         ZNodeName sequence = (ZNodeName) o;
76
77         if (!name.equals(sequence.name))
78             return false;
79
80         return true;
81     }
82
83     @Override
84     public int hashCode() {
85         return name.hashCode() + 37;
86     }
87
88     public int compareTo(ZNodeName that) {
89         int answer = this.prefix.compareTo(that.prefix);
90         if (answer == 0) {
91             int s1 = this.sequence;
92             int s2 = that.sequence;
93             if (s1 == -1 && s2 == -1) {
94                 return this.name.compareTo(that.name);
95             }
96             answer = s1 == -1 ? 1 : s2 == -1 ? -1 : s1 - s2;
97         }
98         return answer;
99     }
100
101     /**
102      * Returns the name of the znode
103      */
104     public String getName() {
105         return name;
106     }
107
108     /**
109      * Returns the sequence number
110      */
111     public int getZNodeName() {
112         return sequence;
113     }
114
115     /**
116      * Returns the text prefix before the sequence number
117      */
118     public String getPrefix() {
119         return prefix;
120     }
121 }