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   *    https://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 implementing the AdDirsSync interface
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class AdDirSyncRequestImpl extends AbstractControl implements AdDirSyncRequest
34  {
35      /** The parentsFirst value */
36      private int parentsFirst;
37  
38      /** The maximum attribute count to return */
39      private int maxAttributeCount = 0;
40  
41      /** The DirSync cookie */
42      private byte[] cookie;
43  
44      /**
45       * Creates an instance of the DirSync control
46       */
47      public AdDirSyncRequestImpl()
48      {
49          super( OID, Boolean.TRUE );
50      }
51  
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public int getParentsFirst()
58      {
59          return parentsFirst;
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public void setParentsFirst( int parentsFirst )
68      {
69          this.parentsFirst = parentsFirst;
70      }
71  
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public int getMaxAttributeCount()
78      {
79          return maxAttributeCount;
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public void setMaxAttributeCount( int maxAttributeCount )
88      {
89          this.maxAttributeCount = maxAttributeCount;
90      }
91  
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public byte[] getCookie()
98      {
99          return cookie;
100     }
101 
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void setCookie( byte[] cookie )
108     {
109         if ( cookie != null )
110         {
111             this.cookie = new byte[cookie.length];
112             System.arraycopy( cookie, 0, this.cookie, 0, cookie.length );
113         }
114         else
115         {
116             this.cookie = Strings.EMPTY_BYTES;
117         }
118     }
119 
120 
121     /**
122      * @see Object#hashCode()
123      */
124     @Override
125     public int hashCode()
126     {
127         int h = super.hashCode();
128 
129         h = h * 17 + parentsFirst;
130         h = h * 17 + maxAttributeCount;
131 
132         if ( cookie != null )
133         {
134             for ( byte b : cookie )
135             {
136                 h = h * 17 + b;
137             }
138         }
139 
140         return h;
141     }
142 
143 
144     /**
145      * @see Object#equals(Object)
146      */
147     @Override
148     public boolean equals( Object other )
149     {
150         if ( this == other )
151         {
152             return true;
153         }
154 
155         if ( !( other instanceof AdDirSyncRequest ) )
156         {
157             return false;
158         }
159 
160         AdDirSyncRequest otherControl = ( AdDirSyncRequest ) other;
161 
162         return super.equals( other )
163             && ( maxAttributeCount == otherControl.getMaxAttributeCount() )
164             && ( parentsFirst == otherControl.getParentsFirst() )
165             && ( Arrays.equals( cookie, otherControl.getCookie() ) )
166             && ( isCritical() == otherControl.isCritical() );
167     }
168 
169 
170     /**
171      * @see Object#toString()
172      */
173     @Override
174     public String toString()
175     {
176         StringBuilder sb = new StringBuilder();
177 
178         sb.append( "    DirSync control :\n" );
179         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
180         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
181         sb.append( "        parentsFirst : " ).append( parentsFirst ).append( "\n" );
182         sb.append( "        maxAttributeCount : '" ).append( maxAttributeCount ).append( "'\n" );
183         sb.append( "        cookie            : '" ).append( Strings.dumpBytes( getCookie() ) ).append( "'\n" );
184 
185         return sb.toString();
186     }
187 }