1. /*
  2. * @(#)file SnmpIndex.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.8
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.agent;
  12. // java imports
  13. //
  14. import java.io.Serializable;
  15. import java.util.Vector;
  16. import java.util.Enumeration;
  17. // jmx imports
  18. //
  19. import com.sun.jmx.snmp.SnmpOid;
  20. /**
  21. * Represents a SNMP index.
  22. * An <CODE>SnmpIndex</CODE> is represented as a <CODE>Vector</CODE> of <CODE>SnmpOid</CODE>.
  23. * <P>
  24. * This class is used internally and by the classes generated by <CODE>mibgen</CODE>.
  25. * You should not need to use this class directly.
  26. *
  27. * <p><b>This API is a Sun Microsystems internal API and is subject
  28. * to change without notice.</b></p>
  29. * @version 4.8 12/19/03
  30. * @author Sun Microsystems, Inc
  31. */
  32. public class SnmpIndex implements Serializable {
  33. /**
  34. * Initializes an <CODE>SnmpIndex</CODE> using a vector of object identifiers.
  35. * <P>Following the RFC recommendations, every syntax that is used as a
  36. * table index should have an object identifier representation. There are
  37. * some guidelines on how to map the different syntaxes into an object identifier.
  38. * In the different <CODE>SnmpValue</CODE> classes provided, there is a <CODE>toOid</CODE> method to get
  39. * the object identifier of the value.
  40. *
  41. * @param oidList The list of Object Identifiers.
  42. */
  43. public SnmpIndex(SnmpOid[] oidList) {
  44. size= oidList.length;
  45. for(int i= 0; i <size; i++) {
  46. // The order is important ...
  47. //
  48. oids.addElement(oidList[i]);
  49. }
  50. }
  51. /**
  52. * Initializes an <CODE>SnmpIndex</CODE> using the specified Object Identifier.
  53. *
  54. * @param oid The Object Identifier.
  55. */
  56. public SnmpIndex(SnmpOid oid) {
  57. oids.addElement(oid);
  58. size= 1;
  59. }
  60. /**
  61. * Gets the number of Object Identifiers the index is made of.
  62. *
  63. * @return The number of Object Identifiers.
  64. */
  65. public int getNbComponents() {
  66. return size;
  67. }
  68. /**
  69. * Gets the index as a vector of Object Identifiers.
  70. *
  71. * @return The index as a vector.
  72. */
  73. public Vector getComponents() {
  74. return oids;
  75. }
  76. /**
  77. * Compares two indexes for equality.
  78. *
  79. * @param index The index to compare <CODE>this</CODE> with.
  80. *
  81. * @return <CODE>true</CODE> if the two indexes are equal, <CODE>false</CODE> otherwise.
  82. */
  83. public boolean equals(SnmpIndex index) {
  84. if (size != index.getNbComponents())
  85. return false;
  86. // The two vectors have the same length.
  87. // Compare each single element ...
  88. //
  89. SnmpOid oid1;
  90. SnmpOid oid2;
  91. Vector components= index.getComponents();
  92. for(int i=0; i <size; i++) {
  93. oid1= (SnmpOid) oids.elementAt(i);
  94. oid2= (SnmpOid) components.elementAt(i);
  95. if (oid1.equals(oid2) == false)
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Compares two indexes.
  102. *
  103. * @param index The index to compare <CODE>this</CODE> with.
  104. *
  105. * @return The value 0 if the two OID vectors have the same elements, another value otherwise.
  106. */
  107. public int compareTo(SnmpIndex index) {
  108. int length= index.getNbComponents();
  109. Vector components= index.getComponents();
  110. SnmpOid oid1;
  111. SnmpOid oid2;
  112. int comp;
  113. for(int i=0; i < size; i++) {
  114. if ( i > length) {
  115. // There is no more element in the index
  116. //
  117. return 1;
  118. }
  119. // Access the element ...
  120. //
  121. oid1= (SnmpOid) oids.elementAt(i);
  122. oid2= (SnmpOid) components.elementAt(i);
  123. comp= oid1.compareTo(oid2);
  124. if (comp == 0)
  125. continue;
  126. return comp;
  127. }
  128. return 0;
  129. }
  130. /**
  131. * Returns a <CODE>String</CODE> representation of the index.
  132. * The different elements are separated by "//".
  133. *
  134. * @return A string representation of the index.
  135. */
  136. public String toString() {
  137. StringBuffer msg= new StringBuffer();
  138. for(Enumeration e= oids.elements(); e.hasMoreElements(); ) {
  139. SnmpOid val= (SnmpOid) e.nextElement();
  140. msg.append( "//" + val.toString());
  141. }
  142. return msg.toString();
  143. }
  144. // PRIVATE VARIABLES
  145. //------------------
  146. /**
  147. * The list of OIDs.
  148. * @serial
  149. */
  150. private Vector oids = new Vector();
  151. /**
  152. * The number of elements in the index.
  153. * @serial
  154. */
  155. private int size = 0;
  156. }