J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package com.sun.jmx.snmp.agent; |
| 28 | |
| 29 | |
| 30 | |
| 31 | // java imports |
| 32 | // |
| 33 | import java.io.Serializable; |
| 34 | import java.util.Vector; |
| 35 | import java.util.Enumeration; |
| 36 | |
| 37 | // jmx imports |
| 38 | // |
| 39 | import com.sun.jmx.snmp.SnmpOid; |
| 40 | |
| 41 | /** |
| 42 | * Represents a SNMP index. |
| 43 | * An <CODE>SnmpIndex</CODE> is represented as a <CODE>Vector</CODE> of <CODE>SnmpOid</CODE>. |
| 44 | * <P> |
| 45 | * This class is used internally and by the classes generated by <CODE>mibgen</CODE>. |
| 46 | * You should not need to use this class directly. |
| 47 | * |
| 48 | * <p><b>This API is a Sun Microsystems internal API and is subject |
| 49 | * to change without notice.</b></p> |
| 50 | */ |
| 51 | |
| 52 | public class SnmpIndex implements Serializable { |
| 53 | private static final long serialVersionUID = 8712159739982192146L; |
| 54 | |
| 55 | /** |
| 56 | * Initializes an <CODE>SnmpIndex</CODE> using a vector of object identifiers. |
| 57 | * <P>Following the RFC recommendations, every syntax that is used as a |
| 58 | * table index should have an object identifier representation. There are |
| 59 | * some guidelines on how to map the different syntaxes into an object identifier. |
| 60 | * In the different <CODE>SnmpValue</CODE> classes provided, there is a <CODE>toOid</CODE> method to get |
| 61 | * the object identifier of the value. |
| 62 | * |
| 63 | * @param oidList The list of Object Identifiers. |
| 64 | */ |
| 65 | public SnmpIndex(SnmpOid[] oidList) { |
| 66 | size= oidList.length; |
| 67 | for(int i= 0; i <size; i++) { |
| 68 | // The order is important ... |
| 69 | // |
| 70 | oids.addElement(oidList[i]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Initializes an <CODE>SnmpIndex</CODE> using the specified Object Identifier. |
| 76 | * |
| 77 | * @param oid The Object Identifier. |
| 78 | */ |
| 79 | public SnmpIndex(SnmpOid oid) { |
| 80 | oids.addElement(oid); |
| 81 | size= 1; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the number of Object Identifiers the index is made of. |
| 86 | * |
| 87 | * @return The number of Object Identifiers. |
| 88 | */ |
| 89 | public int getNbComponents() { |
| 90 | return size; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Gets the index as a vector of Object Identifiers. |
| 95 | * |
| 96 | * @return The index as a vector. |
| 97 | */ |
| 98 | public Vector<SnmpOid> getComponents() { |
| 99 | return oids; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Compares two indexes for equality. |
| 104 | * |
| 105 | * @param index The index to compare <CODE>this</CODE> with. |
| 106 | * |
| 107 | * @return <CODE>true</CODE> if the two indexes are equal, <CODE>false</CODE> otherwise. |
| 108 | */ |
| 109 | public boolean equals(SnmpIndex index) { |
| 110 | |
| 111 | if (size != index.getNbComponents()) |
| 112 | return false; |
| 113 | |
| 114 | // The two vectors have the same length. |
| 115 | // Compare each single element ... |
| 116 | // |
| 117 | SnmpOid oid1; |
| 118 | SnmpOid oid2; |
| 119 | Vector<SnmpOid> components= index.getComponents(); |
| 120 | for(int i=0; i <size; i++) { |
| 121 | oid1= oids.elementAt(i); |
| 122 | oid2= components.elementAt(i); |
| 123 | if (oid1.equals(oid2) == false) |
| 124 | return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Compares two indexes. |
| 131 | * |
| 132 | * @param index The index to compare <CODE>this</CODE> with. |
| 133 | * |
| 134 | * @return The value 0 if the two OID vectors have the same elements, another value otherwise. |
| 135 | */ |
| 136 | public int compareTo(SnmpIndex index) { |
| 137 | |
| 138 | int length= index.getNbComponents(); |
| 139 | Vector<SnmpOid> components= index.getComponents(); |
| 140 | SnmpOid oid1; |
| 141 | SnmpOid oid2; |
| 142 | int comp; |
| 143 | for(int i=0; i < size; i++) { |
| 144 | if ( i > length) { |
| 145 | // There is no more element in the index |
| 146 | // |
| 147 | return 1; |
| 148 | } |
| 149 | // Access the element ... |
| 150 | // |
| 151 | oid1= oids.elementAt(i); |
| 152 | oid2= components.elementAt(i); |
| 153 | comp= oid1.compareTo(oid2); |
| 154 | if (comp == 0) |
| 155 | continue; |
| 156 | return comp; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns a <CODE>String</CODE> representation of the index. |
| 163 | * The different elements are separated by "//". |
| 164 | * |
| 165 | * @return A string representation of the index. |
| 166 | */ |
| 167 | public String toString() { |
| 168 | StringBuffer msg= new StringBuffer(); |
| 169 | for(Enumeration e= oids.elements(); e.hasMoreElements(); ) { |
| 170 | SnmpOid val= (SnmpOid) e.nextElement(); |
| 171 | msg.append( "//" + val.toString()); |
| 172 | } |
| 173 | return msg.toString(); |
| 174 | } |
| 175 | |
| 176 | // PRIVATE VARIABLES |
| 177 | //------------------ |
| 178 | |
| 179 | /** |
| 180 | * The list of OIDs. |
| 181 | * @serial |
| 182 | */ |
| 183 | private Vector<SnmpOid> oids = new Vector<SnmpOid>(); |
| 184 | |
| 185 | /** |
| 186 | * The number of elements in the index. |
| 187 | * @serial |
| 188 | */ |
| 189 | private int size = 0; |
| 190 | } |