001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020
021package org.apache.directory.api.ldap.extras.controls.ad;
022
023import java.util.Arrays;
024
025import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
026import org.apache.directory.api.util.Strings;
027
028/**
029 * The class implemnting the AdDirsSync interface
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class AdDirSyncImpl extends AbstractControl implements AdDirSync
034{
035    /** A flag used to tell the server to return the parent before the children */
036    int parentFirst = 1;
037    
038    /** A flag used to indicate that there are more data to return */
039    AdDirSyncFlag flag = AdDirSyncFlag.DEFAULT;
040
041    /** The maximum number of attributes to return */
042    int maxReturnLength = 0;
043    
044    /** The DirSync cookie */
045    private byte[] cookie;
046
047    /**
048     * Creates an instance of the DirSync control
049     */
050    public AdDirSyncImpl()
051    {
052        super( OID, Boolean.TRUE );
053    }
054
055    
056    /**
057     * {@inheritDoc}
058     */
059    public int getParentFirst()
060    {
061        return parentFirst;
062    }
063
064    
065    /**
066     * {@inheritDoc}
067     */
068    public void setParentFirst( int parentFirst )
069    {
070        this.parentFirst = parentFirst;
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    public AdDirSyncFlag getFlag()
078    {
079        return flag;
080    }
081    
082
083    /**
084     * {@inheritDoc}
085     */
086    public void setFlag( AdDirSyncFlag flag )
087    {
088        this.flag = flag;
089    }
090
091
092    /**
093     * {@inheritDoc}
094     */
095    public int getMaxReturnLength()
096    {
097        return maxReturnLength;
098    }
099
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}