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