package org.jboss.ejb.txtimer;
import org.jboss.mx.util.ObjectNameFactory;
import javax.management.ObjectName;
import java.io.Serializable;
public class TimedObjectId implements Serializable
{
static final long serialVersionUID = -3579003021244520358L;
private ObjectName containerId;
private Object instancePk;
private int hashCode;
public TimedObjectId(ObjectName containerId, Object instancePk)
{
if (containerId == null)
throw new IllegalArgumentException("containerId cannot be null");
this.containerId = containerId;
this.instancePk = instancePk;
}
public TimedObjectId(ObjectName timedObjectId)
{
this(timedObjectId, null);
}
public ObjectName getContainerId()
{
return containerId;
}
public Object getInstancePk()
{
return instancePk;
}
public static TimedObjectId parse(String externalForm)
{
if (externalForm.startsWith("[") == false || externalForm.endsWith("]") == false)
throw new IllegalArgumentException("Square brackets expected arround: " + externalForm);
String inStr = externalForm.substring(1, externalForm.length() - 1);
if (inStr.startsWith("target=") == false)
throw new IllegalArgumentException("Cannot parse: " + externalForm);
String jmxStr = inStr.substring(7);
String pkStr = null;
int pkIndex = jmxStr.indexOf(",pk=");
if (pkIndex > 0)
{
pkStr = jmxStr.substring(pkIndex + 4);
jmxStr = jmxStr.substring(0, pkIndex);
}
ObjectName contatinerId = ObjectNameFactory.create(jmxStr);
return new TimedObjectId(contatinerId, pkStr);
}
public String toExternalForm()
{
String pkStr = (instancePk != null ? ",pk=" + instancePk : "");
return "[target=" + containerId + pkStr + "]";
}
public int hashCode()
{
if (hashCode == 0)
hashCode = toString().hashCode();
return hashCode;
}
public boolean equals(Object obj)
{
if (obj == this) return true;
if (obj instanceof TimedObjectId)
{
TimedObjectId other = (TimedObjectId)obj;
if (containerId.equals(other.containerId))
return (instancePk != null ? instancePk.equals(other.instancePk) : other.instancePk == null);
}
return false;
}
public String toString()
{
return toExternalForm();
}
}