org.weakref.jmx.guice.MapExportBinder Maven / Gradle / Ivy
package org.weakref.jmx.guice;
import org.weakref.jmx.internal.guava.base.Throwables;
import com.google.inject.multibindings.Multibinder;
import org.weakref.jmx.ObjectNames;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.util.Map;
public class MapExportBinder
{
protected final Multibinder> binder;
protected final Class keyClass;
private final Class valueClass;
MapExportBinder(Multibinder> binder, Class keyClass, Class valueClass)
{
this.binder = binder;
this.keyClass = keyClass;
this.valueClass = valueClass;
}
public void withGeneratedName(final NamingFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
try {
String itemName = valueNamingFunction.name(entry.getValue());
return new ObjectName(ObjectNames.generatedNameOf(valueClass, itemName));
}
catch (MalformedObjectNameException e) {
throw Throwables.propagate(e);
}
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
public void withGeneratedName(final ObjectNameFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
return valueNamingFunction.name(entry.getValue());
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
public void withGeneratedName(final MapNamingFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
try {
String itemName = valueNamingFunction.name(entry.getKey(), entry.getValue());
return new ObjectName(ObjectNames.generatedNameOf(valueClass, itemName));
}
catch (MalformedObjectNameException e) {
throw Throwables.propagate(e);
}
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
public void withGeneratedName(final MapObjectNameFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
return valueNamingFunction.name(entry.getKey(), entry.getValue());
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy