001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.message;
021
022
023import org.apache.directory.api.ldap.model.entry.DefaultEntry;
024import org.apache.directory.api.ldap.model.entry.Entry;
025import org.apache.directory.api.ldap.model.name.Dn;
026
027
028/**
029 * Lockable SearchResponseEntry implementation
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class SearchResultEntryImpl extends AbstractResponse implements SearchResultEntry
034{
035    static final long serialVersionUID = -8357316233060886637L;
036
037    /** Entry returned in response to search */
038    private Entry entry = new DefaultEntry();
039
040
041    /**
042     * Creates a SearchResponseEntry as a reply to an SearchRequest to
043     * indicate the end of a search operation.
044     */
045    public SearchResultEntryImpl()
046    {
047        super( -1, MessageTypeEnum.SEARCH_RESULT_ENTRY );
048    }
049
050
051    /**
052     * Creates a SearchResponseEntry as a reply to an SearchRequest to
053     * indicate the end of a search operation.
054     * 
055     * @param id the session unique message id
056     */
057    public SearchResultEntryImpl( final int id )
058    {
059        super( id, MessageTypeEnum.SEARCH_RESULT_ENTRY );
060    }
061
062
063    // ------------------------------------------------------------------------
064    // SearchResponseEntry Interface Method Implementations
065    // ------------------------------------------------------------------------
066
067    /**
068     * Gets the entry
069     * 
070     * @return the entry
071     */
072    @Override
073    public Entry getEntry()
074    {
075        return entry;
076    }
077
078
079    /**
080     * Sets the entry.
081     * 
082     * @param entry the entry
083     */
084    @Override
085    public void setEntry( Entry entry )
086    {
087        this.entry = entry;
088    }
089
090
091    /**
092     * Gets the distinguished name of the entry object returned.
093     * 
094     * @return the Dn of the entry returned.
095     */
096    @Override
097    public Dn getObjectName()
098    {
099        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}