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