/*_############################################################################
_##
_## SNMP4J-AgentJMX - BooleanBitsType.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 BooleanBitsType extends TypedAttribute {
private int offset = 0;
public BooleanBitsType(String name, int offset) {
super(name, byte[].class);
this.offset = offset;
}
public int getOffset() {
return offset;
}
public Object transformFromNative(Object nativeValue, ObjectName objectName) {
StringBuffer buf =
new StringBuffer(((Boolean)nativeValue).booleanValue() ? "1" : "0");
for (int i=0; i<offset; 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);
if (s.length() <= offset) {
return false;
}
return (s.charAt(offset) == '1');
}
}