KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > smi > EnumerationConstraint


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - EnumerationConstraint.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (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.agent.mo.snmp.smi;
22
23 import org.snmp4j.smi.*;
24 import java.util.Arrays JavaDoc;
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,
47                      this.allowedValues, 0, 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 }
62
Popular Tags