KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - ConstraintsImpl.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
22 package org.snmp4j.agent.mo.snmp.smi;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.*;
27 import org.snmp4j.smi.Variable;
28 import org.snmp4j.PDU;
29 import org.snmp4j.smi.OctetString;
30 import org.snmp4j.smi.UnsignedInteger32;
31 import org.snmp4j.smi.Integer32;
32
33 /**
34  * The <code>ConstraintsImpl</code> class represents a collection of constraints
35  * that are applied to a SNMP value in the order they have been added to this
36  * constraints collection.
37  *
38  * @author Frank Fock
39  * @version 1.0
40  */

41 public class ConstraintsImpl implements Constraints {
42
43   private List JavaDoc constraints = new ArrayList JavaDoc();
44
45   /**
46    * Creates a new constraints collection.
47    */

48   public ConstraintsImpl() {
49   }
50
51   public void add(Constraint sizeConstraint) {
52     constraints.add(sizeConstraint);
53   }
54
55   public void remove(Constraint sizeContraint) {
56     constraints.remove(sizeContraint);
57   }
58
59   public Constraint[] getConstraints() {
60     return (Constraint[])
61         constraints.toArray(new Constraint[constraints.size()]);
62   }
63
64   public boolean isValidSize(long size) {
65     if (constraints.size() > 0) {
66       for (Iterator it = constraints.iterator(); it.hasNext(); ) {
67         Constraint sc = (Constraint) it.next();
68         if ((size >= sc.getLowerBound()) && (size <= sc.getUpperBound())) {
69           return true;
70         }
71       }
72       return false;
73     }
74     else {
75       return true;
76     }
77   }
78
79   public int validate(Variable variable) {
80     if (variable instanceof OctetString) {
81       if (!isValidSize(((OctetString)variable).length())) {
82         return PDU.wrongLength;
83       }
84     }
85     else if (variable instanceof Integer32) {
86       Integer32 i = (Integer32)variable;
87       if (!isValidSize(i.getValue())) {
88         return PDU.wrongValue;
89       }
90     }
91     else if (variable instanceof UnsignedInteger32) {
92       UnsignedInteger32 ui = (UnsignedInteger32)variable;
93       if (!isValidSize(ui.getValue())) {
94        return PDU.wrongValue;
95       }
96     }
97     return PDU.noError;
98   }
99
100 }
101
Popular Tags