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 */
020package org.apache.directory.api.ldap.extras.controls.syncrepl.syncDone;
021
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/**
030 * A simple {@link SyncDoneValue} implementation to store control properties.
031 *
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 * @version $Rev$, $Date$
034 */
035public class SyncDoneValueImpl extends AbstractControl implements SyncDoneValue
036{
037    /** The Sync cookie */
038    private byte[] cookie;
039
040    /** the refreshDeletes flag */
041    private boolean refreshDeletes;
042
043
044    /**
045     * Creates a new instance of SyncDoneValueImpl.
046     */
047    public SyncDoneValueImpl()
048    {
049        super( OID );
050    }
051
052
053    /**
054     *
055     * Creates a new instance of SyncDoneValueImpl.
056     *
057     * @param isCritical The critical flag
058     */
059    public SyncDoneValueImpl( boolean isCritical )
060    {
061        super( OID, isCritical );
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    public byte[] getCookie()
069    {
070        return cookie;
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    public void setCookie( byte[] cookie )
078    {
079        this.cookie = cookie;
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    public boolean isRefreshDeletes()
087    {
088        return refreshDeletes;
089    }
090
091
092    /**
093     * {@inheritDoc}
094     */
095    public void setRefreshDeletes( boolean refreshDeletes )
096    {
097        this.refreshDeletes = refreshDeletes;
098    }
099
100
101    /**
102     * @see Object#hashCode()
103     */
104    @Override
105    public int hashCode()
106    {
107        int h = 37;
108
109        h = h * 17 + super.hashCode();
110        h = h * 17 + ( refreshDeletes ? 1 : 0 );
111
112        if ( cookie != null )
113        {
114            for ( byte b : cookie )
115            {
116                h = h * 17 + b;
117            }
118        }
119
120        return h;
121    }
122
123
124    /**
125     * @see Object#equals(Object)
126     */
127    @Override
128    public boolean equals( Object o )
129    {
130        if ( this == o )
131        {
132            return true;
133        }
134
135        if ( !( o instanceof SyncDoneValue ) )
136        {
137            return false;
138        }
139
140        SyncDoneValue otherControl = ( SyncDoneValue ) o;
141
142        return ( refreshDeletes == otherControl.isRefreshDeletes() ) &&
143            ( Arrays.equals( cookie, otherControl.getCookie() ) &&
144            ( isCritical() == otherControl.isCritical() ) );
145    }
146
147
148    /**
149     * @see Object#toString()
150     */
151    @Override
152    public String toString()
153    {
154        StringBuilder sb = new StringBuilder();
155
156        sb.append( "    SyncDoneValue control :\n" );
157        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
158        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
159        sb.append( "        cookie            : '" ).append( Strings.dumpBytes( getCookie() ) ).append( "'\n" );
160        sb.append( "        refreshDeletes : '" ).append( isRefreshDeletes() ).append( "'\n" );
161
162        return sb.toString();
163    }
164}