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