1 20 21 package org.snmp4j.smi; 22 23 import org.snmp4j.asn1.BERInputStream; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 27 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 61 public VariantVariable(Variable initialVariable) { 62 if (initialVariable == null) { 63 throw new NullPointerException (); 64 } 65 this.variable = initialVariable; 66 } 67 68 77 public VariantVariable(Variable initialVariable, 78 VariantVariableCallback callback) { 79 this(initialVariable); 80 this.callback = callback; 81 } 82 83 public synchronized int compareTo(Object 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 { 101 variable.decodeBER(inputStream); 102 variableUpdated(); 103 } 104 105 public synchronized void encodeBER(OutputStream outputStream) throws IOException { 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 (); 140 } 141 142 public synchronized OID toSubIndex(boolean impliedLength) { 143 updateVariable(); 144 return variable.toSubIndex(impliedLength); 145 } 146 147 public synchronized boolean equals(Object 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 toString() { 158 updateVariable(); 159 return variable.toString(); 160 } 161 162 public Object 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 ("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 ("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 ("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 ("A byte array value cannot be assigned to "+ 202 variable); 203 } 204 } 205 206 public synchronized void setValue(String value) { 207 if (variable instanceof AssignableFromString) { 208 ((AssignableFromString)variable).setValue(value); 209 } 210 else { 211 throw new ClassCastException ("A string value cannot be assigned to "+ 212 variable); 213 } 214 } 215 216 221 public Variable getVariable() { 222 return variable; 223 } 224 225 public boolean isDynamic() { 226 return true; 227 } 228 229 } 230 | Popular Tags |