1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.ldif;
21
22
23 import java.io.Externalizable;
24 import java.io.IOException;
25 import java.io.ObjectInput;
26 import java.io.ObjectOutput;
27
28 import org.apache.directory.api.ldap.model.message.Control;
29 import org.apache.directory.api.util.Strings;
30
31
32
33
34
35
36
37
38 public class LdifControl implements Control, Externalizable
39 {
40
41 private String oid;
42
43
44 private boolean criticality = false;
45
46
47 protected byte[] value;
48
49
50
51
52
53 public LdifControl()
54 {
55 }
56
57
58
59
60
61
62
63 public LdifControl( String oid )
64 {
65 this.oid = oid;
66 }
67
68
69
70
71
72 @Override
73 public String toString()
74 {
75 return "LdifControl : {" + getOid() + ", " + isCritical() + ", " + Strings.dumpBytes( getValue() ) + "}";
76 }
77
78
79
80
81
82 @Override
83 public String getOid()
84 {
85 return oid;
86 }
87
88
89
90
91
92 @Override
93 public boolean isCritical()
94 {
95 return criticality;
96 }
97
98
99
100
101
102 @Override
103 public void setCritical( boolean criticality )
104 {
105 this.criticality = criticality;
106 }
107
108
109
110
111
112
113
114 public byte[] getValue()
115 {
116 return value;
117 }
118
119
120
121
122
123
124
125 public void setValue( byte[] value )
126 {
127 this.value = value;
128 }
129
130
131
132
133
134 public boolean hasValue()
135 {
136 return value != null;
137 }
138
139
140
141
142
143 @Override
144 public void writeExternal( ObjectOutput out ) throws IOException
145 {
146 out.writeUTF( oid );
147 out.writeBoolean( criticality );
148
149 if ( hasValue() )
150 {
151 out.writeBoolean( true );
152 out.writeInt( value.length );
153
154 if ( value.length > 0 )
155 {
156 out.write( value );
157 }
158 }
159 else
160 {
161 out.writeBoolean( false );
162 }
163
164 out.flush();
165 }
166
167
168
169
170
171 @Override
172 public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
173 {
174 oid = in.readUTF();
175 criticality = in.readBoolean();
176
177 if ( in.readBoolean() )
178 {
179 int valueLength = in.readInt();
180
181 if ( valueLength > 0 )
182 {
183 value = new byte[valueLength];
184 in.readFully( value );
185 }
186 }
187 }
188
189
190
191
192
193 @Override
194 public int hashCode()
195 {
196 int h = 17;
197 h = h * 37 + ( criticality ? 1 : 0 );
198 h = h * 37 + ( oid == null ? 0 : oid.hashCode() );
199
200 if ( value != null )
201 {
202 for ( byte v : value )
203 {
204 h = h * 37 + v;
205 }
206 }
207
208 return h;
209 }
210
211
212
213
214
215 @Override
216 public boolean equals( Object o )
217 {
218 if ( o == this )
219 {
220 return true;
221 }
222
223 if ( o == null )
224 {
225 return false;
226 }
227
228 if ( !( o instanceof Control ) )
229 {
230 return false;
231 }
232
233 Control otherControl = ( Control ) o;
234
235 if ( !oid.equalsIgnoreCase( otherControl.getOid() ) )
236 {
237 return false;
238 }
239
240 return criticality == otherControl.isCritical();
241 }
242 }