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 *    https://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 implementing the AdDirsSync interface
030 *
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class AdDirSyncRequestImpl extends AbstractControl implements AdDirSyncRequest
034{
035    /** The parentsFirst value */
036    private int parentsFirst;
037
038    /** The maximum attribute count to return */
039    private int maxAttributeCount = 0;
040
041    /** The DirSync cookie */
042    private byte[] cookie;
043
044    /**
045     * Creates an instance of the DirSync control
046     */
047    public AdDirSyncRequestImpl()
048    {
049        super( OID, Boolean.TRUE );
050    }
051
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public int getParentsFirst()
058    {
059        return parentsFirst;
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public void setParentsFirst( int parentsFirst )
068    {
069        this.parentsFirst = parentsFirst;
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public int getMaxAttributeCount()
078    {
079        return maxAttributeCount;
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public void setMaxAttributeCount( int maxAttributeCount )
088    {
089        this.maxAttributeCount = maxAttributeCount;
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public byte[] getCookie()
098    {
099        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}