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
023/**
024 * A simple ExtendedResponse implementation.
025 * 
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public class ExtendedResponseImpl extends AbstractResultResponse implements ExtendedResponse
029{
030    static final long serialVersionUID = -6646752766410531060L;
031
032    /** Object identifier for the extended response */
033    protected String responseName;
034
035
036    /**
037     * Creates an ExtendedResponse as a reply to an ExtendedRequest.
038     * 
039     * @param responseName the ExtendedResponse's name
040     */
041    public ExtendedResponseImpl( String responseName )
042    {
043        super( -1, TYPE );
044        this.responseName = responseName;
045    }
046
047
048    /**
049     * Creates an ExtendedResponse as a reply to an ExtendedRequest.
050     * 
051     * @param id the session unique message id
052     * @param responseName the ExtendedResponse's name
053     */
054    public ExtendedResponseImpl( final int id, String responseName )
055    {
056        super( id, TYPE );
057        this.responseName = responseName;
058    }
059
060
061    /**
062     * Creates an ExtendedResponse as a reply to an ExtendedRequest.
063     * 
064     * @param id the session unique message id
065     */
066    public ExtendedResponseImpl( int id )
067    {
068        super( id, TYPE );
069    }
070
071
072    // ------------------------------------------------------------------------
073    // ExtendedResponse Interface Method Implementations
074    // ------------------------------------------------------------------------
075
076    /**
077     * Gets the OID uniquely identifying this extended response (a.k.a. its
078     * name).
079     * 
080     * @return the responseName of the extended response
081     */
082    @Override
083    public String getResponseName()
084    {
085        return ( responseName == null ) ? "" : responseName;
086    }
087
088
089    /**
090     * Sets the OID uniquely identifying this extended response (a.k.a. its
091     * name).
092     * 
093     * @param responseName the OID of the extended response type.
094     */
095    @Override
096    public void setResponseName( String responseName )
097    {
098        this.responseName = responseName;
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public int hashCode()
107    {
108        int hash = 37;
109
110        if ( responseName != null )
111        {
112            hash = hash * 17 + responseName.hashCode();
113        }
114
115        hash = hash * 17 + super.hashCode();
116
117        return hash;
118    }
119
120
121    /**
122     * Checks to see if an object equals this ExtendedRequest.
123     * 
124     * @param obj
125     *            the object to be checked for equality
126     * @return true if the obj equals this ExtendedRequest, false otherwise
127     */
128    @Override
129    public boolean equals( Object obj )
130    {
131        if ( obj == this )
132        {
133            return true;
134        }
135
136        if ( !super.equals( obj ) )
137        {
138            return false;
139        }
140
141        if ( !( obj instanceof ExtendedResponse ) )
142        {
143            return false;
144        }
145
146        ExtendedResponse resp = ( ExtendedResponse ) obj;
147
148        if ( ( responseName != null ) && ( resp.getResponseName() == null ) )
149        {
150            return false;
151        }
152
153        if ( ( responseName == null ) && ( resp.getResponseName() != null ) )
154        {
155            return false;
156        }
157
158        if ( ( responseName != null ) && ( resp.getResponseName() != null )
159            && !responseName.equals( resp.getResponseName() ) )
160        {
161            return false;
162        }
163
164        return true;
165    }
166
167
168    /**
169     * Get a String representation of an ExtendedResponse
170     * 
171     * @return An ExtendedResponse String
172     */
173    @Override
174    public String toString()
175    {
176        StringBuilder sb = new StringBuilder();
177
178        sb.append( "    Extended Response\n" );
179
180        if ( responseName != null )
181        {
182            sb.append( "        ResponseName :'" ).append( responseName ).append( "'\n" );
183        }
184
185        sb.append( super.toString() );
186
187        return super.toString( sb.toString() );
188    }
189}