KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > smi > VariantVariable


1 /*_############################################################################
2   _##
3   _## SNMP4J - VariantVariable.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.smi;
22
23 import org.snmp4j.asn1.BERInputStream;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 /**
28  * The <code>VariantVariable</code> provides a decorator for any type
29  * of Variable instance, to be able to intercept or monitor variable
30  * value modification by using a {@link VariantVariableCallback}.
31  * <p>
32  * This class will work for implementations that use {@link #getSyntax()}
33  * method to determine the variables syntax. However "instanceof" will not
34  * work.
35  * <p>
36  * In contrast to the native <code>Variable</code> implementations,
37  * <code>VariantVariable</code> can be modified dynamically (i.e. while
38  * a PDU is being BER encoded where this variable has been added to) without
39  * causing BER encoding errors.
40  *
41  * @author Frank Fock
42  * @version 1.8
43  * @since 1.7
44  */

45 public class VariantVariable extends AbstractVariable implements
46     AssignableFromInteger,
47     AssignableFromLong,
48     AssignableFromString,
49     AssignableFromByteArray {
50
51   private static final long serialVersionUID = -3678564678835871188L;
52
53   private Variable variable;
54   private VariantVariableCallback callback;
55
56   /**
57    * Creates a variant variable wrapping the specified value.
58    * @param initialVariable
59    * a <code>Variable</code>.
60    */

61   public VariantVariable(Variable initialVariable) {
62     if (initialVariable == null) {
63       throw new NullPointerException JavaDoc();
64     }
65     this.variable = initialVariable;
66   }
67
68   /**
69    * Creates a variant variable wrapping the specified value and a callback
70    * that monitors value modifications.
71    * @param initialVariable
72    * a <code>Variable</code>.
73    * @param callback
74    * a callback handler that is called before the value is to be modified
75    * and after it has been modified.
76    */

77   public VariantVariable(Variable initialVariable,
78                          VariantVariableCallback callback) {
79     this(initialVariable);
80     this.callback = callback;
81   }
82
83   public synchronized int compareTo(Object JavaDoc o) {
84     updateVariable();
85     return variable.compareTo(o);
86   }
87
88   protected void updateVariable() {
89     if (callback != null) {
90       callback.updateVariable(this);
91     }
92   }
93
94   protected void variableUpdated() {
95     if (callback != null) {
96       callback.variableUpdated(this);
97     }
98   }
99
100   public synchronized void decodeBER(BERInputStream inputStream) throws IOException JavaDoc {
101     variable.decodeBER(inputStream);
102     variableUpdated();
103   }
104
105   public synchronized void encodeBER(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
106     updateVariable();
107     variable.encodeBER(outputStream);
108   }
109
110   public synchronized void fromSubIndex(OID subIndex, boolean impliedLength) {
111     variable.fromSubIndex(subIndex, impliedLength);
112     variableUpdated();
113   }
114
115   public synchronized int getBERLength() {
116     updateVariable();
117     return variable.getBERLength();
118   }
119
120   public int getSyntax() {
121     return variable.getSyntax();
122   }
123
124   public synchronized int toInt() {
125     updateVariable();
126     return variable.toInt();
127   }
128
129   public synchronized long toLong() {
130     updateVariable();
131     return variable.toLong();
132   }
133
134   public synchronized byte[] toByteArray() {
135     updateVariable();
136     if (variable instanceof AssignableFromByteArray) {
137       return ((AssignableFromByteArray)variable).toByteArray();
138     }
139     throw new UnsupportedOperationException JavaDoc();
140   }
141
142   public synchronized OID toSubIndex(boolean impliedLength) {
143     updateVariable();
144     return variable.toSubIndex(impliedLength);
145   }
146
147   public synchronized boolean equals(Object JavaDoc o) {
148     updateVariable();
149     return variable.equals(o);
150   }
151
152   public synchronized int hashCode() {
153     updateVariable();
154     return variable.hashCode();
155   }
156
157   public synchronized String JavaDoc toString() {
158     updateVariable();
159     return variable.toString();
160   }
161
162   public Object JavaDoc clone() {
163     return new VariantVariable((Variable)variable.clone());
164   }
165
166   public synchronized void setValue(int value) {
167     if (variable instanceof AssignableFromInteger) {
168       ((AssignableFromInteger)variable).setValue(value);
169     }
170     else {
171       throw new ClassCastException JavaDoc("An integer value cannot be assigned to "+
172                                    variable);
173     }
174   }
175
176   public synchronized void setValue(long value) {
177     if (variable instanceof AssignableFromLong) {
178       ((AssignableFromLong)variable).setValue(value);
179     }
180     else {
181       throw new ClassCastException JavaDoc("A long value cannot be assigned to "+
182                                    variable);
183     }
184   }
185
186   public synchronized void setValue(OctetString value) {
187     if (variable instanceof AssignableFromByteArray) {
188       ((AssignableFromByteArray)variable).setValue(value.getValue());
189     }
190     else {
191       throw new ClassCastException JavaDoc("An OctetString value cannot be assigned to "+
192                                    variable);
193     }
194   }
195
196   public synchronized void setValue(byte[] value) {
197     if (variable instanceof AssignableFromByteArray) {
198       ((AssignableFromByteArray)variable).setValue(value);
199     }
200     else {
201       throw new ClassCastException JavaDoc("A byte array value cannot be assigned to "+
202                                    variable);
203     }
204   }
205
206   public synchronized void setValue(String JavaDoc value) {
207     if (variable instanceof AssignableFromString) {
208       ((AssignableFromString)variable).setValue(value);
209     }
210     else {
211       throw new ClassCastException JavaDoc("A string value cannot be assigned to "+
212                                    variable);
213     }
214   }
215
216   /**
217    * Returns the typed variable that holds the wrapped value.
218    * @return
219    * a Variable instance.
220    */

221   public Variable getVariable() {
222     return variable;
223   }
224
225   public boolean isDynamic() {
226     return true;
227   }
228
229 }
230
Popular Tags