1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.jvmstat.monitor;
  27 
  28 /**
  29  * Provides a typesafe enumeration for the Variability attribute for
  30  * instrumentation objects.
  31  *
  32  * @author   Brian Doherty
  33  */
  34 public class Variability implements java.io.Serializable {
  35 
  36     /* The enumeration values for this typesafe enumeration must be
  37      * kept in synchronization with the Variability enum in the perfData.hpp file
  38      * in the HotSpot source base.
  39      */
  40 
  41     private static final int NATTRIBUTES = 4;
  42     private static Variability[] map = new Variability[NATTRIBUTES];
  43 
  44     private String name;
  45     private int value;
  46 
  47     /**
  48      * An invalid Variablity value.
  49      */
  50     public static final Variability INVALID = new Variability("Invalid",0);
  51 
  52     /**
  53      * Variability attribute representing Constant counters.
  54      */
  55     public static final Variability CONSTANT = new Variability("Constant",1);
  56 
  57     /**
  58      * Variability attribute representing a Monotonically changing counters.
  59      */
  60     public static final Variability MONOTONIC = new Variability("Monotonic",2);
  61 
  62     /**
  63      * Variability attribute representing Variable counters.
  64      */
  65     public static final Variability VARIABLE = new Variability("Variable",3);
  66 
  67     /**
  68      * Returns a string describing this Variability attribute.
  69      *
  70      * @return String - a descriptive string for this enum.
  71      */
  72     public String toString() {
  73         return name;
  74     }
  75 
  76     /**
  77      * Returns the integer representation of this Variability attribute.
  78      *
  79      * @return int - an integer representation of this Variability attribute.
  80      */
  81     public int intValue() {
  82         return value;
  83     }
  84 
  85     /**
  86      * Maps an integer value its corresponding Variability attribute.
  87      * If the integer value does not have a corresponding Variability enum
  88      * value, the {@link Variability#INVALID} is returned
  89      *
  90      * @param value an integer representation of a Variability attribute
  91      * @return Variability - The Variability object for the given
  92      *                       <code>value</code> or {@link Variability#INVALID}
  93      *                       if out of range.
  94      */
  95     public static Variability toVariability(int value) {
  96 
  97         if (value < 0 || value >= map.length || map[value] == null) {
  98             return INVALID;
  99         }
 100 
 101         return map[value];
 102     }
 103 
 104     private Variability(String name, int value) {
 105         this.name = name;
 106         this.value = value;
 107         map[value]=this;
 108     }
 109 
 110     private static final long serialVersionUID = 6992337162326171013L;
 111 }