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.message.Control;
024
025
026/**
027 * A simple implementation of the {@link Control} interface with storage for 
028 * the OID and the criticality properties. When the codec factory service
029 * does not have specific control factories available, hence the control is
030 * unrecognized, it creates instances of this control for them.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public abstract class AbstractControl implements Control
035{
036    /** The control type */
037    private String oid;
038
039    /** The criticality (default value is false) */
040    private boolean criticality = false;
041
042
043    /**
044     * Creates a Control with a specific OID.
045     *
046     * @param oid The OID of this Control.
047     */
048    public AbstractControl( String oid )
049    {
050        this.oid = oid;
051    }
052
053
054    /**
055     * Creates a Control with a specific OID, and criticality set.
056     *
057     * @param oid The OID of this Control.
058     * @param criticality true if this Control is critical, false otherwise. 
059     */
060    public AbstractControl( String oid, boolean criticality )
061    {
062        this.oid = oid;
063        this.criticality = criticality;
064    }
065
066
067    /**
068     * Get the OID
069     * 
070     * @return A string which represent the control oid
071     */
072    public String getOid()
073    {
074        return oid == null ? "" : oid;
075    }
076
077
078    /**
079     * Get the criticality
080     * 
081     * @return <code>true</code> if the criticality flag is true.
082     */
083    public boolean isCritical()
084    {
085        return criticality;
086    }
087
088
089    /**
090     * Set the criticality
091     * 
092     * @param criticality The criticality value
093     */
094    public void setCritical( boolean criticality )
095    {
096        this.criticality = criticality;
097    }
098
099
100    /**
101     * @see Object#hashCode()
102     */
103    public int hashCode()
104    {
105        int h = 17;
106        h = h * 37 + ( criticality ? 1 : 0 );
107        h = h * 37 + ( oid == null ? 0 : oid.hashCode() );
108
109        return h;
110    }
111
112
113    /**
114     * @see Object#equals(Object)
115     */
116    public boolean equals( Object o )
117    {
118        if ( o == this )
119        {
120            return true;
121        }
122
123        if ( o == null )
124        {
125            return false;
126        }
127
128        if ( !( o instanceof Control ) )
129        {
130            return false;
131        }
132
133        Control otherControl = ( Control ) o;
134
135        if ( !oid.equalsIgnoreCase( otherControl.getOid() ) )
136        {
137            return false;
138        }
139
140        return criticality == otherControl.isCritical();
141    }
142
143
144    /**
145     * Return a String representing a Control
146     */
147    public String toString()
148    {
149        StringBuffer sb = new StringBuffer();
150
151        sb.append( "    " ).append( getClass().getSimpleName() ).append( " " );
152        sb.append( "Control\n" );
153        sb.append( "        Type OID    : '" ).append( oid ).append( "'\n" );
154        sb.append( "        Criticality : '" ).append( criticality ).append( "'\n" );
155
156        sb.append( "'\n" );
157
158        return sb.toString();
159    }
160}