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 */
019package org.apache.directory.api.ldap.extras.controls.transaction;
020
021import java.util.Arrays;
022
023import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
024import org.apache.directory.api.util.Strings;
025
026/**
027 * The Transaction Specification control. It's defined in RFC 5805.
028 * This control is sent with every update once a transaction is started.
029 * It contains the Transaction ID. 
030 *
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class TransactionSpecificationImpl extends AbstractControl implements TransactionSpecification
034{
035    /** The Transaction Specification identifier */
036    private byte[] identifier;
037
038    /**
039     * Default constructor
040     */
041    public TransactionSpecificationImpl()
042    {
043        super( OID );
044    }
045    
046
047    /**
048     * {@inheritDoc}
049     */
050    @Override
051    public byte[] getIdentifier()
052    {
053        return identifier;
054    }
055    
056    
057    /**
058     * {@inheritDoc}
059     */
060    @Override
061    public void setIdentifier( byte[] identifier )
062    {
063        // Copy the byte[]
064        if ( identifier != null )
065        {
066            this.identifier = new byte[identifier.length];
067            System.arraycopy( identifier, 0, this.identifier, 0, identifier.length );
068        }
069    }
070
071
072    /**
073     * @see Object#hashCode()
074     */
075    @Override
076    public int hashCode()
077    {
078        int h = super.hashCode();
079
080        if ( identifier != null )
081        {
082            for ( byte b : identifier )
083            {
084                h = h * 17 + b;
085            }
086        }
087
088        return h;
089    }
090
091
092    /**
093     * @see Object#equals(Object)
094     */
095    @Override
096    public boolean equals( Object o )
097    {
098        if ( this == o )
099        {
100            return true;
101        }
102        
103        if ( !( o instanceof TransactionSpecification ) )
104        {
105            return false;
106        }
107
108        TransactionSpecification otherControl = ( TransactionSpecification ) o;
109
110        return super.equals( o )
111            && Arrays.equals( identifier, otherControl.getIdentifier() );
112    }
113    
114    
115    /**
116     * @see Object#toString()
117     */
118    @Override
119    public String toString()
120    {
121        StringBuilder sb = new StringBuilder();
122
123        sb.append( "    Transaction Spcecification control :\n" );
124        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
125        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
126        
127        if ( identifier != null )
128        {
129            sb.append( "        Transaction ID=null" ).append( '\n' );
130        }
131        else
132        {
133            sb.append( "        Transaction ID=" ).append( Strings.dumpBytes( identifier ) ).append( '\n' );
134        }
135        
136        return sb.toString();
137    }
138}