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      @Override
73      public String getOid()
74      {
75          return oid == null ? "" : oid;
76      }
77  
78  
79      /**
80       * Get the criticality
81       * 
82       * @return <code>true</code> if the criticality flag is true.
83       */
84      @Override
85      public boolean isCritical()
86      {
87          return criticality;
88      }
89  
90  
91      /**
92       * Set the criticality
93       * 
94       * @param criticality The criticality value
95       */
96      @Override
97      public void setCritical( boolean criticality )
98      {
99          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 }