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