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    @Override
060    public LdapResult getLdapResult()
061    {
062        return ldapResult;
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public int hashCode()
071    {
072        int hash = 37;
073        if ( getLdapResult() != null )
074        {
075            hash = hash * 17 + getLdapResult().hashCode();
076        }
077        hash = hash * 17 + super.hashCode();
078
079        return hash;
080    }
081
082
083    /**
084     * Checks to see if an object is equal to this AbstractResultResponse. First
085     * the object is checked to see if it is this AbstractResultResponse
086     * instance if so it returns true. Next it checks if the super method
087     * returns false and if it does false is returned. It then checks if the
088     * LDAPResult's are equal. If not false is returned and if they match true
089     * is returned.
090     * 
091     * @param obj
092     *            the object to compare to this LdapResult containing response
093     * @return true if they objects are equivalent false otherwise
094     */
095    @Override
096    public boolean equals( Object obj )
097    {
098        if ( obj == this )
099        {
100            return true;
101        }
102
103        if ( !super.equals( obj ) )
104        {
105            return false;
106        }
107
108        if ( !( obj instanceof ResultResponse ) )
109        {
110            return false;
111        }
112
113        ResultResponse resp = ( ResultResponse ) obj;
114        
115        return ( ( ldapResult != null ) && ldapResult.equals( resp.getLdapResult() ) ) 
116            || ( resp.getLdapResult() == null );
117    }
118
119
120    /**
121     * Get a String representation of an Response
122     * 
123     * @return An Response String
124     */
125    @Override
126    public String toString()
127    {
128        StringBuilder sb = new StringBuilder();
129
130        sb.append( ldapResult );
131
132        if ( ( controls != null ) && ( controls.size() != 0 ) )
133        {
134            for ( Control control : controls.values() )
135            {
136                sb.append( control );
137            }
138        }
139
140        return sb.toString();
141    }
142}