jdk/src/jdk.snmp/share/classes/com/sun/jmx/snmp/agent/SnmpIndex.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23010 jdk/src/share/classes/com/sun/jmx/snmp/agent/SnmpIndex.java@6dadb192ad81
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14677
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package com.sun.jmx.snmp.agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
// java imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
// jmx imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jmx.snmp.SnmpOid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Represents a SNMP index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * An <CODE>SnmpIndex</CODE> is represented as a <CODE>Vector</CODE> of <CODE>SnmpOid</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class is used internally and by the classes generated by <CODE>mibgen</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * You should not need to use this class directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p><b>This API is a Sun Microsystems internal API  and is subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * to change without notice.</b></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class SnmpIndex implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final long serialVersionUID = 8712159739982192146L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Initializes an <CODE>SnmpIndex</CODE> using a vector of object identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <P>Following the RFC recommendations, every syntax that is used as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * table index should have an object identifier representation. There are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * some guidelines on how to map the different syntaxes into an object identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * In the different <CODE>SnmpValue</CODE> classes provided, there is a <CODE>toOid</CODE> method to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * the object identifier of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param oidList The list of Object Identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public SnmpIndex(SnmpOid[] oidList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        size= oidList.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for(int i= 0; i <size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // The order is important ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            oids.addElement(oidList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Initializes an <CODE>SnmpIndex</CODE> using the specified Object Identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param oid The Object Identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public SnmpIndex(SnmpOid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        oids.addElement(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        size= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Gets the number of Object Identifiers the index is made of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return The number of Object Identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public int getNbComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Gets the index as a vector of Object Identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return The index as a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public Vector<SnmpOid> getComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return oids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Compares two indexes for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param index The index to compare <CODE>this</CODE> with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return <CODE>true</CODE> if the two indexes are equal, <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public boolean equals(SnmpIndex index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (size != index.getNbComponents())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // The two vectors have the same length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Compare each single element ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        SnmpOid oid1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        SnmpOid oid2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Vector<SnmpOid> components= index.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for(int i=0; i <size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            oid1= oids.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            oid2= components.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (oid1.equals(oid2) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Compares two indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param index The index to compare <CODE>this</CODE> with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return The value 0 if the two OID vectors have the same elements, another value otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int compareTo(SnmpIndex index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int length= index.getNbComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Vector<SnmpOid> components= index.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        SnmpOid oid1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        SnmpOid oid2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for(int i=0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if ( i > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // There is no more element in the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // Access the element ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            oid1= oids.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            oid2= components.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            comp= oid1.compareTo(oid2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (comp == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns a <CODE>String</CODE> representation of the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * The different elements are separated by "//".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return A string representation of the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   167
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public String toString() {
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   169
        final StringBuilder msg= new StringBuilder();
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   170
        for(Enumeration<SnmpOid> e= oids.elements(); e.hasMoreElements(); ) {
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   171
            SnmpOid val= e.nextElement();
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   172
            msg.append("//").append( val.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return msg.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // PRIVATE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    //------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The list of OIDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   184
    private Vector<SnmpOid> oids = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * The number of elements in the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}