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.model.message;
021
022
023import org.apache.directory.api.i18n.I18n;
024
025
026/**
027 * Implementation of an AbandonRequest message.
028 * 
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class AbandonRequestImpl extends AbstractRequest implements AbandonRequest
032{
033    /** Sequence identifier of the outstanding request message to abandon */
034    private int abandonId;
035
036    /** Ldap abandon request type code */
037    private static final MessageTypeEnum TYPE = MessageTypeEnum.ABANDON_REQUEST;
038
039
040    /**
041     * Creates an AbandonRequest implementation for an outstanding request.
042     */
043    public AbandonRequestImpl()
044    {
045        super( -1, TYPE, false );
046    }
047
048
049    /**
050     * Creates an AbandonRequest implementation for an outstanding request.
051     * 
052     * @param abdandonnedId the sequence identifier of the AbandonRequest message.
053     */
054    public AbandonRequestImpl( final int abdandonnedId )
055    {
056        super( -1, TYPE, false );
057        abandonId = abdandonnedId;
058    }
059
060
061    /**
062     * Gets the id of the request operation to terminate.
063     * 
064     * @return the id of the request message to abandon
065     */
066    @Override
067    public int getAbandoned()
068    {
069        return abandonId;
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public AbandonRequest setAbandoned( int abandonId )
078    {
079        this.abandonId = abandonId;
080
081        return this;
082    }
083
084
085    /**
086     * RFC 2251 [Section 4.11]: Abandon, Bind, Unbind, and StartTLS operations
087     * cannot be abandoned.
088     */
089    public void abandon()
090    {
091        throw new UnsupportedOperationException( I18n.err( I18n.ERR_13506_CANNOT_ABANDON_OPERATION ) );
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public AbandonRequest setMessageId( int messageId )
100    {
101        super.setMessageId( messageId );
102
103        return this;
104    }
105
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111public AbandonRequest addControl( Control control )
112    {
113        return ( AbandonRequest ) super.addControl( control );
114    }
115
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public AbandonRequest addAllControls( Control[] controls )
122    {
123        return ( AbandonRequest ) super.addAllControls( controls );
124    }
125
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public AbandonRequest removeControl( Control control )
132    {
133        return ( AbandonRequest ) super.removeControl( control );
134    }
135
136
137    /**
138     * Checks for equality first by asking the super method which should compare
139     * all but the Abandoned request's Id. It then compares this to determine
140     * equality.
141     * 
142     * @param obj the object to test for equality to this AbandonRequest
143     * @return true if the obj equals this request, false otherwise
144     */
145    @Override
146    public boolean equals( Object obj )
147    {
148        if ( this == obj )
149        {
150            return true;
151        }
152
153        if ( !( obj instanceof AbandonRequest ) )
154        {
155            return false;
156        }
157
158        if ( !super.equals( obj ) )
159        {
160            return false;
161        }
162
163        AbandonRequest req = ( AbandonRequest ) obj;
164
165        return req.getAbandoned() == abandonId;
166    }
167
168
169    /**
170     * @see Object#hashCode()
171     * @return the instance's hash code 
172     */
173    @Override
174    public int hashCode()
175    {
176        int hash = 37;
177        hash = hash * 17 + abandonId;
178        hash = hash * 17 + super.hashCode();
179
180        return hash;
181    }
182
183
184    /**
185     * Return a String representing an AbandonRequest
186     * 
187     * @return A String representing the AbandonRequest
188     */
189    @Override
190    public String toString()
191    {
192        StringBuilder sb = new StringBuilder();
193
194        sb.append( "    Abandon Request :\n" );
195        sb.append( "        Message Id : " ).append( abandonId );
196
197        // The controls
198        sb.append( super.toString() );
199
200        return sb.toString();
201    }
202}