View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message.controls;
21  
22  
23  import org.apache.directory.api.ldap.model.message.Control;
24  
25  
26  /**
27   * A simple implementation of the {@link Control} interface with storage for 
28   * the OID and the criticality properties. When the codec factory service
29   * does not have specific control factories available, hence the control is
30   * unrecognized, it creates instances of this control for them.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public abstract class AbstractControl implements Control
35  {
36      /** The control type */
37      private String oid;
38  
39      /** The criticality (default value is false) */
40      private boolean criticality = false;
41  
42  
43      /**
44       * Creates a Control with a specific OID.
45       *
46       * @param oid The OID of this Control.
47       */
48      public AbstractControl( String oid )
49      {
50          this.oid = oid;
51      }
52  
53  
54      /**
55       * Creates a Control with a specific OID, and criticality set.
56       *
57       * @param oid The OID of this Control.
58       * @param criticality true if this Control is critical, false otherwise. 
59       */
60      public AbstractControl( String oid, boolean criticality )
61      {
62          this.oid = oid;
63          this.criticality = criticality;
64      }
65  
66  
67      /**
68       * Get the OID
69       * 
70       * @return A string which represent the control oid
71       */
72      public String getOid()
73      {
74          return oid == null ? "" : oid;
75      }
76  
77  
78      /**
79       * Get the criticality
80       * 
81       * @return <code>true</code> if the criticality flag is true.
82       */
83      public boolean isCritical()
84      {
85          return criticality;
86      }
87  
88  
89      /**
90       * Set the criticality
91       * 
92       * @param criticality The criticality value
93       */
94      public void setCritical( boolean criticality )
95      {
96          this.criticality = criticality;
97      }
98  
99  
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 }