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