Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/Enumerated.java
38924 views
/*1* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jmx.snmp;262728import java.io.*;29import java.util.Hashtable;30import java.util.*;31323334/** This class is used for implementing enumerated values.35*36* An enumeration is represented by a class derived from Enumerated.37* The derived class defines what are the permitted values in the enumeration.38*39* An enumerated value is represented by an instance of the derived class.40* It can be represented :41* - as an integer42* - as a string43*44* <p><b>This API is a Sun Microsystems internal API and is subject45* to change without notice.</b></p>46*/4748abstract public class Enumerated implements Serializable {4950/**51* Construct an enumerated with a default value.52* The default value is the first available in getIntTable().53* @exception IllegalArgumentException One of the arguments passed to the method is illegal or inappropriate.54*/55public Enumerated() throws IllegalArgumentException {56Enumeration<Integer> e =getIntTable().keys();57if (e.hasMoreElements()) {58value = e.nextElement().intValue() ;59}60else {61throw new IllegalArgumentException() ;62}63}6465/**66* Construct an enumerated from its integer form.67*68* @param valueIndex The integer form.69* @exception IllegalArgumentException One of the arguments passed to70* the method is illegal or inappropriate.71*/72public Enumerated(int valueIndex) throws IllegalArgumentException {73if (getIntTable().get(new Integer(valueIndex)) == null) {74throw new IllegalArgumentException() ;75}76value = valueIndex ;77}7879/**80* Construct an enumerated from its Integer form.81*82* @param valueIndex The Integer form.83* @exception IllegalArgumentException One of the arguments passed to84* the method is illegal or inappropriate.85*/86public Enumerated(Integer valueIndex) throws IllegalArgumentException {87if (getIntTable().get(valueIndex) == null) {88throw new IllegalArgumentException() ;89}90value = valueIndex.intValue() ;91}929394/**95* Construct an enumerated from its string form.96*97* @param valueString The string form.98* @exception IllegalArgumentException One of the arguments passed99* to the method is illegal or inappropriate.100*/101public Enumerated(String valueString) throws IllegalArgumentException {102Integer index = getStringTable().get(valueString) ;103if (index == null) {104throw new IllegalArgumentException() ;105}106else {107value = index.intValue() ;108}109}110111112/**113* Return the integer form of the enumerated.114*115* @return The integer form116*/117118public int intValue() {119return value ;120}121122123/**124* Returns an Java enumeration of the permitted integers.125*126* @return An enumeration of Integer instances127*/128129public Enumeration<Integer> valueIndexes() {130return getIntTable().keys() ;131}132133134/**135* Returns an Java enumeration of the permitted strings.136*137* @return An enumeration of String instances138*/139140public Enumeration<String> valueStrings() {141return getStringTable().keys() ;142}143144145/**146* Compares this enumerated to the specified enumerated.147*148* The result is true if and only if the argument is not null149* and is of the same class.150*151* @param obj The object to compare with.152*153* @return True if this and obj are the same; false otherwise154*/155@Override156public boolean equals(Object obj) {157158return ((obj != null) &&159(getClass() == obj.getClass()) &&160(value == ((Enumerated)obj).value)) ;161}162163164/**165* Returns the hash code for this enumerated.166*167* @return A hash code value for this object.168*/169@Override170public int hashCode() {171String hashString = getClass().getName() + String.valueOf(value) ;172return hashString.hashCode() ;173}174175176/**177* Returns the string form of this enumerated.178*179* @return The string for for this object.180*/181@Override182public String toString() {183return getIntTable().get(new Integer(value)) ;184}185186187/**188* Returns the hashtable of the integer forms.189* getIntTable().get(x) returns the string form associated190* to the integer x.191*192* This method must be implemented by the derived class.193*194* @return An hashtable for read-only purpose195*/196197protected abstract Hashtable<Integer,String> getIntTable() ;198199200201/**202* Returns the hashtable of the string forms.203* getStringTable().get(s) returns the integer form associated204* to the string s.205*206* This method must be implemented by the derived class.207*208* @return An hashtable for read-only purpose209*/210211protected abstract Hashtable<String,Integer> getStringTable() ;212213214/**215* This variable keeps the integer form of the enumerated.216* The string form is retrieved using getIntTable().217*/218protected int value ;219220}221222223