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