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      public LdapResult getLdapResult()
60      {
61          return ldapResult;
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public int hashCode()
70      {
71          int hash = 37;
72          if ( getLdapResult() != null )
73          {
74              hash = hash * 17 + getLdapResult().hashCode();
75          }
76          hash = hash * 17 + super.hashCode();
77  
78          return hash;
79      }
80  
81  
82      /**
83       * Checks to see if an object is equal to this AbstractResultResponse. First
84       * the object is checked to see if it is this AbstractResultResponse
85       * instance if so it returns true. Next it checks if the super method
86       * returns false and if it does false is returned. It then checks if the
87       * LDAPResult's are equal. If not false is returned and if they match true
88       * is returned.
89       * 
90       * @param obj
91       *            the object to compare to this LdapResult containing response
92       * @return true if they objects are equivalent false otherwise
93       */
94      @Override
95      public boolean equals( Object obj )
96      {
97          if ( obj == this )
98          {
99              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 }