Push variuos changes
[music.git] / jar / src / main / java / org / onap / music / lockingservice / ZNodeName.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.lockingservice;
24
25 import org.onap.music.eelf.logging.EELFLoggerDelegate;
26 import org.onap.music.eelf.logging.format.ErrorSeverity;
27 import org.onap.music.eelf.logging.format.ErrorTypes;
28
29 /**
30  * Represents an ephemeral znode name which has an ordered sequence number and can be sorted in
31  * order
32  *
33  */
34 class ZNodeName implements Comparable<ZNodeName> {
35     private final String name;
36     private String prefix;
37     private int sequence = -1;
38     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ZNodeName.class);
39     
40     public ZNodeName(String name) {
41         if (name == null) {
42             throw new NullPointerException("id cannot be null");
43         }
44         this.name = name;
45         this.prefix = name;
46         int idx = name.lastIndexOf('-');
47         if (idx >= 0) {
48             this.prefix = name.substring(0, idx);
49             try {
50                 this.sequence = Integer.parseInt(name.substring(idx + 1));
51                 // If an exception occurred we misdetected a sequence suffix,
52                 // so return -1.
53             } catch (NumberFormatException e) {
54                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Number format exception "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
55             } catch (ArrayIndexOutOfBoundsException e) {
56                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Array out of bounds for  "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
57             }
58         }
59     }
60
61     @Override
62     public String toString() {
63         return name.toString();
64     }
65
66     @Override
67     public boolean equals(Object o) {
68         if (this == o)
69             return true;
70         if (o == null || getClass() != o.getClass())
71             return false;
72
73         ZNodeName sequence = (ZNodeName) o;
74
75         if (!name.equals(sequence.name))
76             return false;
77
78         return true;
79     }
80
81     @Override
82     public int hashCode() {
83         return name.hashCode() + 37;
84     }
85
86     public int compareTo(ZNodeName that) {
87         int answer = this.prefix.compareTo(that.prefix);
88         if (answer == 0) {
89             int s1 = this.sequence;
90             int s2 = that.sequence;
91             if (s1 == -1 && s2 == -1) {
92                 return this.name.compareTo(that.name);
93             }
94             answer = s1 == -1 ? 1 : s2 == -1 ? -1 : s1 - s2;
95         }
96         return answer;
97     }
98
99     /**
100      * Returns the name of the znode
101      */
102     public String getName() {
103         return name;
104     }
105
106     /**
107      * Returns the sequence number
108      */
109     public int getZNodeName() {
110         return sequence;
111     }
112
113     /**
114      * Returns the text prefix before the sequence number
115      */
116     public String getPrefix() {
117         return prefix;
118     }
119 }