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  import org.apache.directory.api.ldap.model.entry.DefaultEntry;
24  import org.apache.directory.api.ldap.model.entry.Entry;
25  import org.apache.directory.api.ldap.model.name.Dn;
26  
27  
28  /**
29   * Lockable SearchResponseEntry implementation
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class SearchResultEntryImpl extends AbstractResponse implements SearchResultEntry
34  {
35      static final long serialVersionUID = -8357316233060886637L;
36  
37      /** Entry returned in response to search */
38      private Entry entry = new DefaultEntry();
39  
40  
41      /**
42       * Creates a SearchResponseEntry as a reply to an SearchRequest to
43       * indicate the end of a search operation.
44       */
45      public SearchResultEntryImpl()
46      {
47          super( -1, MessageTypeEnum.SEARCH_RESULT_ENTRY );
48      }
49  
50  
51      /**
52       * Creates a SearchResponseEntry as a reply to an SearchRequest to
53       * indicate the end of a search operation.
54       * 
55       * @param id the session unique message id
56       */
57      public SearchResultEntryImpl( final int id )
58      {
59          super( id, MessageTypeEnum.SEARCH_RESULT_ENTRY );
60      }
61  
62  
63      // ------------------------------------------------------------------------
64      // SearchResponseEntry Interface Method Implementations
65      // ------------------------------------------------------------------------
66  
67      /**
68       * Gets the entry
69       * 
70       * @return the entry
71       */
72      public Entry getEntry()
73      {
74          return entry;
75      }
76  
77  
78      /**
79       * Sets the entry.
80       * 
81       * @param entry the entry
82       */
83      public void setEntry( Entry entry )
84      {
85          this.entry = entry;
86      }
87  
88  
89      /**
90       * Gets the distinguished name of the entry object returned.
91       * 
92       * @return the Dn of the entry returned.
93       */
94      public Dn getObjectName()
95      {
96          return ( entry == null ? null : entry.getDn() );
97      }
98  
99  
100     /**
101      * Sets the distinguished name of the entry object returned.
102      * 
103      * @param objectName the Dn of the entry returned.
104      */
105     public void setObjectName( Dn objectName )
106     {
107         if ( entry != null )
108         {
109             entry.setDn( objectName );
110         }
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public int hashCode()
119     {
120         int hash = 37;
121         if ( entry != null )
122         {
123             hash = hash * 17 + entry.hashCode();
124         }
125         hash = hash * 17 + super.hashCode();
126 
127         return hash;
128     }
129 
130 
131     /**
132      * Checks for equality by comparing the objectName, and attributes
133      * properties of this Message after delegating to the super.equals() method.
134      * 
135      * @param obj
136      *            the object to test for equality with this message
137      * @return true if the obj is equal false otherwise
138      */
139     public boolean equals( Object obj )
140     {
141         if ( this == obj )
142         {
143             return true;
144         }
145 
146         if ( !super.equals( obj ) )
147         {
148             return false;
149         }
150 
151         if ( !( obj instanceof SearchResultEntry ) )
152         {
153             return false;
154         }
155 
156         SearchResultEntry resp = ( SearchResultEntry ) obj;
157 
158         return entry.equals( resp.getEntry() );
159     }
160 
161 
162     /**
163      * Return a string representation of a SearchResultEntry request
164      */
165     public String toString()
166     {
167         StringBuilder sb = new StringBuilder();
168 
169         sb.append( "    Search Result Entry\n" );
170 
171         if ( entry != null )
172         {
173             sb.append( entry );
174         }
175         else
176         {
177             sb.append( "            No entry\n" );
178         }
179 
180         return super.toString( sb.toString() );
181     }
182 }