View Javadoc
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.api.ldap.model.message;
21  
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  
30  /**
31   * A Referral implementation. For the time being this implementation uses a
32   * String representation for LDAPURLs. In the future an LdapUrl interface with
33   * default implementations will be used once a parser for an LdapUrl is created.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class ReferralImpl implements Referral
38  {
39      static final long serialVersionUID = 2638820668325359096L;
40  
41      /** Sequence of LDAPUrls composing this Referral */
42      private final List<String> urls = new ArrayList<String>();
43  
44      /** The encoded LdapURL */
45      private final List<byte[]> urlsBytes = new ArrayList<byte[]>();
46  
47      /** The length of the referral */
48      private int referralLength;
49  
50  
51      // ------------------------------------------------------------------------
52      // LdapResult Interface Method Implementations
53      // ------------------------------------------------------------------------
54      /**
55       * {@inheritDoc}
56       */
57      public int getReferralLength()
58      {
59          return referralLength;
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      public void setReferralLength( int referralLength )
67      {
68          this.referralLength = referralLength;
69      }
70  
71  
72      /**
73       * Gets an unmodifiable set of alternative referral urls.
74       * 
75       * @return the alternative url objects.
76       */
77      public Collection<String> getLdapUrls()
78      {
79          return Collections.unmodifiableCollection( urls );
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      public Collection<byte[]> getLdapUrlsBytes()
87      {
88          return urlsBytes;
89      }
90  
91  
92      /**
93       * Adds an LDAPv3 URL to this Referral.
94       * 
95       * @param url the LDAPv3 URL to add
96       */
97      public void addLdapUrl( String url )
98      {
99          urls.add( url );
100     }
101 
102 
103     /**
104      * {@inheritDoc}
105      */
106     public void addLdapUrlBytes( byte[] urlBytes )
107     {
108         urlsBytes.add( urlBytes );
109     }
110 
111 
112     /**
113      * Removes an LDAPv3 URL to this Referral.
114      * 
115      * @param url
116      *            the LDAPv3 URL to remove
117      */
118     public void removeLdapUrl( String url )
119     {
120         urls.remove( url );
121     }
122 
123 
124     /**
125      * @see Object#hashCode()
126      * @return the instance's hash code 
127      */
128     public int hashCode()
129     {
130         int hash = 37;
131         hash = hash * 17 + urls.size();
132 
133         // Order doesn't matter, so just add the url hashCode
134         for ( String url : urls )
135         {
136             hash = hash + url.hashCode();
137         }
138 
139         return hash;
140     }
141 
142 
143     /**
144      * Compares this Referral implementation to see if it is the same as
145      * another. The classes do not have to be the same implementation to return
146      * true. Both this and the compared Referral must have the same entries
147      * exactly. The order of Referral URLs does not matter.
148      * 
149      * @param obj
150      *            the object to compare this ReferralImpl to
151      * @return true if both implementations contain exactly the same URLs
152      */
153     public boolean equals( Object obj )
154     {
155         // just in case for speed return true if obj is this object
156         if ( obj == this )
157         {
158             return true;
159         }
160 
161         if ( obj instanceof Referral )
162         {
163             Collection<String> refs = ( ( Referral ) obj ).getLdapUrls();
164 
165             // if their sizes do not match they are not equal
166             if ( refs.size() != urls.size() )
167             {
168                 return false;
169             }
170 
171             Iterator<String> list = urls.iterator();
172 
173             while ( list.hasNext() )
174             {
175                 // if one of our urls is not contained in the obj return false
176                 if ( !refs.contains( list.next() ) )
177                 {
178                     return false;
179                 }
180             }
181 
182             // made it through the checks so we have a match
183             return true;
184         }
185 
186         return false;
187     }
188 
189 
190     /**
191      * Get a String representation of a Referral
192      * 
193      * @return A Referral String
194      */
195     public String toString()
196     {
197         StringBuffer sb = new StringBuffer();
198 
199         if ( ( urls != null ) && ( urls.size() != 0 ) )
200         {
201             sb.append( "            Referrals :\n" );
202 
203             Object[] urlsArray = urls.toArray();
204 
205             for ( int i = 0; i < urlsArray.length; i++ )
206             {
207 
208                 String referral = ( String ) urlsArray[i];
209 
210                 sb.append( "                Referral[" ).append( i ).append( "] :" ).append( referral ).append( '\n' );
211             }
212         }
213 
214         return sb.toString();
215     }
216 }