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<>();
43  
44      /** The encoded LdapURL */
45      private final List<byte[]> urlsBytes = new ArrayList<>();
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      @Override
58      public int getReferralLength()
59      {
60          return referralLength;
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public void setReferralLength( int referralLength )
69      {
70          this.referralLength = referralLength;
71      }
72  
73  
74      /**
75       * Gets an unmodifiable set of alternative referral urls.
76       * 
77       * @return the alternative url objects.
78       */
79      @Override
80      public Collection<String> getLdapUrls()
81      {
82          return Collections.unmodifiableCollection( urls );
83      }
84  
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public Collection<byte[]> getLdapUrlsBytes()
91      {
92          return urlsBytes;
93      }
94  
95  
96      /**
97       * Adds an LDAPv3 URL to this Referral.
98       * 
99       * @param url the LDAPv3 URL to add
100      */
101     @Override
102     public void addLdapUrl( String url )
103     {
104         urls.add( url );
105     }
106 
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public void addLdapUrlBytes( byte[] urlBytes )
113     {
114         urlsBytes.add( urlBytes );
115     }
116 
117 
118     /**
119      * Removes an LDAPv3 URL to this Referral.
120      * 
121      * @param url
122      *            the LDAPv3 URL to remove
123      */
124     @Override
125     public void removeLdapUrl( String url )
126     {
127         urls.remove( url );
128     }
129 
130 
131     /**
132      * @see Object#hashCode()
133      * @return the instance's hash code 
134      */
135     @Override
136     public int hashCode()
137     {
138         int hash = 37;
139         hash = hash * 17 + urls.size();
140 
141         // Order doesn't matter, so just add the url hashCode
142         for ( String url : urls )
143         {
144             hash = hash + url.hashCode();
145         }
146 
147         return hash;
148     }
149 
150 
151     /**
152      * Compares this Referral implementation to see if it is the same as
153      * another. The classes do not have to be the same implementation to return
154      * true. Both this and the compared Referral must have the same entries
155      * exactly. The order of Referral URLs does not matter.
156      * 
157      * @param obj
158      *            the object to compare this ReferralImpl to
159      * @return true if both implementations contain exactly the same URLs
160      */
161     @Override
162     public boolean equals( Object obj )
163     {
164         // just in case for speed return true if obj is this object
165         if ( obj == this )
166         {
167             return true;
168         }
169 
170         if ( obj instanceof Referral )
171         {
172             Collection<String> refs = ( ( Referral ) obj ).getLdapUrls();
173 
174             // if their sizes do not match they are not equal
175             if ( refs.size() != urls.size() )
176             {
177                 return false;
178             }
179 
180             Iterator<String> list = urls.iterator();
181 
182             while ( list.hasNext() )
183             {
184                 // if one of our urls is not contained in the obj return false
185                 if ( !refs.contains( list.next() ) )
186                 {
187                     return false;
188                 }
189             }
190 
191             // made it through the checks so we have a match
192             return true;
193         }
194 
195         return false;
196     }
197 
198 
199     /**
200      * Get a String representation of a Referral
201      * 
202      * @return A Referral String
203      */
204     @Override
205     public String toString()
206     {
207         StringBuilder sb = new StringBuilder();
208 
209         if ( !urls.isEmpty() )
210         {
211             sb.append( "            Referrals :\n" );
212 
213             Object[] urlsArray = urls.toArray();
214 
215             for ( int i = 0; i < urlsArray.length; i++ )
216             {
217 
218                 String referral = ( String ) urlsArray[i];
219 
220                 sb.append( "                Referral[" ).append( i ).append( "] :" ).append( referral ).append( '\n' );
221             }
222         }
223 
224         return sb.toString();
225     }
226 }