View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  
23  /**
24   * Abstract base for a ResultResponse message.
25   * 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public abstract class AbstractResultResponse extends AbstractResponse implements ResultResponse
29  {
30      /** Response result components */
31      protected LdapResult ldapResult = new LdapResultImpl();
32  
33  
34      // ------------------------------------------------------------------------
35      // Response Interface Method Implementations
36      // ------------------------------------------------------------------------
37  
38      /**
39       * Allows subclasses based on the abstract type to create a response to a
40       * request.
41       * 
42       * @param id the response eliciting this Request
43       * @param type the message type of the response
44       */
45      protected AbstractResultResponse( final int id, final MessageTypeEnum type )
46      {
47          super( id, type );
48      }
49  
50  
51      // ------------------------------------------------------------------------
52      // Response Interface Method Implementations
53      // ------------------------------------------------------------------------
54      /**
55       * Gets the LdapResult components of this Response.
56       * 
57       * @return the LdapResult for this Response.
58       */
59      @Override
60      public LdapResult getLdapResult()
61      {
62          return ldapResult;
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public int hashCode()
71      {
72          int hash = 37;
73          if ( getLdapResult() != null )
74          {
75              hash = hash * 17 + getLdapResult().hashCode();
76          }
77          hash = hash * 17 + super.hashCode();
78  
79          return hash;
80      }
81  
82  
83      /**
84       * Checks to see if an object is equal to this AbstractResultResponse. First
85       * the object is checked to see if it is this AbstractResultResponse
86       * instance if so it returns true. Next it checks if the super method
87       * returns false and if it does false is returned. It then checks if the
88       * LDAPResult's are equal. If not false is returned and if they match true
89       * is returned.
90       * 
91       * @param obj
92       *            the object to compare to this LdapResult containing response
93       * @return true if they objects are equivalent false otherwise
94       */
95      @Override
96      public boolean equals( Object obj )
97      {
98          if ( obj == this )
99          {
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 }