2 * ============LICENSE_START==========================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=============================================
22 * ====================================================================
24 package org.onap.music.lockingservice;
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;
31 * Represents an ephemeral znode name which has an ordered sequence number and can be sorted in
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);
41 public ZNodeName(String name) {
43 throw new NullPointerException("id cannot be null");
47 int idx = name.lastIndexOf('-');
49 this.prefix = name.substring(0, idx);
51 this.sequence = Integer.parseInt(name.substring(idx + 1));
52 // If an exception occurred we misdetected a sequence suffix,
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);
63 public String toString() {
68 public boolean equals(Object o) {
71 if (o == null || getClass() != o.getClass())
74 ZNodeName sequence = (ZNodeName) o;
76 if (!name.equals(sequence.name))
83 public int hashCode() {
84 return name.hashCode() + 37;
87 public int compareTo(ZNodeName that) {
88 int answer = this.prefix.compareTo(that.prefix);
90 int s1 = this.sequence;
91 int s2 = that.sequence;
92 if (s1 == -1 && s2 == -1) {
93 return this.name.compareTo(that.name);
95 answer = s1 == -1 ? 1 : s2 == -1 ? -1 : s1 - s2;
101 * Returns the name of the znode
103 public String getName() {
108 * Returns the sequence number
110 public int getZNodeName() {
115 * Returns the text prefix before the sequence number
117 public String getPrefix() {