KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > util > IndexGenerator


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - IndexGenerator.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.util;
22
23 import org.snmp4j.smi.Variable;
24 import org.snmp4j.smi.OID;
25 import org.snmp4j.smi.Integer32;
26 import org.snmp4j.smi.UnsignedInteger32;
27 import org.snmp4j.smi.Counter64;
28
29 /**
30  * The <code>IndexGenerator</code> class can be used to generate a sequence
31  * of unique index values.
32  *
33  * @author Frank Fock
34  * @version 1.0
35  */

36 public class IndexGenerator {
37
38   private Variable seedSubIndex;
39   private boolean impliedLength;
40
41   /**
42    * Creates a sub-index sequence generator based on a seed value.
43    * @param seedSubIndex
44    * the seed value to start with (the seed itself will never be returned).
45    */

46   public IndexGenerator(Variable seedSubIndex) {
47     if (!((seedSubIndex instanceof Integer32) ||
48           (seedSubIndex instanceof UnsignedInteger32) ||
49           (seedSubIndex instanceof Counter64))) {
50       throw new IllegalArgumentException JavaDoc("A seed subindex of type "+
51                                          seedSubIndex.getSyntaxString()+
52                                          "is not supported");
53     }
54     this.seedSubIndex = seedSubIndex;
55   }
56
57   /**
58    * Creates a sub-index sequence generator based on a seed value.
59    * @param seedSubIndex
60    * the seed value to start with (the seed itself will never be returned).
61    * @param impliedLength
62    * if <code>true</code> the length will not be included in the returned
63    * sub-index for variable-length types.
64    */

65   public IndexGenerator(Variable seedSubIndex, boolean impliedLength) {
66     this(seedSubIndex);
67     this.impliedLength = impliedLength;
68   }
69
70   /**
71    * Creates the next sub-index OID value.
72    * @return
73    * a sub-index value corresponding to the seed sub-index type.
74    */

75   public synchronized OID getNextSubIndex() {
76     if (seedSubIndex instanceof Integer32) {
77       Integer32 i = (Integer32)seedSubIndex;
78       i.setValue(i.getValue()+1);
79     }
80     else if (seedSubIndex instanceof UnsignedInteger32) {
81       UnsignedInteger32 ui = (UnsignedInteger32)seedSubIndex;
82       ui.setValue(ui.getValue()+1);
83     }
84     else if (seedSubIndex instanceof Counter64) {
85       Counter64 c = (Counter64)seedSubIndex;
86       c.setValue(c.getValue()+1);
87     }
88     return seedSubIndex.toSubIndex(impliedLength);
89   }
90 }
91
Popular Tags