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.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    /**
078     * Gets the OID uniquely identifying this extended response (a.k.a. its
079     * name).
080     * 
081     * @return the responseName of the extended response
082     */
083    public String getResponseName()
084    {
085        return ( ( responseName == null ) ? "" : responseName.toString() );
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    public void setResponseName( String responseName )
096    {
097        this.responseName = responseName;
098    }
099
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public int hashCode()
106    {
107        int hash = 37;
108
109        if ( responseName != null )
110        {
111            hash = hash * 17 + responseName.hashCode();
112        }
113
114        hash = hash * 17 + super.hashCode();
115
116        return hash;
117    }
118
119
120    /**
121     * Checks to see if an object equals this ExtendedRequest.
122     * 
123     * @param obj
124     *            the object to be checked for equality
125     * @return true if the obj equals this ExtendedRequest, false otherwise
126     */
127    public boolean equals( Object obj )
128    {
129        if ( obj == this )
130        {
131            return true;
132        }
133
134        if ( !super.equals( obj ) )
135        {
136            return false;
137        }
138
139        if ( !( obj instanceof ExtendedResponse ) )
140        {
141            return false;
142        }
143
144        ExtendedResponse resp = ( ExtendedResponse ) obj;
145
146        if ( ( responseName != null ) && ( resp.getResponseName() == null ) )
147        {
148            return false;
149        }
150
151        if ( ( responseName == null ) && ( resp.getResponseName() != null ) )
152        {
153            return false;
154        }
155
156        if ( ( responseName != null ) && ( resp.getResponseName() != null )
157            && !responseName.equals( resp.getResponseName() ) )
158        {
159            return false;
160        }
161
162        return true;
163    }
164
165
166    /**
167     * Get a String representation of an ExtendedResponse
168     * 
169     * @return An ExtendedResponse String
170     */
171    public String toString()
172    {
173        StringBuilder sb = new StringBuilder();
174
175        sb.append( "    Extended Response\n" );
176
177        if ( responseName != null )
178        {
179            sb.append( "        ResponseName :'" ).append( responseName ).append( "'\n" );
180        }
181
182        sb.append( super.toString() );
183
184        return super.toString( sb.toString() );
185    }
186}