/*_############################################################################
_##
_## SNMP4J-AgentJMX - AbstractSyntheticJMXIndexSupport.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.util;
import javax.management.ObjectName;
import org.snmp4j.agent.mo.jmx.JMXIndexSupport;
import org.snmp4j.smi.OID;
/**
* The <code>AbstractSyntheticJMXIndexSupport</code> maps a synthtically
* generated index to a JMX row identifier and vice versa. The synthetic
* index is generated by using the hash code of the JMX row identifier
* converted to a String.
*
* @author Frank Fock
*/
public abstract class AbstractSyntheticJMXIndexSupport
implements JMXIndexSupport
{
protected KeyIndexRelation keyIndexRelation;
public AbstractSyntheticJMXIndexSupport() {
this(50);
}
public AbstractSyntheticJMXIndexSupport(int initialSize) {
keyIndexRelation = new KeyIndexRelation(initialSize);
}
public OID mapToIndex(Object rowIdentifier) {
OID index = keyIndexRelation.getKeys().get(rowIdentifier);
if (index == null) {
return allocateNewIndex(rowIdentifier);
}
return index;
}
protected OID allocateNewIndex(Object rowIdentifier) {
int hashCode = rowIdentifier.hashCode();
OID index = new OID(new int[] { hashCode });
while (keyIndexRelation.getIndexes().containsKey(index)) {
hashCode++;
index.set(0, hashCode);
}
keyIndexRelation.getKeys().put(rowIdentifier, index);
keyIndexRelation.getIndexes().put(index, rowIdentifier);
return index;
}
public abstract ObjectName mapToRowMBean(Object rowIdentifier);
public Object mapToRowIdentifier(OID rowIndex) {
if (rowIndex == null) {
return null;
}
return keyIndexRelation.getIndexes().get(rowIndex);
}
public Object getRowIdentifier(Object nativeRowId, int nativeRowIndex) {
return nativeRowId;
}
}