Package org.snmp4j.agent.mo.jmx.types

Source Code of org.snmp4j.agent.mo.jmx.types.EnumBitsType

/*_############################################################################
  _##
  _##  SNMP4J-AgentJMX - EnumBitsType.java 
  _##
  _##  Copyright (C) 2006-2009  Frank Fock (SNMP4J.org)
  _## 
  _##  This program is free software; you can redistribute it and/or modify
  _##  it under the terms of the GNU General Public License version 2 as
  _##  published by the Free Software Foundation.
  _##
  _##  This program is distributed in the hope that it will be useful,
  _##  but WITHOUT ANY WARRANTY; without even the implied warranty of
  _##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  _##  GNU General Public License for more details.
  _##
  _##  You should have received a copy of the GNU General Public License
  _##  along with this program; if not, write to the Free Software
  _##  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  _##  MA  02110-1301  USA
  _## 
  _##########################################################################*/
package org.snmp4j.agent.mo.jmx.types;

import org.snmp4j.smi.OctetString;
import javax.management.*;

public class EnumBitsType extends TypedAttribute {

  private Enum[] enumValues;
  private Class enumClass;
  private int offset = 0;

  public EnumBitsType(String name, Class enumClass, Enum[] enumValues) {
    super(name, byte[].class);
    this.enumClass = enumClass;
    this.enumValues = enumValues;
  }

  public EnumBitsType(String name, Class enumClass,
                      Enum[] enumValues, int offset) {
    this(name, enumClass, enumValues);
    this.offset = offset;
  }

  public int getOffset() {
    return offset;
  }

  public Object transformFromNative(Object nativeValue, ObjectName objectName) {
    Enum en;
    if (nativeValue instanceof Enum) {
      en = (Enum)nativeValue;
    }
    else {
      en = Enum.valueOf(enumClass, nativeValue.toString());
    }
    int o = offset + en.ordinal();
    StringBuffer buf = new StringBuffer("1");
    for (int i=0; i<o; i++) {
      buf.insert(0, "0");
    }
    while (buf.length() % 8 > 0) {
      buf.append("0");
    }
    return OctetString.fromString(buf.toString(), 2).toByteArray();
  }

  public Object transformToNative(Object transformedValue,
                                  Object oldNativeValue, ObjectName objectName) {
    OctetString os = new OctetString((byte[])transformedValue);
    String s = os.toString(2);
    int pos = s.indexOf("1");
    if (pos < 0) {
      return null;
    }
    pos -= offset;
    return enumValues[pos];
  }

}
TOP

Related Classes of org.snmp4j.agent.mo.jmx.types.EnumBitsType

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.