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