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