KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > agent > SnmpIndex


1 /*
2  * @(#)file SnmpIndex.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.8
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp.agent;
14
15
16
17 // java imports
18
//
19
import java.io.Serializable JavaDoc;
20 import java.util.Vector JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 // jmx imports
24
//
25
import com.sun.jmx.snmp.SnmpOid;
26
27 /**
28  * Represents a SNMP index.
29  * An <CODE>SnmpIndex</CODE> is represented as a <CODE>Vector</CODE> of <CODE>SnmpOid</CODE>.
30  * <P>
31  * This class is used internally and by the classes generated by <CODE>mibgen</CODE>.
32  * You should not need to use this class directly.
33  *
34  * <p><b>This API is a Sun Microsystems internal API and is subject
35  * to change without notice.</b></p>
36  * @version 4.8 12/19/03
37  * @author Sun Microsystems, Inc
38  */

39
40 public class SnmpIndex implements Serializable JavaDoc {
41   
42     /**
43      * Initializes an <CODE>SnmpIndex</CODE> using a vector of object identifiers.
44      * <P>Following the RFC recommendations, every syntax that is used as a
45      * table index should have an object identifier representation. There are
46      * some guidelines on how to map the different syntaxes into an object identifier.
47      * In the different <CODE>SnmpValue</CODE> classes provided, there is a <CODE>toOid</CODE> method to get
48      * the object identifier of the value.
49      *
50      * @param oidList The list of Object Identifiers.
51      */

52     public SnmpIndex(SnmpOid[] oidList) {
53         size= oidList.length;
54         for(int i= 0; i <size; i++) {
55             // The order is important ...
56
//
57
oids.addElement(oidList[i]);
58         }
59     }
60  
61     /**
62      * Initializes an <CODE>SnmpIndex</CODE> using the specified Object Identifier.
63      *
64      * @param oid The Object Identifier.
65      */

66     public SnmpIndex(SnmpOid oid) {
67         oids.addElement(oid);
68         size= 1;
69     }
70   
71     /**
72      * Gets the number of Object Identifiers the index is made of.
73      *
74      * @return The number of Object Identifiers.
75      */

76     public int getNbComponents() {
77         return size;
78     }
79   
80     /**
81      * Gets the index as a vector of Object Identifiers.
82      *
83      * @return The index as a vector.
84      */

85     public Vector JavaDoc getComponents() {
86         return oids;
87     }
88   
89     /**
90      * Compares two indexes for equality.
91      *
92      * @param index The index to compare <CODE>this</CODE> with.
93      *
94      * @return <CODE>true</CODE> if the two indexes are equal, <CODE>false</CODE> otherwise.
95      */

96     public boolean equals(SnmpIndex index) {
97         
98         if (size != index.getNbComponents())
99             return false;
100     
101         // The two vectors have the same length.
102
// Compare each single element ...
103
//
104
SnmpOid oid1;
105         SnmpOid oid2;
106         Vector JavaDoc components= index.getComponents();
107         for(int i=0; i <size; i++) {
108             oid1= (SnmpOid) oids.elementAt(i);
109             oid2= (SnmpOid) components.elementAt(i);
110             if (oid1.equals(oid2) == false)
111                 return false;
112         }
113         return true;
114     }
115   
116     /**
117      * Compares two indexes.
118      *
119      * @param index The index to compare <CODE>this</CODE> with.
120      *
121      * @return The value 0 if the two OID vectors have the same elements, another value otherwise.
122      */

123     public int compareTo(SnmpIndex index) {
124     
125         int length= index.getNbComponents();
126         Vector JavaDoc components= index.getComponents();
127         SnmpOid oid1;
128         SnmpOid oid2;
129         int comp;
130         for(int i=0; i < size; i++) {
131             if ( i > length) {
132                 // There is no more element in the index
133
//
134
return 1;
135             }
136             // Access the element ...
137
//
138
oid1= (SnmpOid) oids.elementAt(i);
139             oid2= (SnmpOid) components.elementAt(i);
140             comp= oid1.compareTo(oid2);
141             if (comp == 0)
142                 continue;
143             return comp;
144         }
145         return 0;
146     }
147   
148     /**
149      * Returns a <CODE>String</CODE> representation of the index.
150      * The different elements are separated by "//".
151      *
152      * @return A string representation of the index.
153      */

154     public String JavaDoc toString() {
155         StringBuffer JavaDoc msg= new StringBuffer JavaDoc();
156         for(Enumeration JavaDoc e= oids.elements(); e.hasMoreElements(); ) {
157             SnmpOid val= (SnmpOid) e.nextElement();
158             msg.append( "//" + val.toString());
159         }
160         return msg.toString();
161     }
162     
163     // PRIVATE VARIABLES
164
//------------------
165

166     /**
167      * The list of OIDs.
168      * @serial
169      */

170     private Vector JavaDoc oids = new Vector JavaDoc();
171
172     /**
173      * The number of elements in the index.
174      * @serial
175      */

176     private int size = 0;
177 }
178
Popular Tags