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 */
020
021package org.apache.directory.api.ldap.extras.controls.vlv;
022
023
024import java.util.Arrays;
025
026import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * Virtual List View response control as specified in draft-ietf-ldapext-ldapv3-vlv-09.
032 * 
033 *  VirtualListViewResponse ::= SEQUENCE {
034 *         targetPosition    INTEGER (0 .. maxInt),
035 *         contentCount     INTEGER (0 .. maxInt),
036 *         virtualListViewResult ENUMERATED {
037 *              success (0),
038 *              operationsError (1),
039 *              protocolError (3),
040 *              unwillingToPerform (53),
041 *              insufficientAccessRights (50),
042 *              timeLimitExceeded (3),
043 *              adminLimitExceeded (11),
044 *              innapropriateMatching (18),
045 *              sortControlMissing (60),
046 *              offsetRangeError (61),
047 *              other(80),
048 *              ... },
049 *         contextID     OCTET STRING OPTIONAL }
050 *
051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
052 */
053public class VirtualListViewResponseImpl extends AbstractControl implements VirtualListViewResponse
054{
055
056    private int targetPosition;
057    private int contentCount;
058    private VirtualListViewResultCode virtualListViewResult;
059    private byte[] contextId;
060
061
062    /**
063     * Creates a new VirtualListViewResponseImpl instance
064     */
065    public VirtualListViewResponseImpl()
066    {
067        super( OID );
068    }
069
070
071    @Override
072    public int getTargetPosition()
073    {
074        return targetPosition;
075    }
076
077
078    @Override
079    public void setTargetPosition( int targetPosition )
080    {
081        this.targetPosition = targetPosition;
082    }
083
084
085    @Override
086    public int getContentCount()
087    {
088        return contentCount;
089    }
090
091
092    @Override
093    public void setContentCount( int contentCount )
094    {
095        this.contentCount = contentCount;
096    }
097
098
099    @Override
100    public VirtualListViewResultCode getVirtualListViewResult()
101    {
102        return virtualListViewResult;
103    }
104
105
106    @Override
107    public void setVirtualListViewResult( VirtualListViewResultCode virtualListViewResultCode )
108    {
109        this.virtualListViewResult = virtualListViewResultCode;
110    }
111
112
113    @Override
114    public byte[] getContextId()
115    {
116        return contextId;
117    }
118
119
120    @Override
121    public void setContextId( byte[] contextId )
122    {
123        this.contextId = contextId;
124    }
125
126
127    /**
128     * @see Object#hashCode()
129     */
130    @Override
131    public int hashCode()
132    {
133        int h = super.hashCode();
134
135        h = h * 37 + targetPosition;
136        h = h * 37 + contentCount;
137        h = h * 37 + ( ( virtualListViewResult == null ) ? 0 : virtualListViewResult.hashCode() );
138
139        if ( contextId != null )
140        {
141            for ( byte b : contextId )
142            {
143                h = h * 17 + b;
144            }
145        }
146
147        return h;
148    }
149
150
151    /**
152     * @see Object#equals(Object)
153     */
154    @Override
155    public boolean equals( Object o )
156    {
157        if ( !super.equals( o ) )
158        {
159            return false;
160        }
161
162        VirtualListViewResponseImpl otherControl = ( VirtualListViewResponseImpl ) o;
163
164        return ( targetPosition == otherControl.getTargetPosition() )
165            && ( contentCount == otherControl.getContentCount() )
166            && ( virtualListViewResult == otherControl.getVirtualListViewResult() )
167            && Arrays.equals( contextId, otherControl.getContextId() );
168    }
169
170
171    /**
172     * Return a String representing this VirtualListViewResponseImpl.
173     */
174    @Override
175    public String toString()
176    {
177        StringBuilder sb = new StringBuilder();
178
179        sb.append( "    Virtual List View Response Control\n" );
180        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
181        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
182        sb.append( "        targetPosition   : '" ).append( targetPosition ).append( "'\n" );
183        sb.append( "        contentCount   : '" ).append( contentCount ).append( "'\n" );
184        sb.append( "        virtualListViewResult   : '" ).append( virtualListViewResult ).append( "'\n" );
185
186        if ( contextId != null )
187        {
188            sb.append( "        contextID   : '" ).append( Strings.dumpBytes( contextId ) ).append( "'\n" );
189        }
190
191        return sb.toString();
192    }
193}