001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.replication.configuration;
021    
022    
023    import java.io.Serializable;
024    import java.util.regex.Pattern;
025    
026    import org.apache.directory.server.i18n.I18n;
027    import org.apache.directory.shared.ldap.util.StringTools;
028    
029    
030    /**
031     * Store a replica ID after having normalized it.
032     * 
033     * The normalization proces checks that the submitted id is valid, ie
034     * contains only this char set : { '-', '_', 'a..z', 'A..Z', '0..9' }
035     * and its length is between 1 and 16. 
036     *
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     */
039    public class ReplicaId implements Comparable, Serializable
040    {
041        /**
042         * Declares the Serial Version Uid.
043         */
044        private static final long serialVersionUID = 1L;
045    
046        /** The replica pattern. */
047        private static final Pattern REPLICA_ID_PATTERN = Pattern.compile( "[-_A-Z0-9]{1,16}" );
048    
049        /** The formated replicaId */
050        private String id;
051    
052    
053        /**
054         * Creates a new instance of ReplicaId. The id must be a String 
055         * which respect the pattern :
056         * 
057         * [-_a-zA-Z0-9]*
058         * 
059         * and must be between 1 and 16 chars length
060         *
061         * @param id The replica pattern
062         */
063        public ReplicaId( String id )
064        {
065            if ( StringTools.isEmpty( id ) )
066            {
067                throw new IllegalArgumentException( I18n.err( I18n.ERR_694, id ) );
068            }
069    
070            String tmpId = id.trim().toUpperCase();
071    
072            if ( !REPLICA_ID_PATTERN.matcher( tmpId ).matches() )
073            {
074                throw new IllegalArgumentException( I18n.err( I18n.ERR_695, id ) );
075            }
076    
077            this.id = id;
078        }
079    
080    
081        /**
082         * @return The replicaId
083         */
084        public String getId()
085        {
086            return id;
087        }
088    
089    
090        /**
091         * Returns a hash code value for the object.
092         * 
093         * @return a hash code value for this object.
094         */
095        public int hashCode()
096        {
097            return id.hashCode();
098        }
099    
100    
101        /**
102         * Indicates whether some other object is "equal to" this one
103         * 
104         * @param o the reference object with which to compare.
105         * @return <code>true</code> if this object is the same as the obj argument; 
106         * <code>false</code> otherwise.
107         */
108        public boolean equals( Object o )
109        {
110            if ( o == null )
111            {
112                return false;
113            }
114    
115            if ( o == this )
116            {
117                return true;
118            }
119    
120            if ( o instanceof ReplicaId )
121            {
122                return this.id.equals( ( ( ReplicaId ) o ).id );
123            }
124            else
125            {
126                return false;
127            }
128        }
129    
130    
131        /**
132         * Compares this object with the specified object for order.  Returns a
133         * negative integer, zero, or a positive integer as this object is less
134         * than, equal to, or greater than the specified object.<p>
135         * 
136         * @param   o the Object to be compared.
137         * @return  a negative integer, zero, or a positive integer as this object
138         *      is less than, equal to, or greater than the specified object.
139         */
140        public int compareTo( Object o )
141        {
142            return this.id.compareTo( ( ( ReplicaId ) o ).id );
143        }
144    
145    
146        /**
147         * @return the Replica Id
148         */
149        public String toString()
150        {
151            return id;
152        }
153    }