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   * A simple ExtendedResponse implementation.
25   * 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public class ExtendedResponseImpl extends AbstractResultResponse implements ExtendedResponse
29  {
30      static final long serialVersionUID = -6646752766410531060L;
31  
32      /** Object identifier for the extended response */
33      protected String responseName;
34  
35  
36      /**
37       * Creates an ExtendedResponse as a reply to an ExtendedRequest.
38       * 
39       * @param responseName the ExtendedResponse's name
40       */
41      public ExtendedResponseImpl( String responseName )
42      {
43          super( -1, TYPE );
44          this.responseName = responseName;
45      }
46  
47  
48      /**
49       * Creates an ExtendedResponse as a reply to an ExtendedRequest.
50       * 
51       * @param id the session unique message id
52       * @param responseName the ExtendedResponse's name
53       */
54      public ExtendedResponseImpl( final int id, String responseName )
55      {
56          super( id, TYPE );
57          this.responseName = responseName;
58      }
59  
60  
61      /**
62       * Creates an ExtendedResponse as a reply to an ExtendedRequest.
63       * 
64       * @param id the session unique message id
65       */
66      public ExtendedResponseImpl( int id )
67      {
68          super( id, TYPE );
69      }
70  
71  
72      // ------------------------------------------------------------------------
73      // ExtendedResponse Interface Method Implementations
74      // ------------------------------------------------------------------------
75  
76      /**
77       * Gets the OID uniquely identifying this extended response (a.k.a. its
78       * name).
79       * 
80       * @return the responseName of the extended response
81       */
82      @Override
83      public String getResponseName()
84      {
85          return ( responseName == null ) ? "" : responseName;
86      }
87  
88  
89      /**
90       * Sets the OID uniquely identifying this extended response (a.k.a. its
91       * name).
92       * 
93       * @param responseName the OID of the extended response type.
94       */
95      @Override
96      public void setResponseName( String responseName )
97      {
98          this.responseName = responseName;
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public int hashCode()
107     {
108         int hash = 37;
109 
110         if ( responseName != null )
111         {
112             hash = hash * 17 + responseName.hashCode();
113         }
114 
115         hash = hash * 17 + super.hashCode();
116 
117         return hash;
118     }
119 
120 
121     /**
122      * Checks to see if an object equals this ExtendedRequest.
123      * 
124      * @param obj
125      *            the object to be checked for equality
126      * @return true if the obj equals this ExtendedRequest, false otherwise
127      */
128     @Override
129     public boolean equals( Object obj )
130     {
131         if ( obj == this )
132         {
133             return true;
134         }
135 
136         if ( !super.equals( obj ) )
137         {
138             return false;
139         }
140 
141         if ( !( obj instanceof ExtendedResponse ) )
142         {
143             return false;
144         }
145 
146         ExtendedResponse resp = ( ExtendedResponse ) obj;
147 
148         if ( ( responseName != null ) && ( resp.getResponseName() == null ) )
149         {
150             return false;
151         }
152 
153         if ( ( responseName == null ) && ( resp.getResponseName() != null ) )
154         {
155             return false;
156         }
157 
158         if ( ( responseName != null ) && ( resp.getResponseName() != null )
159             && !responseName.equals( resp.getResponseName() ) )
160         {
161             return false;
162         }
163 
164         return true;
165     }
166 
167 
168     /**
169      * Get a String representation of an ExtendedResponse
170      * 
171      * @return An ExtendedResponse String
172      */
173     @Override
174     public String toString()
175     {
176         StringBuilder sb = new StringBuilder();
177 
178         sb.append( "    Extended Response\n" );
179 
180         if ( responseName != null )
181         {
182             sb.append( "        ResponseName :'" ).append( responseName ).append( "'\n" );
183         }
184 
185         sb.append( super.toString() );
186 
187         return super.toString( sb.toString() );
188     }
189 }