01: /*_############################################################################
02: _##
03: _## SNMP4J-Agent - EnumerationConstraint.java
04: _##
05: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
06: _##
07: _## Licensed under the Apache License, Version 2.0 (the "License");
08: _## you may not use this file except in compliance with the License.
09: _## 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.agent.mo.snmp.smi;
22:
23: import org.snmp4j.smi.*;
24: import java.util.Arrays;
25: import org.snmp4j.PDU;
26:
27: /**
28: * The <code>EnumerationConstraint</code> class checks an <code>Integer32</code>
29: * value to match a set of (enumerated) values.
30: *
31: * @author Frank Fock
32: * @version 1.0
33: */
34: public class EnumerationConstraint implements ValueConstraint {
35:
36: private int[] allowedValues;
37:
38: /**
39: * Creates an <code>EnumerationConstraint</code> based on the specified array
40: * of integer values.
41: * @param allowedValues
42: * an array of allowed values.
43: */
44: public EnumerationConstraint(int[] allowedValues) {
45: this .allowedValues = new int[allowedValues.length];
46: System.arraycopy(allowedValues, 0, this .allowedValues, 0,
47: allowedValues.length);
48: Arrays.sort(this .allowedValues);
49: }
50:
51: public int validate(Variable variable) {
52: if (variable instanceof Integer32) {
53: if (Arrays.binarySearch(allowedValues,
54: ((Integer32) variable).getValue()) < 0) {
55: return PDU.wrongValue;
56: }
57: return PDU.noError;
58: }
59: return PDU.wrongType;
60: }
61: }
|