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 * Abstract base for a ResultResponse message.
025 * 
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public abstract class AbstractResultResponse extends AbstractResponse implements ResultResponse
029{
030    /** Response result components */
031    protected LdapResult ldapResult = new LdapResultImpl();
032
033
034    // ------------------------------------------------------------------------
035    // Response Interface Method Implementations
036    // ------------------------------------------------------------------------
037
038    /**
039     * Allows subclasses based on the abstract type to create a response to a
040     * request.
041     * 
042     * @param id the response eliciting this Request
043     * @param type the message type of the response
044     */
045    protected AbstractResultResponse( final int id, final MessageTypeEnum type )
046    {
047        super( id, type );
048    }
049
050
051    // ------------------------------------------------------------------------
052    // Response Interface Method Implementations
053    // ------------------------------------------------------------------------
054    /**
055     * Gets the LdapResult components of this Response.
056     * 
057     * @return the LdapResult for this Response.
058     */
059    public LdapResult getLdapResult()
060    {
061        return ldapResult;
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public int hashCode()
070    {
071        int hash = 37;
072        if ( getLdapResult() != null )
073        {
074            hash = hash * 17 + getLdapResult().hashCode();
075        }
076        hash = hash * 17 + super.hashCode();
077
078        return hash;
079    }
080
081
082    /**
083     * Checks to see if an object is equal to this AbstractResultResponse. First
084     * the object is checked to see if it is this AbstractResultResponse
085     * instance if so it returns true. Next it checks if the super method
086     * returns false and if it does false is returned. It then checks if the
087     * LDAPResult's are equal. If not false is returned and if they match true
088     * is returned.
089     * 
090     * @param obj
091     *            the object to compare to this LdapResult containing response
092     * @return true if they objects are equivalent false otherwise
093     */
094    @Override
095    public boolean equals( Object obj )
096    {
097        if ( obj == this )
098        {
099            return true;
100        }
101
102        if ( !super.equals( obj ) )
103        {
104            return false;
105        }
106
107        if ( !( obj instanceof ResultResponse ) )
108        {
109            return false;
110        }
111
112        ResultResponse resp = ( ResultResponse ) obj;
113
114        if ( ldapResult != null && resp.getLdapResult() == null )
115        {
116            return false;
117        }
118
119        if ( ldapResult == null && resp.getLdapResult() != null )
120        {
121            return false;
122        }
123
124        return ( ( ldapResult == null ) || ( resp.getLdapResult() == null ) || ldapResult.equals( resp.getLdapResult() ) );
125    }
126
127
128    /**
129     * Get a String representation of an Response
130     * 
131     * @return An Response String
132     */
133    public String toString()
134    {
135        StringBuilder sb = new StringBuilder();
136
137        sb.append( ldapResult );
138
139        if ( ( controls != null ) && ( controls.size() != 0 ) )
140        {
141            for ( Control control : controls.values() )
142            {
143                sb.append( control );
144            }
145        }
146
147        return sb.toString();
148    }
149}