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.controls;
021
022
023import org.apache.directory.api.ldap.model.name.Dn;
024
025
026/**
027 * A simple implementation of the EntryChange response control.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class EntryChangeImpl extends AbstractControl implements EntryChange
032{
033    /** The changeType */
034    private ChangeType changeType = ChangeType.ADD;
035
036    private long changeNumber = UNDEFINED_CHANGE_NUMBER;
037
038    /** The previous Dn */
039    private Dn previousDn = null;
040
041
042    /**
043     *
044     * Creates a new instance of EntryChangeControl.
045     *
046     */
047    public EntryChangeImpl()
048    {
049        super( OID );
050    }
051
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public ChangeType getChangeType()
058    {
059        return changeType;
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public void setChangeType( ChangeType changeType )
068    {
069        this.changeType = changeType;
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public Dn getPreviousDn()
078    {
079        return previousDn;
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public void setPreviousDn( Dn previousDn )
088    {
089        this.previousDn = previousDn;
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public long getChangeNumber()
098    {
099        return changeNumber;
100    }
101
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public void setChangeNumber( long changeNumber )
108    {
109        this.changeNumber = changeNumber;
110    }
111
112
113    /**
114     * @see Object#hashCode()
115     */
116    @Override
117    public int hashCode()
118    {
119        int h = super.hashCode();
120
121        h = h * 37 + ( int ) changeNumber;
122        h = h * 37 + ( changeType == null ? 0 : changeType.hashCode() );
123        h = h * 37 + ( previousDn == null ? 0 : previousDn.hashCode() );
124
125        return h;
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public boolean equals( Object o )
134    {
135        if ( !super.equals( o ) )
136        {
137            return false;
138        }
139
140        EntryChange otherControl = ( EntryChange ) o;
141
142        return ( changeNumber == otherControl.getChangeNumber() ) && ( changeType == otherControl.getChangeType() )
143            && ( previousDn.equals( otherControl.getPreviousDn() ) );
144    }
145
146
147    /**
148     * Return a String representing this EntryChangeControl.
149     */
150    @Override
151    public String toString()
152    {
153        StringBuilder sb = new StringBuilder();
154
155        sb.append( "    Entry Change Control\n" );
156        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
157        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
158        sb.append( "        changeType   : '" ).append( changeType ).append( "'\n" );
159        sb.append( "        previousDN   : '" ).append( previousDn ).append( "'\n" );
160
161        if ( changeNumber == UNDEFINED_CHANGE_NUMBER )
162        {
163            sb.append( "        changeNumber : '" ).append( "UNDEFINED" ).append( "'\n" );
164        }
165        else
166        {
167            sb.append( "        changeNumber : '" ).append( changeNumber ).append( "'\n" );
168        }
169
170        return sb.toString();
171    }
172}