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.extras.extended.endTransaction;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.directory.api.ldap.model.message.Control;
026
027/**
028 * The interface for End Transaction Extended Response UpdateControl. It's described in RFC 5805 :
029 * 
030 * <pre>
031 * updateControls SEQUENCE {
032 *     messageID MessageID,
033 *               -- msgid associated with controls
034 *     controls  Controls
035 * } OPTIONAL
036 * </pre>
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class UpdateControls
041{
042    /** The message ID for which we want to get back the controls */
043    private int messageId;
044
045    /** The list of controls (may be empty) */
046    private List<Control> controls = new ArrayList<>();
047    
048    /**
049     * A default constructor for the UpdateControls class
050     */
051    public UpdateControls()
052    {
053        // Nothing to do
054    }
055    
056    
057    /**
058     * @return The messageID
059     */
060    public int getMessageId()
061    {
062        return messageId;
063    }
064    
065    
066    /**
067     * @param messageId the messageId to set
068     */
069    public void setMessageId( int messageId )
070    {
071        this.messageId = messageId;
072    }
073   
074   
075    /**
076     * @return The set of controls associated with the messageID
077     */
078    public List<Control> getControls()
079    {
080        return controls;
081    }
082
083
084    /**
085     * @param controls the controls to set
086     */
087    public void setControls( List<Control> controls )
088    {
089        this.controls = controls;
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public int hashCode()
098    {
099        int hash = 37;
100
101        hash = hash * 17 + messageId;
102        
103        for ( Control control : controls )
104        {
105            hash = hash * 17 + control.hashCode();
106        }
107
108        return hash;
109    }
110
111
112    /**
113     * @see Object#equals(Object)
114     */
115    @Override
116    public boolean equals( Object obj )
117    {
118        if ( obj == this )
119        {
120            return true;
121        }
122
123        if ( !( obj instanceof UpdateControls ) )
124        {
125            return false;
126        }
127        
128        UpdateControls that = ( UpdateControls ) obj;
129        
130        if ( messageId != that.getMessageId() )
131        {
132            return false;
133        }
134        
135        if ( controls.size() != that.getControls().size() )
136        {
137            return false;
138        }
139        
140        for ( Control control : controls )
141        {
142            if ( !that.getControls().contains( control ) )
143            {
144                return false;
145            }
146        }
147        
148        return true;
149    }
150
151
152    /**
153     * @see Object#toString()
154     */
155    @Override
156    public String toString()
157    {
158        StringBuilder sb = new StringBuilder();
159
160        sb.append( "UpdateControl :" );
161        sb.append( "\n    messageId : " ).append( messageId );
162
163        if ( controls.isEmpty() )
164        {
165            sb.append( "\n    No controls" );
166        }
167        else
168        {
169            sb.append( "\n    Controls: [" );
170            boolean isFirst = true;
171            
172            for ( Control control : controls )
173            {
174                if ( isFirst )
175                {
176                    isFirst = false;
177                }
178                else
179                {
180                    sb.append( ", " );
181                }
182                
183                sb.append( control.getOid() );
184            }
185            
186            sb.append( ']' );
187        }
188
189        return sb.toString();
190    }
191}