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    @Override
073    public String getOid()
074    {
075        return oid == null ? "" : oid;
076    }
077
078
079    /**
080     * Get the criticality
081     * 
082     * @return <code>true</code> if the criticality flag is true.
083     */
084    @Override
085    public boolean isCritical()
086    {
087        return criticality;
088    }
089
090
091    /**
092     * Set the criticality
093     * 
094     * @param criticality The criticality value
095     */
096    @Override
097    public void setCritical( boolean criticality )
098    {
099        this.criticality = criticality;
100    }
101
102
103    /**
104     * @see Object#hashCode()
105     */
106    @Override
107    public int hashCode()
108    {
109        int h = 17;
110        h = h * 37 + ( criticality ? 1 : 0 );
111        h = h * 37 + ( oid == null ? 0 : oid.hashCode() );
112
113        return h;
114    }
115
116
117    /**
118     * @see Object#equals(Object)
119     */
120    @Override
121    public boolean equals( Object o )
122    {
123        if ( o == this )
124        {
125            return true;
126        }
127
128        if ( o == null )
129        {
130            return false;
131        }
132
133        if ( !( o instanceof Control ) )
134        {
135            return false;
136        }
137
138        Control otherControl = ( Control ) o;
139
140        if ( !oid.equalsIgnoreCase( otherControl.getOid() ) )
141        {
142            return false;
143        }
144
145        return criticality == otherControl.isCritical();
146    }
147
148
149    /**
150     * Return a String representing a Control
151     */
152    @Override
153    public String toString()
154    {
155        StringBuilder sb = new StringBuilder();
156
157        sb.append( "    " ).append( getClass().getSimpleName() ).append( " " );
158        sb.append( "Control\n" );
159        sb.append( "        Type OID    : '" ).append( oid ).append( "'\n" );
160        sb.append( "        Criticality : '" ).append( criticality ).append( "'\n" );
161
162        sb.append( "'\n" );
163
164        return sb.toString();
165    }
166}