1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.server.replication.configuration; 21 22 23 import java.io.Serializable; 24 import java.util.regex.Pattern; 25 26 import org.apache.directory.shared.ldap.util.StringTools; 27 28 29 /** 30 * Store a replica ID after having normalized it. 31 * 32 * The normalization proces checks that the submitted id is valid, ie 33 * contains only this char set : { '-', '_', 'a..z', 'A..Z', '0..9' } 34 * and its length is between 1 and 16. 35 * 36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 37 */ 38 public class ReplicaId implements Comparable, Serializable 39 { 40 /** 41 * Declares the Serial Version Uid. 42 */ 43 private static final long serialVersionUID = 1L; 44 45 /** The replica pattern. */ 46 private static final Pattern REPLICA_ID_PATTERN = Pattern.compile( "[-_A-Z0-9]{1,16}" ); 47 48 /** The formated replicaId */ 49 private String id; 50 51 52 /** 53 * Creates a new instance of ReplicaId. The id must be a String 54 * which respect the pattern : 55 * 56 * [-_a-zA-Z0-9]* 57 * 58 * and must be between 1 and 16 chars length 59 * 60 * @param id The replica pattern 61 */ 62 public ReplicaId( String id ) 63 { 64 if ( StringTools.isEmpty( id ) ) 65 { 66 throw new IllegalArgumentException( "Empty ID: " + id ); 67 } 68 69 String tmpId = id.trim().toUpperCase(); 70 71 if ( !REPLICA_ID_PATTERN.matcher( tmpId ).matches() ) 72 { 73 throw new IllegalArgumentException( "Invalid replica ID: " + id ); 74 } 75 76 this.id = id; 77 } 78 79 80 /** 81 * @return The replicaId 82 */ 83 public String getId() 84 { 85 return id; 86 } 87 88 89 /** 90 * Returns a hash code value for the object. 91 * 92 * @return a hash code value for this object. 93 */ 94 public int hashCode() 95 { 96 return id.hashCode(); 97 } 98 99 100 /** 101 * Indicates whether some other object is "equal to" this one 102 * 103 * @param o the reference object with which to compare. 104 * @return <code>true</code> if this object is the same as the obj argument; 105 * <code>false</code> otherwise. 106 */ 107 public boolean equals( Object o ) 108 { 109 if ( o == null ) 110 { 111 return false; 112 } 113 114 if ( o == this ) 115 { 116 return true; 117 } 118 119 if ( o instanceof ReplicaId ) 120 { 121 return this.id.equals( ( ( ReplicaId ) o ).id ); 122 } 123 else 124 { 125 return false; 126 } 127 } 128 129 130 /** 131 * Compares this object with the specified object for order. Returns a 132 * negative integer, zero, or a positive integer as this object is less 133 * than, equal to, or greater than the specified object.<p> 134 * 135 * @param o the Object to be compared. 136 * @return a negative integer, zero, or a positive integer as this object 137 * is less than, equal to, or greater than the specified object. 138 */ 139 public int compareTo( Object o ) 140 { 141 return this.id.compareTo( ( ( ReplicaId ) o ).id ); 142 } 143 144 145 /** 146 * @return the Replica Id 147 */ 148 public String toString() 149 { 150 return id; 151 } 152 }