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  
21  package org.apache.directory.api.ldap.extras.controls.ad;
22  
23  import java.util.Arrays;
24  
25  import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
26  import org.apache.directory.api.util.Strings;
27  
28  /**
29   * The class implemnting the AdDirsSync interface
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class AdDirSyncImpl extends AbstractControl implements AdDirSync
34  {
35      /** A flag used to tell the server to return the parent before the children */
36      int parentFirst = 1;
37      
38      /** A flag used to indicate that there are more data to return */
39      AdDirSyncFlag flag = AdDirSyncFlag.DEFAULT;
40  
41      /** The maximum number of attributes to return */
42      int maxReturnLength = 0;
43      
44      /** The DirSync cookie */
45      private byte[] cookie;
46  
47      /**
48       * Creates an instance of the DirSync control
49       */
50      public AdDirSyncImpl()
51      {
52          super( OID, Boolean.TRUE );
53      }
54  
55      
56      /**
57       * {@inheritDoc}
58       */
59      public int getParentFirst()
60      {
61          return parentFirst;
62      }
63  
64      
65      /**
66       * {@inheritDoc}
67       */
68      public void setParentFirst( int parentFirst )
69      {
70          this.parentFirst = parentFirst;
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      public AdDirSyncFlag getFlag()
78      {
79          return flag;
80      }
81      
82  
83      /**
84       * {@inheritDoc}
85       */
86      public void setFlag( AdDirSyncFlag flag )
87      {
88          this.flag = flag;
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      public int getMaxReturnLength()
96      {
97          return maxReturnLength;
98      }
99  
100 
101     /**
102      * {@inheritDoc}
103      */
104     public void setMaxReturnLength( int maxReturnLength )
105     {
106         this.maxReturnLength = maxReturnLength;
107     }
108 
109 
110     /**
111      * {@inheritDoc}
112      */
113     public byte[] getCookie()
114     {
115         return cookie;
116     }
117 
118 
119     /**
120      * {@inheritDoc}
121      */
122     public void setCookie( byte[] cookie )
123     {
124         if ( cookie != null )
125         {
126             this.cookie = new byte[cookie.length];
127             System.arraycopy( cookie, 0, this.cookie, 0,cookie.length );
128         }
129         else
130         {
131             this.cookie = Strings.EMPTY_BYTES;
132         }
133     }
134     
135     
136     /**
137      * @see Object#hashCode()
138      */
139     @Override
140     public int hashCode()
141     {
142         int h = 37;
143 
144         h = h * 17 + super.hashCode();
145         h = h * 17 + parentFirst;
146         h = h * 17 + maxReturnLength;
147 
148         if ( cookie != null )
149         {
150             for ( byte b : cookie )
151             {
152                 h = h * 17 + b;
153             }
154         }
155 
156         return h;
157     }
158 
159 
160     /**
161      * @see Object#equals(Object)
162      */
163     @Override
164     public boolean equals( Object o )
165     {
166         if ( this == o )
167         {
168             return true;
169         }
170 
171         if ( !( o instanceof AdDirSync ) )
172         {
173             return false;
174         }
175 
176         AdDirSync otherControl = ( AdDirSync ) o;
177 
178         return ( maxReturnLength == otherControl.getMaxReturnLength() ) &&
179             ( parentFirst == otherControl.getParentFirst() ) &&
180             ( Arrays.equals( cookie, otherControl.getCookie() ) &&
181             ( isCritical() == otherControl.isCritical() ) );
182     }
183 
184 
185     /**
186      * @see Object#toString()
187      */
188     @Override
189     public String toString()
190     {
191         StringBuilder sb = new StringBuilder();
192 
193         sb.append( "    DirSync control :\n" );
194         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
195         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
196         sb.append( "        parentFirst : '" ).append( getParentFirst() ).append( "'\n" );
197         sb.append( "        maxReturnLength : '" ).append( getMaxReturnLength() ).append( "'\n" );
198         sb.append( "        cookie            : '" ).append( Strings.dumpBytes( getCookie() ) ).append( "'\n" );
199 
200         return sb.toString();
201     }
202 }