jdk/src/share/classes/com/sun/jmx/snmp/Enumerated.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 23711 95fb268275e3
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23711
95fb268275e3 8039022: Fix serial lint warnings in sun.tools.java
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 1999, 2014, 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
package com.sun.jmx.snmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/** This class is used for implementing enumerated values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An enumeration is represented by a class derived from Enumerated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The derived class defines what are the permitted values in the enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An enumerated value is represented by an instance of the derived class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * It can be represented :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  - as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  - as a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p><b>This API is a Sun Microsystems internal API  and is subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * to change without notice.</b></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
23711
95fb268275e3 8039022: Fix serial lint warnings in sun.tools.java
darcy
parents: 23010
diff changeset
    48
@SuppressWarnings("serial") // JDK implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
abstract public class Enumerated  implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
   * Construct an enumerated with a default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
   * The default value is the first available in getIntTable().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    * @exception IllegalArgumentException One of the arguments passed to the method is illegal or inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  public Enumerated() throws IllegalArgumentException {
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
    57
    Enumeration<Integer> e =getIntTable().keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    if (e.hasMoreElements()) {
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
    59
      value = e.nextElement().intValue() ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      throw new IllegalArgumentException() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   * Construct an enumerated from its integer form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
   * @param valueIndex The integer form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   * @exception IllegalArgumentException One of the arguments passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   *            the method is illegal or inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  public Enumerated(int valueIndex) throws IllegalArgumentException {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23711
diff changeset
    74
    if (getIntTable().get(valueIndex) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      throw new IllegalArgumentException() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    value = valueIndex ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
   * Construct an enumerated from its Integer form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
   * @param valueIndex The Integer form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
   * @exception IllegalArgumentException One of the arguments passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
   *            the method is illegal or inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  public Enumerated(Integer valueIndex) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    if (getIntTable().get(valueIndex) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      throw new IllegalArgumentException() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    value = valueIndex.intValue() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
   * Construct an enumerated from its string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   * @param valueString The string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
   * @exception IllegalArgumentException One of the arguments passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
   *  to the method is illegal or inappropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  public Enumerated(String valueString) throws IllegalArgumentException {
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   103
    Integer index = getStringTable().get(valueString) ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (index == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      throw new IllegalArgumentException() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      value = index.intValue() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
   * Return the integer form of the enumerated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   * @return The integer form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    return value ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   * Returns an Java enumeration of the permitted integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   * @return An enumeration of Integer instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   130
  public Enumeration<Integer> valueIndexes() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    return getIntTable().keys() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
   * Returns an Java enumeration of the permitted strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
   * @return An enumeration of String instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   141
  public Enumeration<String> valueStrings() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    return getStringTable().keys() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
   * Compares this enumerated to the specified enumerated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
   * The result is true if and only if the argument is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
   * and is of the same class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
   * @param obj The object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
   * @return True if this and obj are the same; false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
   */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   156
  @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    return ((obj != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            (getClass() == obj.getClass()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            (value == ((Enumerated)obj).value)) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
   * Returns the hash code for this enumerated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
   * @return A hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
   */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   170
  @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
  public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    String hashString = getClass().getName() + String.valueOf(value) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    return hashString.hashCode() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
   * Returns the string form of this enumerated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
   * @return The string for for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
   */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   182
  @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
  public String toString() {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23711
diff changeset
   184
    return getIntTable().get(value);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
   * Returns the hashtable of the integer forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
   * getIntTable().get(x) returns the string form associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
   * to the integer x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
   * This method must be implemented by the derived class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
   * @return An hashtable for read-only purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   198
  protected abstract Hashtable<Integer,String>  getIntTable() ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
   * Returns the hashtable of the string forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
   * getStringTable().get(s) returns the integer form associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
   * to the string s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
   * This method must be implemented by the derived class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
   * @return An hashtable for read-only purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   212
  protected abstract Hashtable<String,Integer> getStringTable() ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
   * This variable keeps the integer form of the enumerated.
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   217
   * The string form is retrieved using getIntTable().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
  protected int value ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}