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; |
| 28 | |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Represents an SNMP 64bits counter. |
| 33 | * |
| 34 | * <p><b>This API is a Sun Microsystems internal API and is subject |
| 35 | * to change without notice.</b></p> |
| 36 | */ |
| 37 | |
| 38 | public class SnmpCounter64 extends SnmpValue { |
| 39 | private static final long serialVersionUID = 8784850650494679937L; |
| 40 | |
| 41 | // CONSTRUCTORS |
| 42 | //------------- |
| 43 | /** |
| 44 | * Constructs a new <CODE>SnmpCounter64</CODE> from the specified long value. |
| 45 | * @param v The initialization value. |
| 46 | * @exception IllegalArgumentException The specified value is negative |
| 47 | * or larger than <CODE>Long.MAX_VALUE</CODE>. |
| 48 | */ |
| 49 | public SnmpCounter64(long v) throws IllegalArgumentException { |
| 50 | |
| 51 | // NOTE: |
| 52 | // The max value for a counter64 variable is 2^64 - 1. |
| 53 | // The max value for a Long is 2^63 - 1. |
| 54 | // All the allowed values for a conuter64 variable cannot be covered !!! |
| 55 | // |
| 56 | if ((v < 0) || (v > Long.MAX_VALUE)) { |
| 57 | throw new IllegalArgumentException() ; |
| 58 | } |
| 59 | value = v ; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructs a new <CODE>SnmpCounter64</CODE> from the specified <CODE>Long</CODE> value. |
| 64 | * @param v The initialization value. |
| 65 | * @exception IllegalArgumentException The specified value is negative |
| 66 | * or larger than <CODE>Long.MAX_VALUE</CODE>. |
| 67 | */ |
| 68 | public SnmpCounter64(Long v) throws IllegalArgumentException { |
| 69 | this(v.longValue()) ; |
| 70 | } |
| 71 | |
| 72 | // PUBLIC METHODS |
| 73 | //--------------- |
| 74 | /** |
| 75 | * Returns the counter value of this <CODE>SnmpCounter64</CODE>. |
| 76 | * @return The value. |
| 77 | */ |
| 78 | public long longValue() { |
| 79 | return value ; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Converts the counter value to its <CODE>Long</CODE> form. |
| 84 | * @return The <CODE>Long</CODE> representation of the value. |
| 85 | */ |
| 86 | public Long toLong() { |
| 87 | return new Long(value) ; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Converts the counter value to its integer form. |
| 92 | * @return The integer representation of the value. |
| 93 | */ |
| 94 | public int intValue() { |
| 95 | return (int)value ; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Converts the counter value to its <CODE>Integer</CODE> form. |
| 100 | * @return The <CODE>Integer</CODE> representation of the value. |
| 101 | */ |
| 102 | public Integer toInteger() { |
| 103 | return new Integer((int)value) ; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Converts the counter value to its <CODE>String</CODE> form. |
| 108 | * @return The <CODE>String</CODE> representation of the value. |
| 109 | */ |
| 110 | public String toString() { |
| 111 | return String.valueOf(value) ; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Converts the counter value to its <CODE>SnmpOid</CODE> form. |
| 116 | * @return The OID representation of the value. |
| 117 | */ |
| 118 | public SnmpOid toOid() { |
| 119 | return new SnmpOid(value) ; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Extracts the counter from an index OID and returns its |
| 124 | * value converted as an <CODE>SnmpOid</CODE>. |
| 125 | * @param index The index array. |
| 126 | * @param start The position in the index array. |
| 127 | * @return The OID representing the counter value. |
| 128 | * @exception SnmpStatusException There is no counter value |
| 129 | * available at the start position. |
| 130 | */ |
| 131 | public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { |
| 132 | try { |
| 133 | return new SnmpOid(index[start]) ; |
| 134 | } |
| 135 | catch(IndexOutOfBoundsException e) { |
| 136 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Scans an index OID, skips the counter value and returns the position |
| 142 | * of the next value. |
| 143 | * @param index The index array. |
| 144 | * @param start The position in the index array. |
| 145 | * @return The position of the next value. |
| 146 | * @exception SnmpStatusException There is no counter value |
| 147 | * available at the start position. |
| 148 | */ |
| 149 | public static int nextOid(long[] index, int start) throws SnmpStatusException { |
| 150 | if (start >= index.length) { |
| 151 | throw new SnmpStatusException(SnmpStatusException.noSuchName) ; |
| 152 | } |
| 153 | else { |
| 154 | return start + 1 ; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpCounter64</CODE> to another OID. |
| 160 | * @param source An OID representing an <CODE>SnmpCounter64</CODE> value. |
| 161 | * @param dest Where source should be appended. |
| 162 | */ |
| 163 | public static void appendToOid(SnmpOid source, SnmpOid dest) { |
| 164 | if (source.getLength() != 1) { |
| 165 | throw new IllegalArgumentException() ; |
| 166 | } |
| 167 | dest.append(source) ; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Performs a clone action. This provides a workaround for the |
| 172 | * <CODE>SnmpValue</CODE> interface. |
| 173 | * @return The SnmpValue clone. |
| 174 | */ |
| 175 | final synchronized public SnmpValue duplicate() { |
| 176 | return (SnmpValue)clone() ; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Clones the <CODE>SnmpCounter64</CODE> object, making a copy of its data. |
| 181 | * @return The object clone. |
| 182 | */ |
| 183 | final synchronized public Object clone() { |
| 184 | SnmpCounter64 newclone = null ; |
| 185 | try { |
| 186 | newclone = (SnmpCounter64) super.clone() ; |
| 187 | newclone.value = value ; |
| 188 | } catch (CloneNotSupportedException e) { |
| 189 | throw new InternalError() ; // vm bug. |
| 190 | } |
| 191 | return newclone ; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns a textual description of the type object. |
| 196 | * @return ASN.1 textual description. |
| 197 | */ |
| 198 | final public String getTypeName() { |
| 199 | return name ; |
| 200 | } |
| 201 | |
| 202 | // VARIABLES |
| 203 | //---------- |
| 204 | /** |
| 205 | * Name of the type. |
| 206 | */ |
| 207 | final static String name = "Counter64" ; |
| 208 | |
| 209 | /** |
| 210 | * This is where the value is stored. This long is positive. |
| 211 | * @serial |
| 212 | */ |
| 213 | private long value = 0 ; |
| 214 | } |