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      @Override
73      public Entry getEntry()
74      {
75          return entry;
76      }
77  
78  
79      /**
80       * Sets the entry.
81       * 
82       * @param entry the entry
83       */
84      @Override
85      public void setEntry( Entry entry )
86      {
87          this.entry = entry;
88      }
89  
90  
91      /**
92       * Gets the distinguished name of the entry object returned.
93       * 
94       * @return the Dn of the entry returned.
95       */
96      @Override
97      public Dn getObjectName()
98      {
99          return entry == null ? null : entry.getDn();
100     }
101 
102 
103     /**
104      * Sets the distinguished name of the entry object returned.
105      * 
106      * @param objectName the Dn of the entry returned.
107      */
108     @Override
109     public void setObjectName( Dn objectName )
110     {
111         if ( entry != null )
112         {
113             entry.setDn( objectName );
114         }
115     }
116 
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public int hashCode()
123     {
124         int hash = 37;
125         if ( entry != null )
126         {
127             hash = hash * 17 + entry.hashCode();
128         }
129         hash = hash * 17 + super.hashCode();
130 
131         return hash;
132     }
133 
134 
135     /**
136      * Checks for equality by comparing the objectName, and attributes
137      * properties of this Message after delegating to the super.equals() method.
138      * 
139      * @param obj
140      *            the object to test for equality with this message
141      * @return true if the obj is equal false otherwise
142      */
143     @Override
144     public boolean equals( Object obj )
145     {
146         if ( this == obj )
147         {
148             return true;
149         }
150 
151         if ( !super.equals( obj ) )
152         {
153             return false;
154         }
155 
156         if ( !( obj instanceof SearchResultEntry ) )
157         {
158             return false;
159         }
160 
161         SearchResultEntry resp = ( SearchResultEntry ) obj;
162 
163         return entry.equals( resp.getEntry() );
164     }
165 
166 
167     /**
168      * Return a string representation of a SearchResultEntry request
169      */
170     @Override
171     public String toString()
172     {
173         StringBuilder sb = new StringBuilder();
174 
175         sb.append( "    Search Result Entry\n" );
176 
177         if ( entry != null )
178         {
179             sb.append( entry );
180         }
181         else
182         {
183             sb.append( "            No entry\n" );
184         }
185 
186         return super.toString( sb.toString() );
187     }
188 }