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;
024import java.util.EnumSet;
025import java.util.Set;
026
027import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
028import org.apache.directory.api.util.Strings;
029
030/**
031 * The class implemnting the AdDirsSync interface
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class AdDirSyncImpl extends AbstractControl implements AdDirSync
036{
037    /** Flags used to control return values (client-to-server) or indicate that there are more data to return (server-to-client) */
038    private Set<AdDirSyncFlag> flags = EnumSet.noneOf( AdDirSyncFlag.class );
039     
040
041    /** The maximum number of attributes to return */
042    private 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    @Override
060    public Set<AdDirSyncFlag> getFlags()
061    {
062        return flags;
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public void setFlags( Set<AdDirSyncFlag> flags )
071    {
072        this.flags = flags;
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public void addFlag( AdDirSyncFlag flag )
081    {
082        flags.add( flag );
083    }
084    
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public void removeFlag( AdDirSyncFlag flag )
091    {
092        flags.remove( flag );
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @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}