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.shared.ldap.message;
021    
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.Collections;
025    import java.util.Iterator;
026    import java.util.List;
027    
028    import org.apache.directory.shared.ldap.message.internal.InternalReferral;
029    
030    
031    
032    /**
033     * A Referral implementation. For the time being this implementation uses a
034     * String representation for LDAPURLs. In the future an LdapUrl interface with
035     * default implementations will be used once a parser for an LdapUrl is created.
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 905344 $
039     */
040    public class ReferralImpl implements InternalReferral
041    {
042        static final long serialVersionUID = 2638820668325359096L;
043    
044        /** Sequence of LDAPUrls composing this Referral */
045        private final List<String> urls = new ArrayList<String>();
046    
047    
048        // ------------------------------------------------------------------------
049        // LdapResult Interface Method Implementations
050        // ------------------------------------------------------------------------
051    
052        /**
053         * Gets an unmodifiable set of alternative referral urls.
054         * 
055         * @return the alternative url objects.
056         */
057        public Collection<String> getLdapUrls()
058        {
059            return Collections.unmodifiableCollection( urls );
060        }
061    
062    
063        /**
064         * Adds an LDAPv3 URL to this Referral.
065         * 
066         * @param url
067         *            the LDAPv3 URL to add
068         */
069        public void addLdapUrl( String url )
070        {
071            urls.add( url );
072        }
073    
074    
075        /**
076         * Removes an LDAPv3 URL to this Referral.
077         * 
078         * @param url
079         *            the LDAPv3 URL to remove
080         */
081        public void removeLdapUrl( String url )
082        {
083            urls.remove( url );
084        }
085    
086    
087        /**
088         * Compares this Referral implementation to see if it is the same as
089         * another. The classes do not have to be the same implementation to return
090         * true. Both this and the compared Referral must have the same entries
091         * exactly. The order of Referral URLs does not matter.
092         * 
093         * @param obj
094         *            the object to compare this ReferralImpl to
095         * @return true if both implementations contain exactly the same URLs
096         */
097        public boolean equals( Object obj )
098        {
099            // just in case for speed return true if obj is this object
100            if ( obj == this )
101            {
102                return true;
103            }
104    
105            if ( obj instanceof InternalReferral )
106            {
107                Collection<String> refs = ( ( InternalReferral ) obj ).getLdapUrls();
108    
109                // if their sizes do not match they are not equal
110                if ( refs.size() != urls.size() )
111                {
112                    return false;
113                }
114    
115                Iterator<String> list = urls.iterator();
116                
117                while ( list.hasNext() )
118                {
119                    // if one of our urls is not contained in the obj return false
120                    if ( !refs.contains( list.next() ) )
121                    {
122                        return false;
123                    }
124                }
125    
126                // made it through the checks so we have a match
127                return true;
128            }
129    
130            return false;
131        }
132    
133    
134        /**
135         * Get a String representation of a Referral
136         * 
137         * @return A Referral String
138         */
139        public String toString()
140        {
141            StringBuffer sb = new StringBuffer();
142    
143            if ( ( urls != null ) && ( urls.size() != 0 ) )
144            {
145                sb.append( "            Referrals :\n" );
146    
147                Object[] urlsArray = urls.toArray();
148    
149                for ( int i = 0; i < urlsArray.length; i++ )
150                {
151    
152                    String referral = ( String ) urlsArray[i];
153    
154                    sb.append( "                Referral[" ).append( i ).append( "] :" ).append( referral ).append( '\n' );
155                }
156            }
157    
158            return sb.toString();
159        }
160    }